Python unittest 中的 setUp() 和 setUpClass() 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/23667610/
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
What is the difference between setUp() and setUpClass() in Python unittest?
提问by etja
What is the difference between setUp()
and setUpClass()
in the Python unittest
framework? Why would setup be handled in one method over the other?
Python框架setUp()
和setUpClass()
在 Pythonunittest
框架中有什么区别?为什么要以一种方法处理设置而不是另一种方法?
I want to understand what part of setup is done in the setUp()
and setUpClass()
functions, as well as with tearDown()
and tearDownClass()
.
我想了解什么设置的一部分在完成setUp()
和setUpClass()
功能,以及与tearDown()
和tearDownClass()
。
回答by Benjamin Hodgson
The difference manifests itself when you have more than one test method in your class. setUpClass
and tearDownClass
are run once for the whole class; setUp
and tearDown
are run before and after each test method.
当您的类中有多个测试方法时,差异就会显现出来。setUpClass
并tearDownClass
为全班运行一次;setUp
并tearDown
在每个测试方法之前和之后运行。
For example:
例如:
class Example(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("setUpClass")
def setUp(self):
print("setUp")
def test1(self):
print("test1")
def test2(self):
print("test2")
def tearDown(self):
print("tearDown")
@classmethod
def tearDownClass(cls):
print("tearDownClass")
When you run this test, it prints:
当您运行此测试时,它会打印:
setUpClass
setUp
test1
tearDown
.setUp
test2
tearDown
.tearDownClass
(The dots (.
) are unittest
's default output when a test passes.) Observe that setUp
and tearDown
appear before and after test1
andtest2
, whereas setUpClass
and tearDownClass
appear only once, at the beginning and end of the whole test case.
(该点(.
)是unittest
的默认输出时,测试通过)观察到setUp
和tearDown
之前和之后出现test1
和test2
,而setUpClass
和tearDownClass
只出现一次,在整个测试案例的开始和结束。
回答by Biggsy
What is the difference between setUp()
and setUpClass()
in the Python unittest
framework?
Python框架setUp()
和setUpClass()
在 Pythonunittest
框架中有什么区别?
The main difference (as noted in the answer by Benjamin Hodgson) is that setUpClass
is called only once and that is before all the tests, while setUp
is called immediately before each and every test. (NB: The same applies to the equivalent methods in other xUnit test frameworks, not just Python's unittest
.)
主要区别(如 Benjamin Hodgson 的回答中所述)是setUpClass
只调用一次并且在所有测试setUp
之前调用,而在每次测试之前立即调用。(注意:这同样适用于其他 xUnit 测试框架中的等效方法,而不仅仅是 Python 的unittest
.)
From the unittest
documentation:
从unittest
文档:
setUpClass()
A class method called before tests in an individual class are run. setUpClass is called with the class as the only argument and must be decorated as a classmethod():
setUpClass()
在运行单个类中的测试之前调用的类方法。setUpClass 使用类作为唯一参数调用,并且必须装饰为 classmethod():
@classmethod
def setUpClass(cls):
...
and:
和:
setUp()
Method called to prepare the test fixture. This is called immediately before calling the test method; other than AssertionError or SkipTest, any exception raised by this method will be considered an error rather than a test failure. The default implementation does nothing.
setUp()
调用方法来准备测试夹具。这是在调用测试方法之前立即调用的;除了 AssertionError 或 SkipTest,此方法引发的任何异常都将被视为错误而不是测试失败。默认实现什么都不做。
Why would setup be handled in one method over the other?
为什么要以一种方法处理设置而不是另一种方法?
This part of the question has not been answered yet. As per my comment in response to the answer by Gearon, the setUp
method is meant for elements of the fixture that are common to all tests (to avoid duplicating that code in each test). I find this is often useful as removing duplication (usually) improves readability and reduces the maintenance burden.
这部分问题尚未得到解答。根据我对 Gearon 回答的评论,该setUp
方法适用于所有测试通用的夹具元素(以避免在每个测试中重复该代码)。我发现这通常很有用,因为删除重复(通常)可以提高可读性并减少维护负担。
The setUpClass
method is for expensive elements that you would rather only have to do once, such as opening a database connection, opening a temporary file on the filesystem, loading a shared library for testing, etc. Doing such things before each test would slow down the test suite too much, so we just do it once before all the tests. This is a slight degradation in the independence of the tests but a necessary optimization in some situations. Arguably, one should not be doing such things in unit tests as it is usually possible to mock the database / filesystem / library / whatever without using the real thing. As such, I find that setUpClass
is rarely needed. However, it is useful when testing the above examples (or similar) becomes necessary.
该setUpClass
方法适用于您宁愿只执行一次的昂贵元素,例如打开数据库连接、打开文件系统上的临时文件、加载共享库进行测试等。在每次测试之前执行这些操作会减慢速度测试套件太多,所以我们只在所有测试之前做一次。这是测试独立性的轻微下降,但在某些情况下是必要的优化。可以说,人们不应该在单元测试中做这样的事情,因为通常可以在不使用真实事物的情况下模拟数据库/文件系统/库/任何东西。因此,我发现setUpClass
很少需要这样做。但是,当需要测试上述示例(或类似示例)时,它很有用。