Reputation: 11
I have a problem when running the test methods on two different modules. I have created suite function in a different module and defined it as a fixture.
On the two test classes, I have entered the created fixture, to use it as setup function just once.
For each test method, I have created setup and teardown methods. When the test methods of the first module are run, the test from the other class (second module) is starting in the first class and then are run again in the second class. So the test methods in the second class are run twice, once from the first class in the first module and then from the second class in the second module.
I want to run the test methods once per class or module, and not to be run twice.
Can someone help me to get a solution?
PS: I use for the suite fixture the parameter scope='session' (tried with the module is the same)!
Example (conftest.py):
@pytest.fixture(scope="session")
def suite():
print "start application"
open_app()
Example (test.py):
def setup_method(self, method):
if method.__name__ == 'test_1':
pass
elif method.__name__ == 'test_2':
pass
else:
print "test method not found"
def teardown_method(self, method):
print "teardown methods"
def test_1(self):
pass
def test_2(self):
pass
def test_3(self):
pass
def setup_test_3(self, testcase):
print "this is only for the test methd: test_3"
def teardown_test_3(self):
print "cleanup state after running test method test_3"
Upvotes: 1
Views: 1096
Reputation: 403
You can pass the parameter to your setup and teardown functions and put the condition based on the class or method name or module name and act it accordingly.
for e.g:--
def setup_method(value):
if value.__name__ == '<name of method from the module1>':
pass
elif value.__name__ == '<name of method from the module2>':
pass
Upvotes: 0