Reputation: 12796
Deo python support setUp() and tearDown() acts differently depending on the context? By all means, I am asking about if I can do something like this:
setUp() {
if(context1){
do A;
}
else{
do B;
}
}
tearDown() {
if(context1){
do A;
}
else{
do B;
}
}
Upvotes: 0
Views: 223
Reputation: 14091
Yeah, and just like you show it: use if blocks and only do particular parts of the setup if the condition is true.
I think what you're getting at is to have different versions of setUp
and tearDown
for different tests. I'd actually suggest that you either:
setUp
/tearDown
methodsor don't use setUp
and tearDown
at all - do something like this
class MyTestCase:
def _setup_for_foo_tests():
# blah blah blah
def _setup_for_bar_tests():
# blah blah blah
def test_foo_1():
self._setup_for_foo_tests()
# test code
def test_foo_2():
self._setup_for_foo_tests()
# test code
def test_bar_1():
self._setup_for_bar_tests()
# test code
# etc etc etc
Upvotes: 1
Reputation: 80811
You should think about doing 2 different classes (maybe with some a common ancestor) of test for each context of test you need, it would be easier.
Something like this :
class BaseTest():
def test_01a(self):
pass
class Context1TestCase(BaseTest, unittest.TestCase):
def setUp(self):
# do what you need for context1
def tearDown(self):
# do what you need for context1
class Context2TestCase(BaseTest, unittest.TestCase):
def setUp(self):
# do what you need for context2
def tearDown(self):
# do what you need for context2
this way, test_01a
will be executed once in context1, once in context2.
Upvotes: 2