Java setUp() 和 setUpBeforeClass() 的区别
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3413092/
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
Difference between setUp() and setUpBeforeClass()
提问by Sagar Varpe
When unit testing with JUnit, there are two similar methods, setUp()
and setUpBeforeClass()
. What is the difference between these methods? Also, what is the difference between tearDown()
and tearDownAfterClass()
?
使用 JUnit 进行单元测试时,有两种类似的方法,setUp()
和setUpBeforeClass()
. 这些方法有什么区别?此外,之间有什么区别tearDown()
和tearDownAfterClass()
?
Here are the signatures:
以下是签名:
@BeforeClass
public static void setUpBeforeClass() throws Exception {
}
@AfterClass
public static void tearDownAfterClass() throws Exception {
}
@Before
public void setUp() throws Exception {
}
@After
public void tearDown() throws Exception {
}
采纳答案by Andrzej Doyle
The @BeforeClass
and @AfterClass
annotated methods will be run exactly once during your test run - at the very beginning and end of the test as a whole, before anything else is run. In fact, they're run before the test class is even constructed, which is why they must be declared static
.
在@BeforeClass
和@AfterClass
注解的方法将你的测试运行期间只有一次运行-在测试整体的开始和结束,什么都运行之前。事实上,它们甚至在构建测试类之前运行,这就是为什么它们必须被声明static
。
The @Before
and @After
methods will be run before and after every test case, so will probably be run multiple times during a test run.
该@Before
和@After
方法将在每次测试案例之前和之后运行,所以在测试运行期间可能会多次运行。
So let's assume you had three tests in your class, the order of method calls would be:
所以让我们假设你的类中有三个测试,方法调用的顺序是:
setUpBeforeClass()
(Test class first instance constructed and the following methods called on it)
setUp()
test1()
tearDown()
(Test class second instance constructed and the following methods called on it)
setUp()
test2()
tearDown()
(Test class third instance constructed and the following methods called on it)
setUp()
test3()
tearDown()
tearDownAfterClass()
回答by Justin King
From the Javadoc:
从Javadoc:
Sometimes several tests need to share computationally expensive setup (like logging into a database). While this can compromise the independence of tests, sometimes it is a necessary optimization. Annotating a
public static void
no-arg method with@BeforeClass
causes it to be run once before any of the test methods in the class. The@BeforeClass
methods of superclasses will be run before those the current class.
有时,多个测试需要共享计算成本高的设置(例如登录数据库)。虽然这可能会影响测试的独立性,但有时这是必要的优化。注释
public static void
无参数方法@BeforeClass
会导致它在类中的任何测试方法之前运行一次。@BeforeClass
超类的方法将在当前类之前运行。
回答by netbrain
setUpBeforeClass is run before any method execution right after the constructor (run only once)
setUpBeforeClass 在构造函数之后的任何方法执行之前运行(仅运行一次)
setUp is run before each method execution
setUp 在每个方法执行之前运行
tearDown is run after each method execution
在每个方法执行后运行tearDown
tearDownAfterClass is run after all other method executions, is the last method to be executed. (run only once deconstructor)
tearDownAfterClass 在所有其他方法执行之后运行,是最后一个要执行的方法。(只运行一次解构器)
回答by madhurtanwani
Think of "BeforeClass" as a static initializer for your test case - use it for initializing static data - things that do not change across your test cases. You definitely want to be careful about static resources that are not thread safe.
将“BeforeClass”视为测试用例的静态初始化器——用它来初始化静态数据——在你的测试用例中不会改变的东西。你肯定要小心那些不是线程安全的静态资源。
Finally, use the "AfterClass" annotated method to clean up any setup you did in the "BeforeClass" annotated method (unless their self destruction is good enough).
最后,使用“AfterClass”注释方法清除您在“BeforeClass”注释方法中所做的任何设置(除非它们的自毁足够好)。
"Before" & "After" are for unit test specific initialization. I typically use these methods to initialize / re-initialize the mocks of my dependencies. Obviously, this initialization is not specific to a unit test, but general to all unit tests.
“之前”和“之后”用于单元测试特定的初始化。我通常使用这些方法来初始化/重新初始化我的依赖项的模拟。显然,这种初始化不是特定于单元测试,而是适用于所有单元测试。