python Django TestCase 测试顺序
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2581005/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Django TestCase testing order
提问by zs2020
If there are several methods in the test class, I found that the order to execute is alphabetical. But I want to customize the order of execution. How to define the execution order?
如果测试类中有多个方法,我发现执行的顺序是按字母顺序排列的。但我想自定义执行顺序。如何定义执行顺序?
For example: testTestA will be loaded first than testTestB.
例如:testTestA 将比 testTestB 先加载。
class Test(TestCase):
def setUp(self):
...
def testTestB(self):
#test code
def testTestA(self):
#test code
回答by unutbu
A tenet of unit-testing is that each test should be independent of all others. If in your case the code in testTestA must come before testTestB, then you could combine both into one test:
单元测试的原则是每个测试都应该独立于所有其他测试。如果在您的情况下 testTestA 中的代码必须出现在 testTestB 之前,那么您可以将两者合并为一个测试:
def testTestA_and_TestB(self):
# test code from testTestA
...
# test code from testTestB
or, perhaps better would be
或者,也许更好
def TestA(self):
# test code
def TestB(self):
# test code
def test_A_then_B(self):
self.TestA()
self.TestB()
The Test
class only tests those methods who name begins with a lower-case test...
.
So you can put in extra helper methods TestA
and TestB
which won't get run unless you explicitly call them.
该Test
班仅测试谁名称以小写的方法test...
。所以你可以加入额外的辅助方法TestA
,TestB
除非你明确调用它们,否则它们不会运行。
回答by Yaroslav
As far as I know, there is no way to order tests other than rename them. Could you explain why you need to run test cases in the specific order? In unit testing it usually considered as bad practice since it means that your cases are not independent.
据我所知,除了重命名测试之外,没有其他方法可以对测试进行排序。您能解释一下为什么需要按特定顺序运行测试用例吗?在单元测试中,它通常被认为是不好的做法,因为这意味着您的案例不是独立的。
回答by Wtower
To update on the topic (from documentation):
要更新该主题(来自文档):
Order in which tests are executed
In order to guarantee that all
TestCase
code starts with a clean database, the Django test runner reorders tests in the following way:
- All
TestCase
subclasses are run first.- Then, all other Django-based tests (test cases based on
SimpleTestCase
, includingTransactionTestCase
) are run with no particular ordering guaranteed nor enforced among them.- Then any other
unittest.TestCase
tests (includingdoctests
) that may alter the database without restoring it to its original state are run.Note: The new ordering of tests may reveal unexpected dependencies on test case ordering. This is the case with doctests that relied on state left in the database by a given
TransactionTestCase
test, they must be updated to be able to run independently.
为了保证所有
TestCase
代码都从一个干净的数据库开始,Django 测试运行器按以下方式重新排序测试:
TestCase
首先运行所有子类。- 然后,所有其他基于 Django 的测试(基于
SimpleTestCase
,包括 的 测试用例TransactionTestCase
)将在没有特定顺序保证或强制执行的情况下运行。- 然后运行可能会更改数据库而不将其恢复到其原始状态的任何其他
unittest.TestCase
测试(包括doctests
)。注意:新的测试顺序可能会揭示对测试用例顺序的意外依赖。这是依赖于给定
TransactionTestCase
测试在数据库中留下的状态的 doctests 的情况,它们必须更新才能独立运行。