zoukankan      html  css  js  c++  java
  • JUnit accuracy/failure/stress test区别

      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

  • 相关阅读:
    Codeforces ECR 83 C. Adding Powers (位运算)
    Codeforces Round #636div3 D. Constant Palindrome Sum (划分区间,差分)
    Codeforces Round #603 C. Everyone is a Winner!
    Centos7 下搭建SVN + Apache 服务器 风行天下
    完整部署CentOS7.2+OpenStack+kvm 云平台环境(1)基础环境搭建 风行天下
    云计算的理解 风行天下
    Python之路3【知识点】白话Python编码和文件操作 风行天下
    C#中TreeView组件使用方法初步
    复制文件时explorer.exe出错解决方法
    C# 里TreeView绑定数据库实现分类
  • 原文地址:https://www.cnblogs.com/WJQ2017/p/7582393.html
Copyright © 2011-2022 走看看