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

时间:2016-08-23 07:17:24

标签: python pytest

是否可以在pytest中使用分布式测试执行找到命令行参数?

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

os.sys.argv在我的代码中给出'-c',如果我正在执行我的pytest代码

1 个答案:

答案 0 :(得分:0)

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

这样做的方法是:

  1. 在项目中创建文件 conftest.py (如果它尚不存在)并添加选项 - env
  2. #conftest.py
    def pytest_addoption(parser):
        parser.addoption('--env', action="store", dest="env", default="",
        help="Define name of test environment")
    
    1. [可选]使用选项 - env
    2. 的值分配灯具 env_name
      #conftest.py
      @pytest.fixture
      def env_name(request):
          return request.config.getoption('env')
      
      1. 在测试功能中,使用fixture env_name ,您可以从中获取该选项的值。 如果未定义,请使用fixture request ,并使用request.config.getoption('env')
      2. 获取值
        #test.py
        def test1(env_name):
            print("Test Environment is: {}".format(env_name))
        
相关问题