Pytest 不会在类中选取测试方法 [英] Pytest does not pick up test methods inside a class

查看:46
本文介绍了Pytest 不会在类中选取测试方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

Always worked with python unittest2, and just started migrating to pytest. Naturally I am trying to draw parallels and one thing I am just not able to figure out is:

问题为什么 Pytest 没有选择我在测试"类中定义的测试方法.

Question Why does Pytest not pick up my test methods defined inside a "test" class.

什么对我有用

# 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

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

Not sure what other information is necessary to debug this?

感谢任何建议.

推荐答案

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

I was just running into a similar problem. I found that if you name your classes starting with "Test" then pytest will pick them up, otherwise your test class will not get discovered.

这有效:

class TestClass:
    def test_one(self):
        assert 0

这不会:

class ClassTest:
    def test_one(self):
        assert 0

这篇关于Pytest 不会在类中选取测试方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆