py.test does not find tests under a class

Michali picture Michali · Nov 29, 2013 · Viewed 18.7k times · Source

I am trying to create test classes that aren't unittest based.

This method under this class

class ClassUnderTestTests:

    def test_something(self):

cannot be detected and run when you call py.test from the command line or when you run this test in PyCharm (it's on its own module).

This

def test_something(self):

same method outside of a class can be detected and run.

I'd like to group my tests under classes and unless I'm missing something I'm following the py.test spec to do that.

Environment: Windows 7, PyCharm with py.test set as the test runner.

Answer

twil picture twil · Nov 29, 2013

By convention it searches for

Test prefixed test classes (without an init method)

eg.

# content of test_class.py
class TestClass:
    def test_one(self):
        x = "this"
        assert 'h' in x

    def test_two(self):
        x = "hello"
        assert hasattr(x, 'check')

See the docs here