Pytest does not pick up test methods inside a class

爱⌒轻易说出口 提交于 2021-02-08 01:54:47

问题


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:

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

What works for me

# 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'

What does not work for me (Does not work = Test methods do not get discovered)

# 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'

In pytest.ini I have

# 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?

Any advice is appreciated.


回答1:


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.

This works:

class TestClass:
    def test_one(self):
        assert 0

This does not:

class ClassTest:
    def test_one(self):
        assert 0



回答2:


Doesn't the class have to subclass unittest.TestCase?

And using py.test, why would you want to re-introduce the class boilerplate? One of the principles of py.test is to minimize boilerplate!

Here are the default conventions in the docs:

http://pytest.org/latest/goodpractises.html#python-test-discovery



来源:https://stackoverflow.com/questions/31971415/pytest-does-not-pick-up-test-methods-inside-a-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!