所有内容均由人工智能模型输出,其内容的准确性和完整性无法保证,不代表我的态度或观点。
该回答引用ChatGPT-3.5,仅为您提供参考,不保证完全正确
这个错误提示表明pytest没有找到名为 'loginName' 的fixture。在pytest中,fixture是一种在测试函数之前运行的函数,它可以为测试函数提供一些必要的参数或设置。在你的代码中,'loginName' 和 'password' 被传递给 test_login 函数作为参数,但pytest并不知道如何提供这些参数。
要解决这个问题,你可以考虑使用fixture来为 test_login 函数提供这些参数。你可以创建一个fixture函数,在该fixture中生成并返回 'loginName' 和 'password',然后在 test_login 函数中通过参数来使用这个fixture。
以下是一个示例,如何创建fixture来为 test_login 提供参数:
import pytest
# 创建一个名为 'login_credentials' 的fixture,用来提供 'loginName' 和 'password'
@pytest.fixture
def login_credentials():
loginName = "ljq"
password = "jxzy0219@AM"
return loginName, password
# 使用 'login_credentials' fixture 来传递参数给 'test_login' 函数
def test_login(login_credentials):
loginName, password = login_credentials
# 在这里继续你的测试逻辑
url = "/account/token"
# ... 其他测试代码 ...
print(res)
if __name__ == '__main__':
pytest.main([__file__])
在上面的示例中,login_credentials fixture 返回 'loginName' 和 'password',然后在 test_login 函数中通过参数 login_credentials 使用这些值。
确保在运行测试时,使用 pytest.main([__file__]) 来执行你的测试脚本,这样pytest将会自动识别和运行测试函数。
该回答引用NewBing,仅为您提供参考,不保证完全正确
根据我从网络上搜索到的信息¹²,您的错误可能是由于以下原因造成的:
- 您的
test_login函数被装饰为一个测试函数,但它需要两个参数loginName和password。Pytest会尝试从可用的fixture中找到这两个参数,但是没有找到。您需要定义一个fixture来提供这两个参数,或者将它们作为常量传递给测试函数。 - 您的
test_login函数没有使用self参数,这意味着它不是一个类方法,而是一个静态方法。如果您想在类中定义测试函数,您需要使用self参数,并且在调用类的其他方法时使用self.前缀。
我为您生成了一个可能的修改后的代码,您可以参考一下:
import pytest
def encip_md5(password):
# your encryption function here
return password
@pytest.fixture
def login_data():
# your fixture to provide login data here
return "ljq", "jxzy0219@AM"
class TestDemo8:
def test_login(self, login_data):
url = "/account/token"
loginName, password = login_data
loginName = loginName + "{{key}}"
password = encip_md5(encip_md5(encip_md5(password) + loginName) + "{{value}}")
data = {
"key": "{{key}}",
"loginName": loginName,
"password": password
}
res = RequestsUtil("base", "base_url").send_request(method="post", url=url, data=data).text
print(res)
if __name__ == '__main__':
pytest.main()
源: 与必应的对话, 2023/10/22
(1) python 3.x - Fixture not found pytest - Stack Overflow. https://stackoverflow.com/questions/70801967/fixture-not-found-pytest.
(2) python - pytest fixture of fixture, not found - Stack Overflow. https://stackoverflow.com/questions/47402435/pytest-fixture-of-fixture-not-found.
(3) fixture 'xxx' not found-CSDN博客. https://blog.csdn.net/weixin_44017891/article/details/103418031.
(4) python - Getting fixture not found in pytest - Stack Overflow. https://stackoverflow.com/questions/54925020/getting-fixture-not-found-in-pytest.
该回答引用文心一言,仅为您提供参考,不保证完全正确
你的问题在于,你正在试图在函数test_login(self, loginName, password)中使用两个fixture loginName 和 password,但pytest无法找到这两个fixture。在pytest中,fixture是用来提供测试所需的一些预置资源或者设置的一种机制。
要解决这个问题,你需要先为你的测试函数定义这两个fixture。你可以在同一个.py文件的顶部或者在conftest.py文件中定义fixture。定义的方式如下:
@pytest.fixture
def loginName():
return "ljq"
@pytest.fixture
def password():
return "jxzy0219@AM"
然后,你可以在你的测试函数中通过参数的方式使用这些fixture:
def test_login(self, loginName, password):
# your test code here
如果你已经定义了这些fixture,但仍然报错说找不到,那可能是因为你的测试函数无法正确地找到这些fixture。你需要确保你的测试函数在正确的范围内查找这些fixture。如果你的fixture是在不同的模块中定义的,那么你可能需要使用request对象来传递这些fixture。具体如下:
def test_login(self, request, loginName, password):
# your test code here
在这个函数中,request对象是一个特殊的参数,它提供了一个接口来获取在其他地方定义的fixture。你可以使用request.getfixturevalue('fixturename')来获取fixture的值。
该回答引用讯飞星火,仅为您提供参考,不保证完全正确
这个问题是因为在运行pytest时,没有正确地传递fixture参数。要解决这个问题,你需要在命令行中使用-k参数来指定要运行的测试函数,并使用--fixtures参数来显示可用的fixture。
首先,确保你已经安装了pytest库。如果没有安装,可以使用以下命令安装:
pip install pytest
然后,在命令行中运行以下命令:
pytest --fixtures test_requestDemo.py::TestDemo8::test_login
这将显示可用的fixture,包括loginName和password。接下来,你可以使用-k参数来指定要运行的测试函数,例如:
pytest -k loginName=ljq password=jxzy0219@AM test_requestDemo.py::TestDemo8::test_login
这将只运行test_login函数,并将loginName和password作为参数传递给它。
该回答引用通义千问,仅为您提供参考,不保证完全正确
这个错误是因为在test_login函数中,你试图使用一个名为loginName的fixture,但是这个fixture没有被定义。你需要确保在你的测试模块中定义了这个fixture。
如果你已经在测试模块中定义了这个fixture,那么可能是你没有正确地使用它。在pytest中,你需要在fixture的名称前加上@pytest.fixture来定义它,然后在需要使用这个fixture的测试函数中通过参数来传递它。例如:
@pytest.fixture
def loginName():
return "ljq"
@pytest.fixture
def password():
return "jxzy0219@AM"
def test_login(loginName, password):
url = "/account/token"
loginName = loginName + "{{key}}"
password = encip_md5(encip_md5(encip_md5(password) + loginName) + "{{value}}")
data = {
"key": "{{key}}",
"loginName": loginName,
"password": password
}
res = RequestsUtil("base", "base_url").send_request(method="post", url=url, data=data).text
print(res)
在这个例子中,我们定义了两个fixture:loginName和password。然后在test_login函数中,我们通过参数来传递这两个fixture。这样,pytest就会自动调用这两个fixture来获取它们的值,然后将这些值作为参数传递给test_login函数。