Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Pytest - How to pass an argument to setup_class?

Tags:

python

pytest

I have some code as shown below. I am getting a too few args error when I run it. I am not calling setup_class explicitly, so not sure how to pass any parameter to it. I tried decorating the method with @classmethod, but still see the same error.

The error that I am seeing is this - E TypeError: setup_class() takes exactly 2 arguments (1 given)

One point to note - If I do not pass any parameter to the class, and pass only cls, then I am not seeing the error.

Any help is greatly appreciated.

I did review these questions question #1 and question #2prior to posting. I did not understand the solutions posted to these questions, or how they would work.

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

    def some_method_in_a_helper(self):
        print "foo"

class Test_class:
    def setup_class(cls, fixture):
        print "!!! In setup class !!!"
        cls.a_helper = A_Helper(fixture)

    def test_some_method(self):
        self.a_helper.some_method_in_a_helper()
        assert 0 == 0
like image 966
Muthu Palanisamy Avatar asked Feb 02 '15 22:02

Muthu Palanisamy


People also ask

Can a pytest fixture take an argument?

Summary. You can pass arguments to fixtures with the params keyword argument in the fixture decorator, and you can also pass arguments to tests with the @pytest. mark.

How do you call pytest fixture directly?

To access the fixture function, the tests have to mention the fixture name as input parameter. Pytest while the test is getting executed, will see the fixture name as input parameter. It then executes the fixture function and the returned value is stored to the input parameter, which can be used by the test.

What is Autouse in pytest?

It is possible to apply a fixture to all of the tests in a hierarchy, even if the tests don't explicitly request a fixture, by passing autouse=True to the @pytest. fixture decorator. This is useful when we need to apply a side-effect before and/or after each test unconditionally. @pytest.fixture(autouse=True)

Can a pytest fixture be a test?

Pytest fixtures are functions that can be used to manage our apps states and dependencies. Most importantly, they can provide data for testing and a wide range of value types when explicitly called by our testing software. You can use the mock data that fixtures create across multiple tests.


2 Answers

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
like image 198
freakish Avatar answered Nov 15 '22 15:11

freakish


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
like image 29
metatoaster Avatar answered Nov 15 '22 15:11

metatoaster