【问题标题】:How do I add cli arguments to py.test?如何将 cli 参数添加到 py.test?
【发布时间】:2014-12-09 16:16:24
【问题描述】:

我正在尝试拥有可互换的配置文件,并且我正在使用 py.test 将测试套件发送到云端。

当我在终端中使用python main.py --site site1 在本地运行它们时,以下工作有效。

我试图弄清楚如何添加 cli 参数以便它可以与 py.test 一起使用

我有 3 个文件。 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 [选项] [file_or_dir] [file_or_dir] [...] py.test:错误:无法识别的参数:

【问题讨论】:

    标签: python selenium saucelabs


    【解决方案1】:

    我真的不明白你想要做什么,但是为了将 CLI 参数添加到 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")
    

    【讨论】:

      【解决方案2】:

      你的unittest.main(verbosity = 2)相当于pytest.main(['-v'])

      注意事项:

      • 在 pytest 中,我不确定是否可以选择详细程度
      • pytest.main(input) 上的一些参考资料并未指定输入必须是列表,但从今天开始它必须是。

      https://docs.pytest.org/en/latest/usage.html

      【讨论】:

        猜你喜欢
        • 2022-07-11
        • 1970-01-01
        • 2020-06-22
        • 2017-01-23
        • 1970-01-01
        • 2012-02-27
        • 2015-11-26
        • 1970-01-01
        • 2012-11-23
        相关资源
        最近更新 更多