PyTest中是否可以将命令行参数直接传递给conftest.py?

时间:2019-12-18 06:04:05

标签: python pytest

我知道有一种方法可以在测试本身like it was shown here中传递args,但是是否可以直接传递conftest.py?我想以这种方式为所有测试配置一些常见的东西。 这是我的conftest.py:

 import pytest
 from appium import webdriver
 import json
 import os

 from config import *

 CAPS_FILE = "android_9.0_emulator.json"


 def pytest_addoption(parser):
     parser.addoption("--desired-capabilities", action="store", default=CAPS_FILE)


 def pytest_generate_tests(metafunc):
     # This is called for every test. Only get/set command line arguments
     # if the argument is specified in the list of test "fixturenames".
     option_value = metafunc.config.option.name
     if 'desired-capabilities' in metafunc.fixturenames and option_value is not None:
         # metafunc.parametrize("desired_capabilities", [option_value])
         CAPS_FILE = option_value


 def get_desired_caps(json_file):
     with open(desired_caps_dir_path) as json_file:
         return json.load(json_file)


 @pytest.fixture(scope="session")
 def browser():
     driver = webdriver.Remote(appium_driver_url, desired_capabilities=get_desired_caps(CAPS_FILE))
     # driver = webdriver.Remote(appium_driver_url, desired_capabilities=get_desired_caps(metafunc.config.option))
     yield driver
     driver.quit()

更新:或者我应该使用此功能从命令行初始化/配置conftest.py本身:

def pytest_configure(config):
     option_val = config.option.name
     if 'desired-capabilities' in config.option and option_val is not None:
         CAPS_FILE = option_val

或者也许有更好的方法?

1 个答案:

答案 0 :(得分:0)

 def pytest_addoption(parser):
     desired_caps = None
     parser.addoption("--desired-capabilities", dest=desired_caps, action="store", default=CAPS_FILE)


 def pytest_configure(config):
     option_val = config.option.desired_capabilities
     if 'desired_capabilities' in config.option and option_val is not None:
         CAPS_FILE = option_val

注意:-所需功能->所需功能(目标参数似乎已损坏,或者我缺少某些内容...)