当我使用JUnit测试某些Java函数时,无论成功或失败,它都会输出一些结果。但是有时它看起来像一堆小蚂蚁,让我感到头晕。
也许生成此输出的最初目的是为了进行更好的调试。 但是,如果有成百上千的测试用例,则输出可能无法在控制台中完全显示。 因此,是否有一种方法可以在测试失败时触发操作,仅将失败的内容写入文件或将其发送到邮箱?
例如:
Sntest.java
package com.jut;
public class Sntest {
public String getString() {
return "String";
}
public Object getNull() {
return null;
}
}
SntestTest.java
package com.jut;
import org.junit.Assert;
import org.junit.Test;
import com.jut.Sntest;
public class SntestTest {
@Test
public void t1() {
Sntest sn = new Sntest();
String msg = sn.getString();
Assert.assertEquals("AString != String", "AString", msg);
}
@Test
public void t2() {
Sntest sn = new Sntest();
Assert.assertNull("Not null", sn.getNull());
}
}
在上面的示例中,测试方法t1将得到错误的结果并显示很多信息,如下所示:
Running com.jut.SntestTest
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.156 sec <<< FAILURE!
t1(com.jut.SntestTest) Time elapsed: 0.025 sec <<< FAILURE!
org.junit.ComparisonFailure: AString != String expected:<[A]String> but was:<[]String>
at org.junit.Assert.assertEquals(Assert.java:125)
at com.jut.SntestTest.t1(SntestTest.java:15)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.junit.runners.model.FrameworkMethod$1.runReflectiveCall(FrameworkMethod.java:45)
at org.junit.internal.runners.model.ReflectiveCallable.run(ReflectiveCallable.java:15)
at org.junit.runners.model.FrameworkMethod.invokeExplosively(FrameworkMethod.java:42)
at org.junit.internal.runners.statements.InvokeMethod.evaluate(InvokeMethod.java:20)
at org.junit.runners.ParentRunner.runLeaf(ParentRunner.java:263)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:68)
at org.junit.runners.BlockJUnit4ClassRunner.runChild(BlockJUnit4ClassRunner.java:47)
at org.junit.runners.ParentRunner$3.run(ParentRunner.java:231)
at org.junit.runners.ParentRunner$1.schedule(ParentRunner.java:60)
at org.junit.runners.ParentRunner.runChildren(ParentRunner.java:229)
at org.junit.runners.ParentRunner.access$000(ParentRunner.java:50)
at org.junit.runners.ParentRunner$2.evaluate(ParentRunner.java:222)
at org.junit.runners.ParentRunner.run(ParentRunner.java:300)
at org.apache.maven.surefire.junit4.JUnit4Provider.execute(JUnit4Provider.java:252)
at org.apache.maven.surefire.junit4.JUnit4Provider.executeTestSet(JUnit4Provider.java:141)
at org.apache.maven.surefire.junit4.JUnit4Provider.invoke(JUnit4Provider.java:112)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.maven.surefire.util.ReflectionUtils.invokeMethodWithArray(ReflectionUtils.java:189)
at org.apache.maven.surefire.booter.ProviderFactory$ProviderProxy.invoke(ProviderFactory.java:165)
at org.apache.maven.surefire.booter.ProviderFactory.invokeProvider(ProviderFactory.java:85)
at org.apache.maven.surefire.booter.ForkedBooter.runSuitesInProcess(ForkedBooter.java:115)
at org.apache.maven.surefire.booter.ForkedBooter.main(ForkedBooter.java:75)
但是我只想看到第一行到第四行,那么如何将输出转换为以下结果呢?
Running com.jut.SntestTest
Tests run: 2, Failures: 1, Errors: 0, Skipped: 0, Time elapsed: 0.156 sec <<< FAILURE!
t1(com.jut.SntestTest) Time elapsed: 0.025 sec <<< FAILURE!
org.junit.ComparisonFailure: AString != String expected:<[A]String> but was:<[]String>
答案 0 :(得分:0)
恐怕它无法更改为您想要的。它只是一个调用堆栈,可以帮助您查找问题