AttributeError: 模块 'pytest' 没有属性 'config'

use*_*r_5 0 functional-testing azure pytest flask azure-devops

我遵循了教程(使用 Azure DevOps 为 Python Flask 构建 DevOps CI/CD 管道)。有一个命令行任务来执行功能测试,我在运行它时遇到错误。

命令行任务脚本如下:

pip install selenium && pip install pytest && pytest Tests/functional_tests/ --webAppUrl=$(webAppUrl.AppServiceApplicationUrl) --junitxml=TestResults/test-results.xml
Run Code Online (Sandbox Code Playgroud)

这是用于功能测试的脚本:

import pytest
from selenium import webdriver
import unittest
import os
import sys
import pytest
import time

class FunctionalTests(unittest.TestCase):

def setUp(self):
    options = webdriver.ChromeOptions()
    options.add_argument('--no-sandbox')
    self.driver = webdriver.Chrome(os.path.join(os.environ["ChromeWebDriver"], 'chromedriver.exe'), chrome_options=options)
    self.driver.implicitly_wait(300)

def test_selenium(self):
    webAppUrl = pytest.config.getoption('webAppUrl')
    start_timestamp = time.time()
    end_timestamp = start_timestamp + 60*10
    while True:
        try:
            response = self.driver.get(webAppUrl)
            title = self.driver.title
            self.assertIn("Home Page - Python Flask Application", title)
            break
        except Exception as e:
            print('"##vso[task.logissue type=error;]Test test_selenium failed with error: ' + str(e))
            current_timestamp = time.time()
            if(current_timestamp > end_timestamp):
                raise
            time.sleep(5)

def tearDown(self):
    try:
        self.driver.quit()
    except Exception as e:
        print('tearDown.Error occurred while trying to close the selenium chrome driver: ' + str(e))
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

> webAppUrl = pytest.config.getoption('webAppUrl')
    AttributeError: module 'pytest' has no attribute 'config'
Run Code Online (Sandbox Code Playgroud)

本教程在此任务之前的任务中使用 python353x86,以确定 Python 版本,但我使用的是 Python 3.6.4 x86。

另一方面,在运行命令行任务时,会打印以下设置:

platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1
Run Code Online (Sandbox Code Playgroud)

我是 pytest 的新手,有人可以为此错误提供解决方案吗?我在其他 stackoverflow 页面中找不到答案。

hoe*_*ing 6

pytest.config全球在弃用pytest==4.0和删除pytest==5.0。如果您希望您的测试与 5.0 兼容,则需要通过自动使用装置传递配置实例才能访问它:

class FunctionalTests(unittest.TestCase):

    @pytest.fixture(autouse=True)
    def inject_config(self, request):
        self._config = request.config

    def test_selenium(self):
        webAppUrl = self._config.getoption('webAppUrl')
        ...
Run Code Online (Sandbox Code Playgroud)

这与我在通过 self 而不是方法参数对类的 Pytest 固定装置的回答中描述的方法相同。