Pytest - How to pass setup_class argument?

I have a code as shown below. When I start, I get an error of too few args . I do not call setup_class explicitly, so I'm not sure how to pass any parameter to it. I tried to decorate the @classmethod method, but still see the same error.

The error I see is E TypeError: setup_class() takes exactly 2 arguments (1 given)

One remark - if I do not pass any parameter to the class and do not pass only cls , then I do not see an error.

Any help is greatly appreciated.

I looked through these questions, question number 1 and question number 2 before publication. I did not understand the solutions sent to these questions, or how they will 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 
+5
source share
2 answers

You get this error because you are trying to combine two independent testing styles that py.test supports: classic unit testing and pytest fittings.

I suggest not mixing them and instead just define a snap with a class 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 
+8
source

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

You just have to follow the documentation and define the setup_class function as indicated, and then configure your class inside this method with your custom arguments, what you need inside this function, which will 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 
+3
source

Source: https://habr.com/ru/post/1212492/


All Articles