pytest 无法识别从命令行发送的参数

时间:2021-05-27 18:53:13

标签: python pytest

在我的实践中,我尝试构建我的混合测试框架。这是 My framework

GitHub 存储库的链接

它的结构是

...root/
   ...Base/
   ...Locators/
   ...Pages/
   ...Tests/
   ...conftest.py
   ...pytest.ini

我想让从终端命令或 conftest.py 文件传递​​配置成为可能 但是每次我运行命令时:

pytest search_test.py --browser chrome --server server_ip/wd/hub

出现错误:

ERROR: usage: pytest [options] [file_or_dir] [file_or_dir] [...]    
pytest: error:  unrecognized arguments: --browser chrome --server server_ip/wd/hub/wd/hub
inifile: path to inifile
rootdir: path to root directory

这是我的 conftest.py 文件的内容:

import pytest
import configparser
from selenium import webdriver


def environment_options(parser):
    parser.addoption('--browser', '-B', dest='BROWSER', choises=['chrome', 'chr', 'firefox', 'ff'],
                     help=f"possible values are: {['chrome', 'chr', 'firefox', 'ff']}")

    parser.addoption('--server', '-S', dest="SERVER")


@pytest.fixture(scope='class')
def environment_configuration(request):
    read_config = configparser.ConfigParser()
    # checking if browser was input from console or from config file from section Environment and assigment
    # it to browser_name variable

    browser_name = request.config.getoption(
        "BROWSER") or read_config.get("Environments", "browser")

    # checking if remote server was input from console or from config file from section Environment and assigment
    # it to remote_server variable
    remote_server = request.config.getoption(
        "SERVER") or read_config.get("Environments", "remote_server")

    try:
        request.cls.driver = webdriver.Remote(
            command_executor=remote_server,
            desired_capabilities={
                "browserName": browser_name})
    except BaseException:
        print("check browser or remote server configs")

    yield request.cls.driver

    request.cls.driver.close()
    request.cls.driver.quit()

1 个答案:

答案 0 :(得分:-1)

需要将函数名称从 environment_options 更改为 pytest_addoption,然后一切开始工作:-)