Python unittest 上的执行顺序 [英] Execution order on Python unittest
本文介绍了Python unittest 上的执行顺序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我需要为我的测试设置一个执行顺序,因为我需要在其他数据之前验证一些数据.可以下单吗?
I need to set an order of execution for my tests, because I need some data verified before the others. Is possible to set an order?
class OneTestCase(unittest.TestCase):
def setUp(self):
# something to do
def test_login (self):
# first test
pass
def test_other (self):
# any order after test_login
def test_othermore (self):
# any order after test_login
if __name__ == '__main__':
unittest.main()
推荐答案
最好不要这样做.
测试应该是独立的.
做你想做的最好的事情就是将代码放入测试调用的函数中.
To do what you want best would be to put the code into functions that are called by the test.
像这样:
def assert_can_log_in(self):
...
def test_1(self):
self.assert_can_log_in()
...
def test_2(self):
self.assert_can_log_in()
...
甚至可以拆分测试类并将断言放入setUp函数中.
Or even to split the test class and put the assertions into the setUp function.
class LoggedInTests(unittest.TestCase):
def setUp(self):
# test for login or not - your decision
def test_1(self):
...
当我拆分班级时,我经常编写更多更好的测试,因为测试被拆分了,我可以更好地了解所有应该测试的案例.
When I split the class I often write more and better tests because the tests are split up and I can see better through all the cases that should be tested.
这篇关于Python unittest 上的执行顺序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文