Does the setUp method from the unittest.TestCase knows what is the next test case that will be executed? For example:
import unittest
class Tests(unittest.TestCase):
def setUp(self):
print "The next test will be: " + self.next_test_name()
def test_01(self):
pass
def test_02(self):
pass
if __name__ == '__main__':
unittest.main()
Such code should produce upon execution:
The next test will be: test_01
The next test will be: test_02
No, unittest
makes no guarantee about the order of test execution.
Moreover, you should structure your unit tests not to rely on any particular order. If they require some state setup from another test method then you no longer have, by definition, a unit test.
The current test method name which will be executed lives in self._testMethodName
, but use it at your own risk (accessing _private
attributes is subject to breakage without warning). Do not use this to customise setUp
based on the particular test method, prefer to split tests requiring a different setup off into separate test classes.
class Tests(unittest.TestCase):
def setUp(self):
print "The next test will be: " + self.id()
def test_01(self):
pass
def test_02(self):
pass
if __name__ == '__main__':
unittest.main()
Which will produce:
The next test will be: __main__.Tests.test_01
The next test will be: __main__.Tests.test_02
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.