【问题标题】:AttributeError: module 'pytest' has no attribute 'config'AttributeError:模块'pytest'没有属性'config'
【发布时间】:2022-05-17 00:53:53
【问题描述】:

我遵循this 教程(使用 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

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

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))

我收到以下错误:

> webAppUrl = pytest.config.getoption('webAppUrl')
    AttributeError: module 'pytest' has no attribute 'config'

本教程之前的任务中使用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

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

【问题讨论】:

  • pytest.config global was removed in 5.0。要么使用旧版本的pytest,要么将配置引用存储在测试用例实例中,如图所示,例如here.
  • @hoefling,感谢您的回复。但我不确定如何在我的案例中使用链接中的答案。我正在使用 webdriver,这使得它与链接中的答案不同,所以不确定如何使用我的代码调整该答案。

标签: azure flask azure-devops pytest functional-testing


【解决方案1】:

pytest.configglobalpytest==4.0 中被弃用,并在pytest==5.0 中被删除。如果您希望您的测试与 5.0 兼容,则需要通过 autouse 夹具传递配置实例才能访问它:

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')
        ...

这与我在对Pytest fixture for a class through self not as method argument 的回答中描述的方法相同。

【讨论】:

  • 感谢您的回复。我认为这种方法不适用于 webdriver,我得到错误。 (我是 pytest 的新手,所以可能有不同的原因)。我收到此错误:[error]Test test_selenium failed with error: 'Home Page - Python Flask Application' not found in '' on this line: self.assertIn("Home Page - Python Flask Application", title)
  • 这是一个不同的错误,与测试本身有关。新错误也意味着无法访问配置的原始错误已解决。
  • 您得到的新错误可能会导致传递错误的 url 或根本没有传递 url。打印测试中webAppUrl的值,检查是否是预期值。
  • 我打印了网址,它是正确的。但是当我在这一行之后打印倾斜时:title = self.driver.title 它是 None。
  • 我建议如下。如果答案解决了最初的问题(我认为确实如此),那么考虑接受/支持答案,请参阅stackoverflow.com/help/someone-answers 并使用minimal reproducible example 提出一个描述新错误的新问题。新问题可能是URL错误或目标页面缺少标题,或查询页面时后端服务器未启动等。无法从问题中的sn-p推导出来。
【解决方案2】:

对我来说,我只需要将 pytest-django 更新到最新的 4.5.2(截至 22 年 5 月 16 日)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-02-26
    • 2021-06-09
    • 2020-04-03
    • 2021-02-06
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多