如何将JUnit4的测试通过/失败结果记录到文件中? [英] How to log test pass/fail results of JUnit4 to a file?
问题描述
运行给定的JUnit4测试时,我希望它生成所有TestResults的日志文件.我不想通过CI/ant/maven来调用它. 我希望它可以在随时随地调用JUnit测试的任何时间运行.如果无法实现,那么我想编写自己的运行程序,该运行程序有问题地调用我的AllTestSuites类并记录所有结果本身. /p>
这将是我的测试班之一:
public class SimpleTestSuite extends TestCase{
@Test
public void simpleTestPass() {
assertTrue(true);
}
@Test
public void simpleTestFail() {
assertTrue(false);
}
@Test
public void simpleTestException() {
throw new RuntimeException();
}
}
我将其包含在TestSuite中,其中包含我要运行的所有测试套件:
@RunWith(Suite.class)
@Suite.SuiteClasses({SimpleTestSuite.class, ExampleSuite.class,})
public final class AllTestSuites {}
我想调用AllTestSuites并使其生成如下的日志文件.记住,我更喜欢捕获JUnit4框架的结果总线上的内容,而不是重新发明/创建一个新的测试运行器.
simpleTestPass - pass - 1/1 assertions passed
simpleTestFail - fail - 0/1 assertions passed
simpleTestException - exception - stacktrace as follows...
在测试类和基础测试类中添加记录器,并将其定义为TestWatchMan
,如下所示:
Logger logger = LoggerFactory.getLogger(TestCase.class);
@Rule public MethodRule watchman = new TestWatchman() {
public void starting(FrameworkMethod method) {
logger.info("Test {} is running.", method.getName());
}
public void succeeded(FrameworkMethod method) {
logger.info("Test {} succesfully run.", method.getName());
}
public void failed(Throwable e, FrameworkMethod method) {
logger.error("Test {} failed with {} reason.",
method.getName(), e.getMessage());
}
};
When a given JUnit4 test runs, I want it to generate a logfile of all of its TestResults. I do not want to invoke this via CI/ant/maven. I want this to run anytime the JUnit test is called from anywhere. If that is not possible, then I would like to write my own runner which problematically invokes my AllTestSuites class and logs all the results itself.
Here would be one of my test classes:
public class SimpleTestSuite extends TestCase{
@Test
public void simpleTestPass() {
assertTrue(true);
}
@Test
public void simpleTestFail() {
assertTrue(false);
}
@Test
public void simpleTestException() {
throw new RuntimeException();
}
}
I included it in a TestSuite, which contains all of my test Suites to run:
@RunWith(Suite.class)
@Suite.SuiteClasses({SimpleTestSuite.class, ExampleSuite.class,})
public final class AllTestSuites {}
I want to invoke AllTestSuites and have it generate a log file such as below. Remember, I prefer to capture what is on the JUnit4 framework's result bus, and not to reinvent/create a new test runner.
simpleTestPass - pass - 1/1 assertions passed
simpleTestFail - fail - 0/1 assertions passed
simpleTestException - exception - stacktrace as follows...
Add logger in you Test Class and Base Test Class and define as TestWatchMan
as below:
Logger logger = LoggerFactory.getLogger(TestCase.class);
@Rule public MethodRule watchman = new TestWatchman() {
public void starting(FrameworkMethod method) {
logger.info("Test {} is running.", method.getName());
}
public void succeeded(FrameworkMethod method) {
logger.info("Test {} succesfully run.", method.getName());
}
public void failed(Throwable e, FrameworkMethod method) {
logger.error("Test {} failed with {} reason.",
method.getName(), e.getMessage());
}
};
这篇关于如何将JUnit4的测试通过/失败结果记录到文件中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!