Search code examples
pythonselenium-webdriverpytestautomation-testing

I am trying to add pytest to my test but I got collected 0 items message in terminal


from selenium import webdriver from selenium.webdriver.common.by import By from selenium.webdriver.edge.service import Service import pytest

@pytest.fixture()
def wrong_login_test():
website = 'https://www.saucedemo.com'
path =  "/Users/Bernardo Cabrera/PycharmProjects/Project/resource/msedgedriver.exe"

options = webdriver.EdgeOptions()
options.add_experimental_option("Detach", True)

service = Service(executable_path=path)
driver = webdriver.Edge(service=service)
driver.get(website)
driver.implicitly_wait(5)
driver.maximize_window()

username= driver.find_element(By.XPATH, '//*[@id="user-name"]')
password= driver.find_element(By.XPATH, '//*[@id="password"]')
login= driver.find_element(By.XPATH, '//*[@id="login-button"]')

username.send_keys("username")
password.send_keys("password")
login.click()

badicon = driver.find_element(By.CLASS_NAME, 'error-button')
assert badicon.is_displayed()

driver.save_screenshot('FailedLogin')

Solution

  • You can't use a fixture as a test directly. To make your method collectible by pytest, you should start its name with test_. In your case, keep your fixture, but split your test and call the fixture within the test method.

    In your fixture change:

    assert badicon.is_displayed()
    

    To:

    yield driver
    

    Then create a test method:

    def test_wrong_login(wrong_login_test):
        badicon = wrong_login_test.find_element(By.CLASS_NAME, 'error-button')
        assert badicon.is_displayed()