如何为py.test添加命令行参数?
我正在尝试使用可互换的配置文件,并且我在用py.test把测试套件发送到云端。
当我在终端中运行 python main.py --site site1
时,这个方法在本地是有效的。
现在我想弄明白如何添加命令行参数,以便它能在py.test中正常工作。
我有三个文件:main、config和site1_config。
main.py
if __name__ == "__main__":
parser = argparse.ArgumentParser(description='CLI tests for an environment')
parser.add_argument('--site', nargs='?', help='the site to use for the config')
parser.add_argument('unittest_args', nargs='*')
#Get our property
args = parser.parse_args()
if args.site:
config.localizeConfig(args.site)
sys.argv[1:] = args.unittest_args
unittest.main(verbosity = 2)
config.py
def localizeConfig(site):
localizedConfig = __import__(site+'_config');
# localizedConfig = __import__(site);
#print dir(localizedConfig)
props = filter(lambda a: not a.startswith('__'), dir(localizedConfig))
#Iterate over each property and set it on the config
for prop in props:
if prop != 'os' and prop != 'sys' and prop != 'webdriver':
globals()[prop] = getattr(localizedConfig, prop)
host_url = www.google.com
site1_config.py
host_url = www.yahoo.com
我想设置一个标志,这样当运行 py.test -n6 --boxed main.py site1
时,site1_config.py 的内容就会复制到 config.py 中。
我不太确定如何让这个在py.test中正常工作。
用法:py.test [选项] [文件或目录] [文件或目录] [...]
py.test: 错误:未识别的参数:
2 个回答
1
你在使用 unittest.main(verbosity = 2)
时的效果,在 pytest
中可以用 pytest.main(['-v'])
来实现。
需要注意的几点:
- 在
pytest
中,我不太确定是否可以选择详细程度。 - 关于
pytest.main(input)
的一些参考资料没有说明输入必须是一个列表,但截至今天,它确实必须是。
1
我不太明白你想要做什么,不过如果你想给py.test添加命令行参数,可以查看这个链接:http://pytest.org/latest/example/simple.html。
# content of conftest.py
import pytest
def pytest_addoption(parser):
parser.addoption("--cmdopt", action="store", default="type1",
help="my option: type1 or type2")