在junit 4中,有的时候可以在所有测试开始前,后,每个测试执行前,后进行监测,
需要继承的是RunListener类,例子如下,
首先是两个要测试的类,
public class TestFeatureOne {
@Test
public void testFirstFeature()
{
Assert.assertTrue(true);
}
}
public class TestFeatureTwo {
@Test
public void testSecondFeature()
{
Assert.assertTrue(true);
}
@Test
@Ignore
public void testSecondFeatureIngored()
{
Assert.assertTrue(true);
}
然后写一个继承runlistner的类,如下:
public class ExecutionListener extends RunListener
{
/**
* Called before any tests have been run.
* */
public void testRunStarted(Description description) throws java.lang.Exception
{
System.out.println("Number of testcases to execute : " + description.testCount());
}
/**
* Called when all tests have finished
* */
public void testRunFinished(Result result) throws java.lang.Exception
{
System.out.println("Number of testcases executed : " + result.getRunCount());
}
/**
* Called when an atomic test is about to be started.
* */
public void testStarted(Description description) throws java.lang.Exception
{
System.out.println("Starting execution of test case : "+ description.getMethodName());
}
/**
* Called when an atomic test has finished, whether the test succeeds or fails.
* */
public void testFinished(Description description) throws java.lang.Exception
{
System.out.println("Finished execution of test case : "+ description.getMethodName());
}
/**
* Called when an atomic test fails.
* */
public void testFailure(Failure failure) throws java.lang.Exception
{
System.out.println("Execution of test case failed : "+ failure.getMessage());
}
/**
* Called when a test will not be run, generally because a test method is annotated with Ignore.
* */
public void testIgnored(Description description) throws java.lang.Exception
{
System.out.println("Execution of test case ignored : "+ description.getMethodName());
}
public static void main(String[] args)
{
JUnitCore runner = new JUnitCore();
//Adding listener here
runner.addListener(new ExecutionListener());
runner.run(TestFeatureOne.class, TestFeatureTwo.class);
}
分别是在所有的测试进行前,进行后,每个测试开始前,调用失败后,监视忽略调的测试调用的,所以结果为:
Number of testcases to execute : 3
Starting execution of test case : testFirstFeature
Finished execution of test case : testFirstFeature
Starting execution of test case : testSecondFeature
Finished execution of test case : testSecondFeature
Execution of test case ignored : testSecondFeatureIngored
Number of testcases executed : 2
分享到:
相关推荐
JUnit 5 = JUnit Platform + JUnit Jupiter + JUnit Vintage,包含依赖包:junit-jupiter-5.5.1.jar,junit-jupiter-engine-5.5.1.jar,junit-jupiter-params-5.5.1.jar,junit-platform-launcher-1.5.1.jar,junit-...
| |安装安装mvn软件包将依赖项添加到您的pom中: < dependency> < groupId>io.orangebeard</ groupId> < artifactId>junit4-listener</ artifactId> < version>version</ version> < scope>test</ scope></ ...
junit4.1junit4.1junit4.1junit4.1junit4.1
ANT执行junit可运行
JUNIT介绍.JUNIT介绍.JUNIT介绍.JUNIT介绍.
Files contained in junit4-4.8.2.jar: LICENSE.txt META-INF/MANIFEST.MF junit.extensions.ActiveTestSuite.class junit.extensions.RepeatedTest.class junit.extensions.TestDecorator.class junit.extensions...
JUnit API JUnit API JUnit API JUnit API JUnit API
Junit入门实验Junit入门实验Junit入门实验Junit入门实验Junit入门实验Junit入门实验Junit入门实验Junit入门实验Junit入门实验Junit入门实验Junit入门实验
关于junit的介绍 关于关于junit的介绍关于junit的介绍关于junit的介绍关于junit的介绍关于junit的介绍关于junit的介绍
JUnit讲课文档JUnit讲课文档JUnit讲课文档JUnit讲课文档JUnit讲课文档JUnit讲课文档JUnit讲课文档
JUnit in Action 中文版英文版 JUnit Recipies好书哦!
JUnit是Java编程语言的单元测试框架,用于编写和可重复运行的自动化测试。JUnit4通过注解的方式来识别测试方法。目前支持的主要注解有(下列注解标示了单元测试的不同运行阶段执行的代码): @BeforeClass 全局只会...
当一个测试方法还没有具备测试条件时可以先标志为忽略的测试方法 @BeforeClass:在所有测试方法执行前执行,只执行一次 ,且必须为static void,一般用于初始化一些共享的实例或加载共享的资源 @AfterClass:在所有...
junit3 junit4 api,单元测试的利器
JUnit4
赠送jar包:junit-4.12.jar; 赠送原API文档:junit-4.12-javadoc.jar; 赠送源代码:junit-4.12-sources.jar; 包含翻译后的API文档:junit-4.12-javadoc-API文档-中文(简体)版.zip 对应Maven信息:groupId:...
JUnit学习资料
junit5.jar
junit4的jar包,使用annotation进行junit测试!
Junit5.7.2 离线jar