JUnitCore run() 方法在每次测试之前不会隐式调用 setUp()
我正在使用 JUnitCore 的 run(junit.framework.Test test)
并传入 ClassName.suite()
为 JUnit 测试用例创建自定义测试运行程序。我的测试已运行,但返回的结果为 null
。似乎对象没有在 setUp()
方法中初始化,导致 setUp()
显然从未按应有的方式调用,即使使用 @Before
代码>注释。如果我实例化每个测试方法中所需的每个对象,则测试成功。然而,这种方法很乏味,并且违背了测试类的目的。这种行为正常吗? JUnit 是否有更好的测试运行程序可以反映与 Eclipse 中的测试运行程序相同的行为?谢谢。
以下是运行程序的代码:
public class TestRunner
{
Result res = new Result();
String results = new String();
JUnitCore runner = new JUnitCore();
protected TestRunner()
{
}
public String runReport(Test input)
{
System.out.println(input.toString());
res = runner.run(input);
String testClass = "Class: ";
String testFailCt = "Failure Count: ";
String testFalures = "Failures: ";
String testRunCt = "Runs: ";
String testRunTm = "Run Time: ";
String testSuccess = "Success: ";
String newln = "\n";
results += testClass + input.getClass() + newln;
results += testFailCt + res.getFailureCount() + newln;
results += testFalures + newln;
List<Failure> failures = res.getFailures();
int i = 0;
for (Failure x: failures)
{
i++;
results += i +": " + x + newln;
}
results += testRunCt + res.getRunCount() + newln;
results += testRunTm + res.getRunTime() + newln;
results += testSuccess + res.wasSuccessful() + newln;
return results;
}
}
以下是从另一个类调用 runReport()
方法的方式:
runner.runReport(TestClassName.suite());
我应该将什么传递给 run()
以便 setUp()
会在每次测试之前隐式调用吗?我知道通过套房不会这样做。因此,我只是更改了我的测试用例,以便在每个测试中实例化所有必要的对象。
I am creating a custom test runner for JUnit test cases using JUnitCore's run(junit.framework.Test test)
and passing in ClassName.suite()
. My tests run, however the results returned are null
. It seems that objects are not being initialized in the setUp()
method cause setUp()
apparently is never called as it should, even with the @Before
annotation. The tests are successful if I instantiate each object required within each test method. This approach however is tedious and defeats the purpose of having a test class. Is this behavior normal? Are there better test runners out there for JUnit that reflect the same behavior as the test runner in Eclipse? Thanks.
Here is the code for the runner:
public class TestRunner
{
Result res = new Result();
String results = new String();
JUnitCore runner = new JUnitCore();
protected TestRunner()
{
}
public String runReport(Test input)
{
System.out.println(input.toString());
res = runner.run(input);
String testClass = "Class: ";
String testFailCt = "Failure Count: ";
String testFalures = "Failures: ";
String testRunCt = "Runs: ";
String testRunTm = "Run Time: ";
String testSuccess = "Success: ";
String newln = "\n";
results += testClass + input.getClass() + newln;
results += testFailCt + res.getFailureCount() + newln;
results += testFalures + newln;
List<Failure> failures = res.getFailures();
int i = 0;
for (Failure x: failures)
{
i++;
results += i +": " + x + newln;
}
results += testRunCt + res.getRunCount() + newln;
results += testRunTm + res.getRunTime() + newln;
results += testSuccess + res.wasSuccessful() + newln;
return results;
}
}
Here is how the runReport()
method is being called from another class:
runner.runReport(TestClassName.suite());
What should I pass into run()
so that setUp()
will be implicitly called before each test? I know passing in the suite will not do so. Therefore, I just altered my test cases so that all necessary objects are instantiated within each test.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
JUnit 版本 4 有注释支持 - 我认为 junit.framework 表明您正在使用版本 3。
如果您使用 JUnit 3 TestRunner 运行 JUnit 4 测试,您可能会发现以下感兴趣的文章:
JUnit 测试运行程序在运行测试之前创建测试
JUnit 常见问题解答 - 请参阅“编写测试”第 1 段。 3.
JUnit 4 的早期介绍
祝你好运!
JUnit version 4 has annotation support - I think junit.framework indicates that you are using Version 3.
If you are running JUnit 4 tests using a JUnit 3 TestRunner, you might find these articles of interest:
JUnit Test Runner that creates tests just before running them
JUnit FAQ - see "Writing Tests" para. 3.
An early look at JUnit 4
Good luck!