Amey
Amey

Reputation: 8548

Pytest does not pick up test methods inside a class

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.

Upvotes: 8

Views: 7404

Answers (2)

Jonathan
Jonathan

Reputation: 121

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

Upvotes: 12

moorecm
moorecm

Reputation: 359

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

Upvotes: -1

Related Questions