简体   繁体   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)

Hi, I would like to execute " test_supplier " function multiple times for multiple strings.嗨,我想为多个字符串多次执行“ test_supplier ”function。

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.如果我取消注释“@pytest.fixture()” ,那么甚至无法启动测试并在终端上得到以下错误。 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.

Fixtures (as the package implies) are used by pytest. pytest 使用夹具(正如 package 所暗示的)。 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. Pytest 查找以前缀test_开头的所有函数,然后为测试提供夹具。 This would result in calling test_supplier(["Walker", "Rio"]) , which doesn't seem like what you want to be doing.这将导致调用test_supplier(["Walker", "Rio"]) ,这似乎不是您想要做的。

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).如果您的测试范围只是这两种情况,那么我会将test_supplier更改为不同的名称,例如run_supplier (此处是领域知识,所以只需运行一秒钟)。 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如果您要测试比这更多的案例,我会研究 pytest 参数化: 如何测试列表中的所有元素

Ninja edit: One way to run pytest is to call ptest./my/file.py忍者编辑:运行 pytest 的一种方法是调用ptest./my/file.py

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

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