我关注了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在 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')
...
这与我在 Pytest fixture for a class through self not as method argument 的回答中描述的方法相同。 .
关于azure - 属性错误: module 'pytest' has no attribute 'config' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/62762845/