2016-08-23 235 views
1

是否有可能通过分布式测试执行在pytest中查找命令行参数?pytest命令行参数@ pytest.mark.parametrize

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

os.sys.argv是给在我的代码“-c”,如果我执行我的pytest代码

+0

获得的价值,我不知道我是否正确地理解你的问题,但确实[这个例子](HTTP:/ /docs.pytest.org/en/latest/example/simple.html#pass-different-values-to-a-test-function-depending-on-command-line-options)回答它? –

+0

上面的链接不适合我。但是,以下是:http://docs.pytest.org/en/latest/example/simple.html –

回答

0

的问题不是很清楚,但我想你想添加一个选项“ - -env“并读取它的值。

做到这一点的方法是:

  1. 在你的项目中创建一个文件conftest.py(如果不存在的话),并添加选项--env

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

  • [可选]指派夹具env_name与选项--env

  • #conftest.py 
    @pytest.fixture 
    def env_name(request): 
        return request.config.getoption('env') 
    
    的值
  • 在您的测试功能,使用夹具env_name,您可以获得该选项的值。 如果没有定义,使用夹具要求而不是与request.config.getoption('env')

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