AttributeError: module 'pytest' has no attribute 'config'
Asked Answered
S

2

6

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.

Saba answered 6/7, 2020 at 19:5 Comment(2)
pytest.config global was removed in 5.0. Either use an older version of pytest, or store the config reference in the test case instance as shown e.g. here.Frodi
@hoefling, Thanks for your reply. But I'm not sure how to use the answer in the link in my case. I'm using webdriver which makes it different from the answer in the link, so not sure how to adapt that answer with my code.Saba
F
10

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.

Frodi answered 7/7, 2020 at 8:1 Comment(6)
Thanks for your response. I think this approach is not working with webdriver, I get error. (I'm new to pytest, so it might have a different reason). I get this error: [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)Saba
This is a different error that has something to do with the test itself. The new error also means the original error with being unable to access the config is resolved.Frodi
The new error you get probably results in a wrong url passed or no url passed at all. Print the value of webAppUrl in the test and check whether it is an expected one.Frodi
I printed the URL, it was correct. But when I print tilte after this line: title = self.driver.title it's None.Saba
I would suggest the following. If the answer resolved the original issue (which I think it does), then consider accepting/upvoting the answer, see stackoverflow.com/help/someone-answers and ask a new question describing the new error with a minimal reproducible example. The new issue may lie in wrong URL or the target page missing the title, or the backend server not started when querying the page etc. It can't be derived from the snippet in the question.Frodi
Thanks, I accepted your solution and posted a new question in this link: #62792426Saba
A
2

for me, I just needed to updated pytest-django to the latest, 4.5.2 (as of 05/16/22).

Adham answered 16/5, 2022 at 16:53 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.