pytest not able to find fixture


#@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)

嗨,我想执行">testrongupplier";对多个字符串执行多次函数。

如果我执行上面的代码,然后在执行测试后,我在终端上得到以下错误

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.

并且如果I取消注释"@pytest.fixture((&">则甚至无法启动测试,并在终端上出现以下错误。有人能让我明白我做错了什么,这样就可以纠正了吗。谢谢

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

夹具(正如包所暗示的(由pytest使用。您不应该直接调用fixture,正如错误所述。Pytest查找所有以前缀test_开头的函数,然后为测试提供fixture。这将导致调用test_supplier(["Walker", "Rio"]),这似乎不是您想要做的。

如果您的测试范围仅为这两种情况,那么我会将test_supplier更改为不同的名称,如run_supplier(此处为域知识,请使用它运行一秒钟(。然后我会有:

def run_supplier(title):
...
def test_supplier_walker():
run_supplier("Walker")
def test_supplier_rio():
run_supplier("Rio")

如果你要测试更多的案例,我会研究pytest参数化:如何测试列表中的所有元素

Ninja编辑:运行pytest的一种方法是调用ptest ./my/file.py

最新更新