Pytest - How to pass an argument to setup_class?

Since you are using this with pytest, it will only call setup_class with one argument and one argument only, doesn't look like you can change this without changing how pytest calls this.

You should just follow the documentation and define the setup_class function as specified and then set up your class inside that method with your custom arguments that you need inside that function, which would look something like

class Test_class:
    @classmethod
    def setup_class(cls):
        print "!!! In setup class !!!"
        arg = '' # your parameter here
        cls.a_helper = A_Helper(arg)

    def test_some_method(self):
        self.a_helper.some_method_in_a_helper()
        assert 0 == 0

You get this error because you are trying to mix two independent testing styles that py.test supports: the classical unit testing and pytest's fixtures.

What I suggest is not to mix them and instead simply define a class scoped fixture like this:

import pytest

class A_Helper:
    def __init__(self, fixture):
        print "In class A_Helper"

    def some_method_in_a_helper(self):
        print "foo"

@pytest.fixture(scope='class')
def a_helper(fixture):
    return A_Helper(fixture)

class Test_class:
    def test_some_method(self, a_helper):
        a_helper.some_method_in_a_helper()
        assert 0 == 0

Tags:

Python

Pytest