码迷,mamicode.com
首页 > 编程语言 > 详细

Java课程学习笔记 — JUnit accuracy/failure/stress test区别

时间:2017-09-23 21:18:06      阅读:215      评论:0      收藏:0      [点我收藏+]

标签:.com   学习   except   hid   test   超时   png   public   单位   

  accuracy test(结果准确性测试)

  例如,Assert.assertEquals(expected, actual)。

  如果结果不符合期望则产生failure。说明程序逻辑有问题。

 

  failure test(抛出异常测试)

  expected属性用来指示期望抛出的异常类型。例如,@Test(expected = IllegalArgumentException.class)。

  如果结果不符合期望则产生failure。说明程序逻辑有问题。

 

  stress test(运行时间测试)

  timeout属性用来指示时间上限,单位是毫秒。例如,@Test(timeout = 300)。

  如果超时则产生error。说明程序本身性能达不到要求。

  

  举个例子

  技术分享

技术分享
 1 package edu.zju.cst.Student;
 2 
 3 public class Student {
 4     private String name;
 5 
 6     public String getName() {
 7         return name;
 8     }
 9 
10     public void setName(String name) {
11         this.name = name;
12     }
13     
14     public String speak(String stm) {
15         try {
16             Thread.sleep(400);
17         } catch (InterruptedException e) {
18             e.printStackTrace();
19         }
20         
21         return name + ": " + stm;
22     }
23 }
Student.java
技术分享
 1 package edu.zju.cst;
 2 
 3 import org.junit.After;
 4 import org.junit.Before;
 5 import org.junit.Test;
 6 
 7 import edu.zju.cst.Student.Student;
 8 import junit.framework.Assert;
 9 
10 public class StudentAccuracyTest {
11     private Student student;
12     
13     @Before
14     public void setUp() {
15         student = new Student();
16         student.setName("Tom");
17     }
18     
19     @Test
20     public void testSpeak() {
21         String expected = "Tom: hello";
22         String actual = student.speak("hell");
23         Assert.assertEquals(expected, actual);
24     }
25     
26     @After
27     public void tearDown() {
28         
29     }
30 }
StudentAccuracyTest.java
技术分享
 1 package edu.zju.cst;
 2 
 3 import org.junit.After;
 4 import org.junit.Before;
 5 import org.junit.Test;
 6 
 7 import edu.zju.cst.Student.Student;
 8 
 9 public class StudentFailureTest {
10     private Student student;
11     
12     @Before
13     public void setUp() {
14         student = new Student();
15         student.setName("Tom");
16     }
17     
18     @Test(expected = IllegalArgumentException.class)
19     public void testSpeak() throws IllegalArgumentException {
20         student.speak("hell");
21     }
22     
23     @After
24     public void tearDown() {
25         
26     }
27 }
StudentFailureTest.java
技术分享
 1 package edu.zju.cst;
 2 
 3 import org.junit.After;
 4 import org.junit.Before;
 5 import org.junit.Test;
 6 
 7 import edu.zju.cst.Student.Student;
 8 
 9 public class StudentStressTest {
10     private Student student;
11     
12     @Before
13     public void setUp() {
14         student = new Student();
15         student.setName("Tom");
16     }
17     
18     @Test(timeout = 300)
19     public void testSpeak() {
20         student.speak("hell");
21     }
22     
23     @After
24     public void tearDown() {
25         
26     }
27 }
StudentStressTest.java
技术分享
 1 package edu.zju.cst;
 2 
 3 import org.junit.runner.RunWith;
 4 import org.junit.runners.Suite;
 5 import org.junit.runners.Suite.SuiteClasses;
 6 
 7 @RunWith(Suite.class)
 8 @SuiteClasses({StudentAccuracyTest.class, StudentFailureTest.class,
 9     StudentStressTest.class})
10 public class TestSuite {
11     
12 }
TestSuite.java

  技术分享

 

  参考资料

  JUnit单元测试的几个规律总结

  JUnit4:Test注解的两个属性:expected和timeout

Java课程学习笔记 — JUnit accuracy/failure/stress test区别

标签:.com   学习   except   hid   test   超时   png   public   单位   

原文地址:http://www.cnblogs.com/WJQ2017/p/7582393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!