在Django中进行测试:TestCase类中的setUpClass,setUpTestData和setUp有什么区别? [英] Testing in django: what are differences between setUpClass, setUpTestData and setUp in TestCase class?
问题描述
setUpClass
, setUpTestData
和 setUp $ c $有什么区别c>在
TestCase
类中?
更具体地说,每种用例是什么?
More specifically, what are the use cases for each?
到目前为止我所了解的是:
What I've understood so far:
- 此方法在测试类中的所有测试之前运行一次
- 此方法运行一次如果数据库具有事务支持。否则它将在每次测试之前运行。
- 此方法在测试类中的每个测试之前运行。
从我上面提到的理解来看,setUpTestData似乎位于setUpClass和setUp之间的区域。为什么我们需要一个用于setuUpTestData的类级别方法,而 setUpClass
或 setUp
或
From the understanding I mentioned above, it seems setUpTestData lies in the area between setUpClass and setUp. Why do we need a class level method for setuUpTestData while the same effect could be achieved by either setUpClass
or setUp
or a combination of the both?
推荐答案
编辑:Alasdair发表评论后进行更新/更正
Update/Correction after Alasdair's comment
-
setUpClass
用于执行类范围的初始化/配置(例如,创建连接,加载Web驱动程序)。例如,使用setUpClass
打开数据库连接/会话时,可以使用tearDownClass
关闭它们。 $在运行任何测试之前,都会为TestCase调用一次b $ b -
setUpClass
。同样,在所有测试运行之后,将调用tearDownClass
。
setUpClass
is used to perform class-wide initialization/configuration (e.g. creating connections, loading webdrivers). When usingsetUpClass
for instance to open database connection/session you can usetearDownClass
to close them.setUpClass
is called once for the TestCase before running any of the tests. SimilarlytearDownClass
is called after all the tests have run.
文档中的注释:
SimpleTestCase及其子类(例如TestCase等)依赖于setUpClass()和tearDownClass()来执行整个类初始化(例如,覆盖设置)。如果您需要重写这些方法,请不要忘记调用超级实现:
SimpleTestCase and its subclasses (e.g. TestCase, ...) rely on setUpClass() and tearDownClass() to perform some class-wide initialization (e.g. overriding settings). If you need to override those methods, don’t forget to call the super implementation:
setUpTestData
-
setUpTestData
用于为每个TestCase创建初始测试数据。此方法由TestCase.setUpClass()( src调用) -
setUpTestData
称为TestCase一次,如文档。如果数据库不支持事务,将在每次测试运行之前调用setUpTestData
(感谢@Alasdair纠正我) setUpTestData
is used to create initial test data per TestCase. This method is called by TestCase.setUpClass() (src)setUpTestData
is called is once for TestCase, as explained in documentation. In case databases does not support transactions,setUpTestData
will be called before each test run (thanks @Alasdair for correcting me)-
setUp
setUp
will be called before each test run, and should be used to prepare test dataset for each test run.
setUpTestData
使用 setUpTestData
可以提高测试性能,请注意,在测试中对此数据所做的更改将在不同的测试运行之间持续存在。如果需要重新加载,可以通过 setUp
方法完成。
如果用于运行测试的数据库不支持事务,则性能会被否定(因为 setUpTestData
将在每次测试运行之前被调用)
Using setUpTestData
allows for test performance improvement, be aware that change to this data in tests will persist between different test runs. If needs to be reloaded it can be done so from setUp
method.
If database used for running tests does not support transactions, performance improvement is negated (as setUpTestData
will be called before each test run)
这篇关于在Django中进行测试:TestCase类中的setUpClass,setUpTestData和setUp有什么区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!