zs2*_*020 24 python django unit-testing
如果测试类中有多个方法,我发现执行的顺序是按字母顺序排列的.但我想自定义执行顺序.如何定义执行顺序?
例如:testTestA将首先加载testTestB.
class Test(TestCase):
def setUp(self):
...
def testTestB(self):
#test code
def testTestA(self):
#test code
Run Code Online (Sandbox Code Playgroud)
unu*_*tbu 52
单元测试的一个原则是每个测试应该独立于所有其他测试.如果在您的情况下testTestA中的代码必须在testTestB之前,那么您可以将两者结合到一个测试中:
def testTestA_and_TestB(self):
# test code from testTestA
...
# test code from testTestB
Run Code Online (Sandbox Code Playgroud)
或者,或许会更好
def TestA(self):
# test code
def TestB(self):
# test code
def test_A_then_B(self):
self.TestA()
self.TestB()
Run Code Online (Sandbox Code Playgroud)
该Test班仅测试谁名称以小写的方法test....所以你可以放入额外的辅助方法TestA,TestB除非你明确地调用它们,否则它们将无法运行.
Yar*_*lav 10
据我所知,除了重命名之外,没有办法订购测试.您能解释为什么需要按特定顺序运行测试用例吗?在单元测试中,它通常被认为是不好的做法,因为这意味着你的案件不是独立的.
要更新主题(来自文档):
为了保证所有
TestCase代码都以干净的数据库开头,Django测试运行器以下列方式重新排序测试:
TestCase首先运行所有子类.- 然后,所有其他基于Django的测试(基于测试用例
SimpleTestCase,包括 测试用例TransactionTestCase)在没有特定排序的情况下运行,也不会在其中强制执行.- 然后运行可能改变数据库而不将其恢复到其原始状态的任何其他
unittest.TestCase测试(包括doctests).注意:新的测试顺序可能会显示对测试用例排序的意外依赖性.这是doctests的情况,它依赖于给定
TransactionTestCase测试在数据库中留下的状态,它们必须更新才能独立运行.
| 归档时间: |
|
| 查看次数: |
10502 次 |
| 最近记录: |