gpt4 book ai didi

python - Pytest 不会在类中获取测试方法

转载 作者:太空宇宙 更新时间:2023-11-03 15:06:35 25 4
gpt4 key购买 nike

一直使用 python unittest2,刚刚开始迁移到 pytest。自然地,我试图画出相似之处,但我无法弄清楚的一件事是:

问题为什么 Pytest 不获取我在“测试”类中定义的测试方法。

什么对我有用

# login_test.py
import pytest
from frontend.app.login.login import LoginPage

@pytest.fixture
def setup():
login = LoginPage()
return login

def test_successful_login(setup):
login = setup
login.login("incorrect username","incorrect password")
assert login.error_msg_label().text == 'Invalid Credentials'

什么对我不起作用(不起作用 = 测试方法未被发现)

# login_test.py 
import pytest
from frontend.app.login.login import LoginPage


class LoginTestSuite(object):

@pytest.fixture
def setup():
login = LoginPage()
return login

def test_invalid_login(self, setup):
login = setup
login.login("incorrect username","incorrect password")
assert login.error_msg_label().text == 'Invalid Credentials'

pytest.ini 我有

# pytest.ini
[pytest]
python_files = *_test.py
python_classes = *TestSuite
# Also tried
# python_classes = *
# That does not work either

不确定调试它还需要哪些其他信息?

如有任何建议,我们将不胜感激。

最佳答案

我刚刚遇到了类似的问题。我发现如果你命名你的类以“Test”开头,那么 pytest 会选择它们,否则你的测试类将不会被发现。

这个有效:

class TestClass:
def test_one(self):
assert 0

这不是:

class ClassTest:
def test_one(self):
assert 0

关于python - Pytest 不会在类中获取测试方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31971415/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com