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.
conf.py file
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()
Baseclass.py
import pytest
@pytest.mark.usefixtures("setup")
class BaseClass:
pass
main test file
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
============================= test session starts =============================
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.
CodePudding user response:
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.
More on conftest file : here