Junit 4 Tutorials(Junit 4 教程) Junit4 七、超时测试
2014-07-08 17:06
337 查看
Junit 4超时测试(Timeout test)可以被用来测试方法的执行时间。 Junit 4 超时测试可以被用在:
在测试类的方法上使用 @Timeout 注解
测试类的所有方法应用 Timeout规则
测试类的所有方法应用 Timeout规则
Junit 4 提供了 Timeout 规则来测试类中的所有方法。如果类中的任意一个方法执行时间超过了在Timeout 规则中规定的值,测试方法将抛出异常,测试结果为失败。指定的超时参数是以毫秒记。
Timeout 规则
TimeoutRuleTest.java 测试Timeout 规则的测试类:
样例结果输出
结果在 eclipse junit 窗口中显示如下:
教程目录导航
Junit测试框架介绍
Junit Eclipse教程
Junit 4注解
Junit 4断言方法(Assert methods)
Junit 4参数化测试
Junit 4测试套件(Test Suite)
Junit 4忽略测试(Ignore
Test)
Junit 4超时测试(Timeout
Test)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[align=center]本文出处为 http://blog.csdn.net/luanlouis,转载请注明出处,谢谢![/align]
在测试类的方法上使用 @Timeout 注解
测试类的所有方法应用 Timeout规则
在测试类的方法上使用 @Timeout 注解
Junit 4 提供了 @Timeout 注解来测试任意特定方法的执行时间。如果测试方法的执行时间大于指定的超时参数,测试方法将抛出异常,测试结果为失败。指定的超时参数是以毫秒记.@Timeout 注解样例
TimeoutTest.java test class for timeout test.package in.co.javatutorials; import org.junit.Test; /** * @author javatutorials.co.in */ public class TimeoutTest { /** * Example of timeout test. * Test will fail if it takes more than 200 ms to execute */ @Test(timeout = 200) public void testTimeout() { while (true); } }
样例输出结果
结果在 eclipse junit 窗口中显示如下:测试类的所有方法应用 Timeout规则
Junit 4 提供了 Timeout 规则来测试类中的所有方法。如果类中的任意一个方法执行时间超过了在Timeout 规则中规定的值,测试方法将抛出异常,测试结果为失败。指定的超时参数是以毫秒记。Timeout 规则
TimeoutRuleTest.java 测试Timeout 规则的测试类:package in.co.javatutorials; import org.junit.Rule; import org.junit.Test; import org.junit.rules.Timeout; /** * @author javatutorials.co.in */ public class TimeoutRuleTest { /** * Rule is applied to all methods of class * time is specified in milliseconds */ @Rule public Timeout timeout = new Timeout(1000); /** * Example of timeout test. * Test will fail if it takes more than 1 sec to execute */ @Test public void testTimeout1() { while(true); } /** * Example of timeout test. * Test will fail if it takes more than 1 sec to execute */ @Test public void testTimeout2() { while(true); } }
样例结果输出
结果在 eclipse junit 窗口中显示如下:
源码下载
点击我下载源码教程目录导航
Junit测试框架介绍Junit Eclipse教程
Junit 4注解
Junit 4断言方法(Assert methods)
Junit 4参数化测试
Junit 4测试套件(Test Suite)
Junit 4忽略测试(Ignore
Test)
Junit 4超时测试(Timeout
Test)
---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
[align=center]本文出处为 http://blog.csdn.net/luanlouis,转载请注明出处,谢谢![/align]
相关文章推荐
- Junit 4 Tutorials(Junit 4 教程) 五、测试套件
- Junit 4 Tutorials(Junit 4 教程) 四、Junit4 参数化测试
- Junit 4 Tutorials(Junit 4 教程) 六、忽略测试
- Junit 测试 简明教程
- JUnit 测试插件教程
- parasoft Jtest 使用教程:生成、执行和扩展Junit测试用例
- [图解教程]Eclipse不可不知的用法之二:自动生成接口和JUnit测试类
- Junit 实例精讲基础教程(五) JUnit套件测试,一次性运行多个测试用例(测试类的测试方法)
- Junit 实例精讲基础教程(三) 使用@Test(timeout=5000)模拟超时进行单元测试
- junit 4 超时测试
- Junit 4 Tutorials(Junit 4 教程) 三、Junit4 断言方法
- parasoft Jtest 使用教程:生成、执行和扩展Junit测试用例
- Junit 小案例 测试超时
- TestNG 教程4 – 超时测试
- Junit 4 Tutorials(Junit 4 系列教程) 二、Junit4 注解
- Java基础教程:tutorialspoint-junit
- JUnit教程-- 创建套件测试
- 北风网JUnit测试驱动开发VIP培训教程
- Junit 4 Tutorials(Junit 4 教程) 一、Junit简介及Junit Eclipse 教程
- 原创:Spring整合junit测试框架(简易教程 基于myeclipse,不需要麻烦的导包)