免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1285 | 回复: 1
打印 上一主题 下一主题

junit4新特性-测试必备利器 2 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2012-02-25 11:56 |只看该作者 |倒序浏览

junit4新特性-测试必备利器 2








三、以下来源于:http://q.sohu.com/forum/5/topic/1531298


下面上三种基本的测试用例的程序编写:

1.基本测试
  1. [java] view plaincopyprint?import static org.junit.Assert.*;  
  2. import org.apache.commons.logging.*;  
  3. import org.junit.*;  
  4.   
  5.   
  6. public class Test1{  
  7. private static Log log = LogFactory.getLog(Test1.class);  
  8.   
  9. @Before//每个测试单元执行前执行该方法,方法级的前置,当然还有类级别的前置BeforeClass,只执行一次   
  10. public void init() {  
  11.   log.info("init.....");  
  12. }  
  13.   
  14. @After//每个测试单元执行后执行该方法,方法级的后置,当然还有类级别的后置AfterClass,只执行一次   
  15. public void destory() {  
  16.   log.info("release resource");  
  17. }  
  18.   
  19. public void functionOne() {  
  20.   System.out.println("");  
  21. }  
  22.   
  23. @Test(expected=NullPointerException.class)//指定抛出指定的异常   
  24. public void functionError() {  
  25.   String test = null;   
  26. //   assertNull(test);   
  27.    test.toString();  
  28. }  
  29.   
  30. @Test  
  31. public void Equals() {  
  32.   String testPoint = new String("true");  
  33.   String testTargt = new String("true1");  
  34.   assertEquals(testPoint, testTargt);  
  35. }  
  36.    
  37. @Ignore("此测试已被忽略")  
  38. @Test(timeout=1)//测试运行时间 (单位:millisecond)   
  39. public void timeout() {  
  40.   String testPoint = new String("true");  
  41.   String testTargt = new String("true1");  
  42.   assertTrue("Pattern did not validate zip code", true);  
  43. }  
  44.   
  45. }  
  46. import static org.junit.Assert.*;
  47. import org.apache.commons.logging.*;
  48. import org.junit.*;


  49. public class Test1{
  50. private static Log log = LogFactory.getLog(Test1.class);

  51. @Before//每个测试单元执行前执行该方法,方法级的前置,当然还有类级别的前置BeforeClass,只执行一次
  52. public void init() {
  53.   log.info("init.....");
  54. }

  55. @After//每个测试单元执行后执行该方法,方法级的后置,当然还有类级别的后置AfterClass,只执行一次
  56. public void destory() {
  57.   log.info("release resource");
  58. }

  59. public void functionOne() {
  60.   System.out.println("");
  61. }

  62. @Test(expected=NullPointerException.class)//指定抛出指定的异常
  63. public void functionError() {
  64.   String test = null;
  65. //   assertNull(test);
  66.    test.toString();
  67. }

  68. @Test
  69. public void Equals() {
  70.   String testPoint = new String("true");
  71.   String testTargt = new String("true1");
  72.   assertEquals(testPoint, testTargt);
  73. }

  74. @Ignore("此测试已被忽略")
  75. @Test(timeout=1)//测试运行时间 (单位:millisecond)
  76. public void timeout() {
  77.   String testPoint = new String("true");
  78.   String testTargt = new String("true1");
  79.   assertTrue("Pattern did not validate zip code", true);
  80. }

  81. }
复制代码
2.套件测试

测试用例类1:Test1.java

  1. [java] view plaincopyprint?public class Test1 {  
  2. @Test  
  3. public void test1(){  
  4.   System.out.println("test1");  
  5. }  
  6.    
  7. @Test  
  8. public void test2(){  
  9.   System.out.println("test2");  
  10. }  
  11. }  
  12. public class Test1 {
  13. @Test
  14. public void test1(){
  15.   System.out.println("test1");
  16. }

  17. @Test
  18. public void test2(){
  19.   System.out.println("test2");
  20. }
  21. }
复制代码
测试用例类2:Test2.java

  1. [java] view plaincopyprint?public class Test2 {  
  2. @Test  
  3. public void Tets3(){  
  4.   System.out.println("test3");  
  5. }  
  6. @Test  
  7. public void Test4(){  
  8.   System.out.println("test4");  
  9. }  
  10. }  
  11. public class Test2 {
  12. @Test
  13. public void Tets3(){
  14.   System.out.println("test3");
  15. }
  16. @Test
  17. public void Test4(){
  18.   System.out.println("test4");
  19. }
  20. }
复制代码
测试套件类:
  1. /**
  2. * 在 JUnit 4 中,套件语义被两个新注释所替代。
  3. * 第一个是 @RunWith,设计它是为了方便让不同的运行器
  4. * (除了构建进框架的运行器)执行一个特别的测试类。
  5. * JUnit 4 绑定一个叫做 Suite 的套件运行器,
  6. * 必须在 @RunWith 注释中指定这个运行器。
  7. * 不仅如此,还必须提供另一项叫做 @SuiteClasses 的注释,
  8. * 它将一个意欲表示测试套件的类列表作为参数。
  9. * @author JersoN
  10. *
  11. */

  12. [java] view plaincopyprint?@RunWith(Suite.class)  
  13. @SuiteClasses({Test1.class,Test2.class})  
  14. public class SuiteTest {  
  15.   
  16.    /*在这里还可以继续添加新的测试@Test*/  
  17.   
  18. }  
  19. @RunWith(Suite.class)
  20. @SuiteClasses({Test1.class,Test2.class})
  21. public class SuiteTest {

  22.    /*在这里还可以继续添加新的测试@Test*/

  23. }
复制代码
3.参数测试

参数测试代码要多一些
  1. [java] view plaincopyprint?import java.util.*;  
  2. import org.apache.commons.logging.*;  
  3. import org.junit.Test;  
  4. import org.junit.runner.RunWith;  
  5. import org.junit.runners.Parameterized;  
  6. import org.junit.runners.Parameterized.Parameters;  
  7. import static org.junit.Assert.*;  
  8.   
  9. @RunWith(Parameterized.class)//指定此测试类使用测试时使用参数集进行测试   
  10. public class MainTest {  
  11. Log log = LogFactory.getLog(MainTest.class);  
  12.   
  13. @Test  
  14. public void testEqual() {  
  15.   assertTrue(source.equals(target));  
  16. }  
  17.    
  18. @Test  
  19. public void testStringLength(){  
  20.   
  21. //可以看到以往做参数测试得把测试的参数传递进来,当我们要测试多个参数的时候很麻烦,Juint4用了更好的实现解决这个问题,你可以把定义的多个参数用于不同的测试   
  22.   assertTrue(source.length()==target.length());  
  23. }  
  24.   
  25. @Parameters  
  26. public static Collection params() {  
  27.   return Arrays.asList(new Object[][] {  
  28.     { "22101", "22101" },  
  29.     { "221x1", "22101" },  
  30.     { "22101-5150", "22101"},  
  31.     { "221015150", "221015150" }  
  32.   });  
  33. }  
  34.    
  35. /**
  36.   * 以下是为参数化测试所做的编码
  37.   */  
  38. private String source;  
  39. private String target;  
  40.    
  41. /**
  42.   * 参数化测试必须的构造函数
  43.   * @param source 对应参数集中的第一个参数
  44.   * @param target 对应参数集中的第二个参数
  45.   */  
  46. public MainTest(String source,String target){  
  47.   this.source = source;  
  48.   this.target = target;  
  49. }  
  50.    
  51. public String getSource() {  
  52.   return source;  
  53. }  
  54. public void setSource(String source) {  
  55.   this.source = source;  
  56. }  
  57. public String getTarget() {  
  58.   return target;  
  59. }  
  60. public void setTarget(String target) {  
  61.   this.target = target;  
  62. }  
  63.   
  64. }  
复制代码

论坛徽章:
0
2 [报告]
发表于 2012-02-25 11:56 |只看该作者
谢谢分享
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP