Sam
Sam

Reputation: 405

Pytest fixture inside a class is not executed

I tried to evaluate the below code, but the fixture method 'newFix' wasn't even evaluated, so the console didn't print 'This is executed' when I tried the execute using the below command

pytest -v --capture=no

But both Test1 and Test2 returned pass. It's supposed to print 'This is executed' before each test. But if I remove the class line, and make the functions general, then the fixture method 'newFix' is executed. Any idea why it's not executed inside a class? Please advise.

import pytest

class TestClass:

    @pytest.fixture()
    def newFix():
        print('This is executed')

    def test_Test1(newFix):
        assert True

    def test_Test2(newFix):
        assert True

Upvotes: 2

Views: 1006

Answers (1)

rlee827
rlee827

Reputation: 1873

The tests are non-static methods, so they should be defined as

def test_Test1(self, newFix):
    # Unit test here

Upvotes: 2

Related Questions