Fixture not found python

Getting fixture not found in pytest

I am getting following error while running pytest using following code im unable to figure out whats wrong please find below code snippets.

Console ouput :

================================================= test session starts ================================================= platform win32 -- Python 3.7.2, pytest-4.2.0, py-1.7.0, pluggy-0.8.1 rootdir: D:WorkspaceAutomationProject, inifile: plugins: cov-2.6.1, allure-pytest-2.5.5 collected 1 item testspagestest.py E [100%] ======================================================= ERRORS ======================================================== __________________________________________ ERROR at setup of test.test_test ___________________________________________ file D:WorkspaceAutomationProjecttestspagestest.py, line 5 def test_test(self): E fixture 'web_driver' not found > available fixtures: _UnitTestCase__pytest_class_setup, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, cov, doctest_namespace, monkeypatch, no_cover, pytestconfig, record_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory > use 'pytest --fixtures [testpath]' for help on them. D:WorkspaceAutomationProjecttestspagestest.py:5 =============================================== 1 error in 0.12 seconds ===============================================

My Base class contains following code:

from selenium import webdriver import pytest import unittest @pytest.fixture(scope="class") def web_driver(request): driver = webdriver.Chrome("C:/chromedriver.exe") request.cls.driver = driver yield web_driver.close() @pytest.mark.usefixtures("web_driver") class Base(unittest.TestCase): ''' This fixture contains the set up and tear down code for each test. ''' pass

and test class contains following code:

from core.web.Base import Base class test(Base): def test_test(self): self.driver.get("http://google.com")

The test fixture is web_driver still getting error not found !

Answer

web_driver() is defined outside Base class scope, so it’s invisible to the usefixtures as it is part of test class scope. You could move it to conftest file, but IMHO a better solution is to move web_driver inside Base

@pytest.mark.usefixtures("web_driver") class Base(unittest.TestCase): @pytest.fixture(scope="class") def web_driver(self, request): driver = webdriver.Chrome("C:/chromedriver.exe") request.cls.driver = driver yield driver.close()

As a side note, it should be driver.close() , not web_driver.close()

Читайте также:  Javascript является ли цифрой

Источник

Fixture not found pytest error is being displayed when I run my test

Here are my conf.py, baseClass.py and actual test file (test_e2e.py). Can you please check and let me know what is wrong in my code. Looks like everything is fine but still my test is not running and showing ‘fixture not found’ error.

import pytest from selenium import webdriver from selenium.webdriver.chrome.service import Service @pytest.fixture(scope="class") def setup(request): chrome_options = webdriver.ChromeOptions() chrome_options.add_argument("--start-maximized") # chrome_options.add_argument("headless") chrome_options.add_argument("--ignore-certificate-errors") ser = Service("C://chromedriver.exe") driver = webdriver.Chrome(service=ser,options=chrome_options) driver.get('https://google.com') request.cls.driver = driver yield driver.close() 
import pytest @pytest.mark.usefixtures("setup") class BaseClass: pass 
from selenium.webdriver.common.by import By from selenium.webdriver.support import expected_conditions as EC from selenium.webdriver.support.wait import WebDriverWait from utilities.BaseClass import BaseClass class TestOne(BaseClass): def test_e2e(self): self.driver.find_element(By.CSS_SELECTOR, "a[href*='shop']").click() cards = self.driver.find_elements(By.CSS_SELECTOR, ".card-title a") i = -1 for card in cards: i = i + 1 cardText = card.text print(cardText) if cardText == "Blackberry": self.driver.find_element(By.CSS_SELECTOR, ".card-footer button")[i].click() 

Error I get after execution

collecting … collected 1 item

test_e2e.py::TestOne::test_e2e ERROR [100%]
test setup failed
file C:\Users\Admin\Desktop\pythonselframework\tests\test_e2e.py, line 8
def test_e2e(self):
E fixture ‘setup’ not found

 available fixtures: cache, capfd, capfdbinary, caplog, capsys, capsysbinary, doctest_namespace, monkeypatch, pytestconfig, record_property, record_testsuite_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory use 'pytest --fixtures [testpath]' for help on them. 

I am expecting my selenium tests to run smooth. I dont find any error in my files but still when I run my test file, it shows an error.

>Solution :

Pytest use a single file for fixture discovery, the conftest.py file. You should then store the fixtures declaration in a conftest.py file (and not in a conf.py as you declared). This file should be stored at the test root folder.

Источник

Getting fixture not found in pytest

I am getting following error while running pytest using following code im unable to figure out whats wrong please find below code snippets.

Читайте также:  To-do CRUD

Console ouput :

================================================= test session starts ================================================= platform win32 -- Python 3.7.2, pytest-4.2.0, py-1.7.0, pluggy-0.8.1 rootdir: D:\Workspace\AutomationProject, inifile: plugins: cov-2.6.1, allure-pytest-2.5.5 collected 1 item tests\pages\test.py E [100%] ======================================================= ERRORS ======================================================== __________________________________________ ERROR at setup of test.test_test ___________________________________________ file D:\Workspace\AutomationProject\tests\pages\test.py, line 5 def test_test(self): E fixture 'web_driver' not found > available fixtures: _UnitTestCase__pytest_class_setup, cache, capfd, capfdbinary, caplog, capsys, capsysbinary, cov, doctest_namespace, monkeypatch, no_cover, pytestconfig, record_property, record_xml_attribute, recwarn, tmp_path, tmp_path_factory, tmpdir, tmpdir_factory > use 'pytest --fixtures [testpath]' for help on them. D:\Workspace\AutomationProject\tests\pages\test.py:5 =============================================== 1 error in 0.12 seconds =============================================== 

My Base class contains following code:

from selenium import webdriver import pytest import unittest @pytest.fixture(scope="class") def web_driver(request): driver = webdriver.Chrome("C:/chromedriver.exe") request.cls.driver = driver yield web_driver.close() @pytest.mark.usefixtures("web_driver") class Base(unittest.TestCase): ''' This fixture contains the set up and tear down code for each test. ''' pass 

and test class contains following code:

from core.web.Base import Base class test(Base): def test_test(self): self.driver.get("http://google.com") 

The test fixture is web_driver still getting error not found !

Solution

web_driver() is defined outside Base class scope, so it’s invisible to the usefixtures as it is part of test class scope. You could move it to conftest file, but IMHO a better solution is to move web_driver inside Base

@pytest.mark.usefixtures("web_driver") class Base(unittest.TestCase): @pytest.fixture(scope="class") def web_driver(self, request): driver = webdriver.Chrome("C:/chromedriver.exe") request.cls.driver = driver yield driver.close() 

As a side note, it should be driver.close() , not web_driver.close()

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Источник

Оцените статью