繁体   English   中英

pytest 找不到夹具

[英]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)

嗨,我想为多个字符串多次执行“ test_supplier ”function。

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

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.

如果我取消注释“@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 使用夹具(正如 package 所暗示的)。 您不应该直接调用固定装置,因为错误状态。 Pytest 查找以前缀test_开头的所有函数,然后为测试提供夹具。 这将导致调用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 参数化: 如何测试列表中的所有元素

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM