简体   繁体   English

@ pytest.mark.parametrize的Pytest命令行参数

[英]Pytest command line argument for @pytest.mark.parametrize

Is it possible to find command line arguments in pytest with distributed test execution? 是否可以通过分布式测试执行在pytest中找到命令行参数?

py.test -n 16  tests/tests_*****.py --env="TestEnvironemnt" --html=XYZ/Reports.html

os.sys.argv is giving as '-c' in my code , if am executing my pytest code 如果我正在执行我的pytest代码,os.sys.argv在我的代码中给出'-c'

The question is not very clear but I assume you want to add an option "--env" and read its value. 问题不是很清楚,但我假设你想添加一个选项“--env”并读取它的值。

The way to do it is: 做到这一点的方法是:

  1. Create a file conftest.py in your project (if it does not exist yet) and add the option --env 在项目中创建文件conftest.py (如果它还不存在)并添加选项--env

#conftest.py
def pytest_addoption(parser):
    parser.addoption('--env', action="store", dest="env", default="",
    help="Define name of test environment")

  1. [optional] Assign a fixture env_name with the value of option --env [可选]指派夹具env_name与选项的值--env

#conftest.py
@pytest.fixture
def env_name(request):
    return request.config.getoption('env')

  1. In your test function, use the fixture env_name , which which you can get the value of the option. 在测试函数中,使用fixture env_name ,您可以从中获取选项的值。 If not defined, use fixture request instead and get the value with request.config.getoption('env') 如果没有定义,请使用fixture 请求,并使用request.config.getoption('env')获取值

#test.py
def test1(env_name):
    print("Test Environment is: {}".format(env_name))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM