5

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.

user_5
  • 498
  • 1
  • 5
  • 22
  • [`pytest.config` global was removed in 5.0](https://docs.pytest.org/en/latest/deprecations.html#pytest-config-global). Either use an older version of `pytest`, or store the config reference in the test case instance as shown e.g. [here](https://stackoverflow.com/a/50135020/2650249). – hoefling Jul 06 '20 at 23:00
  • @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. – user_5 Jul 07 '20 at 07:25

2 Answers2

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.

hoefling
  • 59,418
  • 12
  • 147
  • 194
  • 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) – user_5 Jul 07 '20 at 10:33
  • 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. – hoefling Jul 07 '20 at 10:35
  • 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. – hoefling Jul 07 '20 at 11:02
  • I printed the URL, it was correct. But when I print tilte after this line: title = self.driver.title it's None. – user_5 Jul 07 '20 at 16:21
  • I would suggest the following. If the answer resolved the original issue (which I think it does), then consider accepting/upvoting the answer, see https://stackoverflow.com/help/someone-answers and ask a new question describing the new error with a [mcve]. 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. – hoefling Jul 07 '20 at 16:29
  • Thanks, I accepted your solution and posted a new question in this link: https://stackoverflow.com/questions/62791926/none-response-and-empty-title-from-selenium-webdriver-in-functional-test – user_5 Jul 08 '20 at 09:42
0

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