Home > OS >  pytest not able to find fixture
pytest not able to find fixture

Time:09-01

#@pytest.fixture()
def create_list():
    test_strings = ["Walker", "Rio"]
    return test_strings

def test_supplier(create_list):
    global driver
    chrome_linux_64 = './Drivers/chromedriver_linux64'
    driver = webdriver.Chrome(chrome_linux_64)
    driver.get("https://iprocure.com")
    username = driver.find_element(By.ID, "login")
    username.send_keys(login_credientials.LOGIN_USER)
    password = driver.find_element(By.ID, "Passwd")
    password.send_keys(login_credientials.LOGIN_PASSWORD)
    driver.find_element(By.ID, "btnLogin").click()
    driver.find_element(By.LINK_TEXT, create_list).click()
    driver.close()
    time.sleep(3)
    driver.quit()


for title in create_list():
    test_supplier(create_list = title)

Hi, I would like to execute "test_supplier" function multiple times for multiple strings.

If I execute above code then after executing the tests I get below error on terminal

E       fixture 'create_list' 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.

and if I uncomment "@pytest.fixture()" then not even able to start the tests and get below error on terminal. Could some one make me to understand where I am doing wrong so could be corrected. Thank you

Fixture "create_list" called directly. Fixtures are not meant to be called directly,
but are created automatically when test functions request them as parameters.

CodePudding user response:

Fixtures (as the package implies) are used by pytest. You should not be calling to fixtures directly, as the error states. Pytest looks for all functions that start with the prefix test_ and then provides fixtures to the test. This would result in calling test_supplier(["Walker", "Rio"]), which doesn't seem like what you want to be doing.

If the extent of your testing is just these two cases, then I would change test_supplier to a different name, like run_supplier (idk the domain knowledge here, so just run with it for a second). Then I would have:

def run_supplier(title):
    ...

def test_supplier_walker():
    run_supplier("Walker")

def test_supplier_rio():
    run_supplier("Rio")

If you are going to test more cases than this, I would look into pytest parameterization: How to test all elements from a list

Ninja edit: One way to run pytest is to call ptest ./my/file.py

  • Related