Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

AttributeError: module 'pytest' has no attribute 'config'

I followed this tutorial (Build DevOps CI/CD pipeline for Python Flask with Azure DevOps). There is a command line task to execute a functional test which I get an error while running it.

The command line task script is as follows:

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

This is the script used for functional test:

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

I get the following error:

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

The tutorial uses python353x86 in the task prior to this task, to determine the Python version, but I have used Python 3.6.4 x86.

In the other hand, while running command line task, the following settings are printed:

platform win32 -- Python 3.8.3, pytest-5.4.3, py-1.9.0, pluggy-0.13.1

I'm new to pytest, can someone please give a solution for this error? I couldn't find the answer in other stackoverflow pages.

like image 895
user_5 Avatar asked Sep 03 '26 22:09

user_5


1 Answers

pytest.configglobal was deprecated in pytest==4.0 and removed in pytest==5.0. If you want your tests to be compatible with 5.0, you need to pass the config instance via an autouse fixture in order to access it:

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

This is the same approach that I described in my answer to Pytest fixture for a class through self not as method argument.

like image 107
hoefling Avatar answered Sep 06 '26 21:09

hoefling



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!