setUp/tearDown (@Before/@After) 为什么我们在 JUnit 中需要它们? [英] setUp/tearDown (@Before/@After) why we need them in JUnit?

查看:21
本文介绍了setUp/tearDown (@Before/@After) 为什么我们在 JUnit 中需要它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相信大家都知道setUp(@Before)会在任何测试方法之前执行,而tearDown(@After)会在测试方法之后执行.

I believe that we are all know that setUp (@Before) will execute before any test method and tearDown(@After) will execute after test method.

我们也知道 Junit 将创建一个测试实例每个测试方法.

Also we know that Junit will create one instance of Test per test method.

我的问题是,我们可以将 setUp 方法内容移动到类 Constructor 并删除 setUp 方法吗?保留 setUp 方法有什么特殊原因吗?

my question is that can we just move setUp method content to class Constructor and remove setUp method? is there any specific reason to keep setUp method?

推荐答案

This (old) JUnit 最佳实践 文章是这样写的:

This (old) JUnit best practices article puts it like this:

设置测试用例构造函数不是一个好主意.考虑:

Do not use the test-case constructor to set up a test case

Setting up a test case in the constructor is not a good idea. Consider:

public class SomeTest extends TestCase
   public SomeTest (String testName) {
      super (testName);
      // Perform test set-up
   }
}

想象一下,在执行设置,设置代码抛出一个IllegalStateException.作为回应,JUnit 会抛出一个AssertionFailedError,说明测试用例不能实例化.这是一个例子结果堆栈跟踪:

Imagine that while performing the setup, the setup code throws an IllegalStateException. In response, JUnit would throw an AssertionFailedError, indicating that the test case could not be instantiated. Here is an example of the resulting stack trace:

junit.framework.AssertionFailedError: Cannot instantiate test case: test1   
    at junit.framework.Assert.fail(Assert.java:143)
    at junit.framework.TestSuite.runTest(TestSuite.java:178)
    at junit.framework.TestCase.runBare(TestCase.java:129)
    at junit.framework.TestResult.protect(TestResult.java:100)
    at junit.framework.TestResult.runProtected(TestResult.java:117)
    at junit.framework.TestResult.run(TestResult.java:103)
    at junit.framework.TestCase.run(TestCase.java:120)
    at junit.framework.TestSuite.run(TestSuite.java, Compiled Code)
    at junit.ui.TestRunner2.run(TestRunner.java:429)

此堆栈跟踪证明相当无信息;它只表明测试用例不能实例化.它没有详细说明原始错误的位置或地点起源.这种信息的缺乏使得很难推断出异常的根本原因.

This stack trace proves rather uninformative; it only indicates that the test case could not be instantiated. It doesn't detail the original error's location or place of origin. This lack of information makes it hard to deduce the exception's underlying cause.

而不是将数据设置在构造函数,执行测试设置覆盖 setUp().任何异常在 setUp() 中抛出被报告正确.比较此堆栈跟踪与前面的例子:

Instead of setting up the data in the constructor, perform test setup by overriding setUp(). Any exception thrown within setUp() is reported correctly. Compare this stack trace with the previous example:

java.lang.IllegalStateException: Oops
    at bp.DTC.setUp(DTC.java:34) 
    at junit.framework.TestCase.runBare(TestCase.java:127)
    at junit.framework.TestResult.protect(TestResult.java:100)
    at junit.framework.TestResult.runProtected(TestResult.java:117)
    at junit.framework.TestResult.run(TestResult.java:103)
    ...

这个堆栈跟踪更多信息量大;它显示了哪个异常被抛出 (IllegalStateException) 和从哪里.这使它变得容易得多解释测试设置的失败.

This stack trace is much more informative; it shows which exception was thrown (IllegalStateException) and from where. That makes it far easier to explain the test setup's failure.

这篇关于setUp/tearDown (@Before/@After) 为什么我们在 JUnit 中需要它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆