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
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.
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.
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)
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.
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
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With