Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 971
Amey Avatar asked Aug 12 '15 17:08

Amey


1 Answers

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
like image 82
Jonathan Avatar answered Oct 11 '22 06:10

Jonathan