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;
}
}
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.
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
methods or 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
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.