Skip to content
Advertisement

Can pytest run tests within a test class?

I have a bunch of tests which I decided to put within a class, sample code is below:

class IntegrationTests:

    @pytest.mark.integrationtest
    @pytest.mark.asyncio
    async def test_job(self):
        assert await do_stuff()

However, when I try to run the tests: pipenv run pytest -v -m integrationtest, they are not detected at all, where I got the following before moving them to a class:

5 passed, 4 deselected in 0.78 seconds

I now get this:

2 passed, 4 deselected in 0.51 seconds

Why does pytest not detect these tests? Are test classes not supported?

Advertisement

Answer

The name of the class needs to start with Test for the pytest discovery to find it.

class TestIntegration:

    @pytest.mark.integrationtest
    @pytest.mark.asyncio
    async def test_job(self):
        assert await do_stuff()

See Conventions for Python test discovery

Advertisement