zoukankan      html  css  js  c++  java
  • junit学习(3.x)

    自动化测试

    测试所有测试类

     1 import junit.framework.TestCase;
     2 import junit.framework.Assert;
     3 
     4 /**
     5  *测试类必须要继承TestCase类
     6  *在junit3.8中,测试方法需要满足以下原则:
     7  *1、public
     8  *2。void
     9  *3.无方法参数
    10  *4.方法名称必须以test开头
    11  *执行每个测试用例前会调用setup方法,后会调用tearDown方法
    12  */
    13 public class CalculatorTest extends TestCase {
    14     private Calculator cal;
    15     public CalculatorTest(String name){
    16         super(name);
    17     }
    18     @Override
    19     public void setUp() throws Exception {
    20         cal=new Calculator();
    21     }
    22     public void testAdd(){
    23         int result=cal.add(2, 3);
    24         Assert.assertEquals(5, result);
    25     }
    26     public void testSubtract(){
    27         int result=cal.subtract(2, 3);
    28         Assert.assertEquals(-1, result);
    29     }
    30     public void testMutiply(){
    31         int result=cal.multiply(2, 3);
    32         Assert.assertEquals(6, result);
    33     }
    34     public void testDivide(){
    35         int result=0;
    36         try {
    37             result = cal.divide(6, 2);
    38             
    39         } catch (Exception e) {
    40             // TODO Auto-generated catch block
    41             e.printStackTrace();
    42             Assert.fail();
    43         }
    44         Assert.assertEquals(3, result);
    45     }
    46     /**
    47      * Throwable为错误和异常的父类
    48      */
    49     public void testDivideByZero(){
    50         Throwable tx=null;
    51         try {
    52             cal.divide(2, 0);
    53             Assert.fail();
    54         } catch (Exception e) {
    55             tx=e;
    56             e.printStackTrace();
    57         }
    58         Assert.assertEquals(Exception.class,tx.getClass());
    59         Assert.assertEquals("除数不能为0", tx.getMessage());
    60     }
    61 }

    添加进自动化测试

     1 import junit.extensions.RepeatedTest;
     2 import junit.framework.TestCase;
     3 import junit.framework.TestSuite;
     4 import junit.framework.Test;
     5 
     6 /**
     7  * 自动化测试
     8  *
     9  */
    10 public class TestAll extends TestCase {
    11     public static Test suite(){
    12         TestSuite suite=new TestSuite();
    13         suite.addTestSuite(CalculatorTest.class);
    14         suite.addTestSuite(LargestTest.class);
    15         suite.addTestSuite(DeleteAllTest.class);
    16         //重复执行CalculatorTest里的testAdd方法的次数
    17         suite.addTest(new RepeatedTest(new CalculatorTest("testAdd"), 20));
    18         return suite;
    19     }
    20 }
  • 相关阅读:
    在线学习VIM
    对三叉搜索树的理解
    Suffix Tree
    Skip list
    中文分词算法
    土豆的seo
    Gentle.NET文档(链接)
    a标签的link、visited、hover、active的顺序
    html dl dt dd标签元素语法结构与使用
    WEBZIP为什么打不开网页
  • 原文地址:https://www.cnblogs.com/oldcownotGiveup/p/5373654.html
Copyright © 2011-2022 走看看