zoukankan      html  css  js  c++  java
  • 软件测试——JUnit基础

    软件测试——JUnit基础

    1. 综述

      之前(很久了…)说过JUnit的安装和使用,但其实没有讲JUnit的写法,今天写写JUnit的基础。

      博客链接:在Eclipse中使用JUnit4进行单元测试(初级篇)

      博客链接:在Eclipse中使用JUnit4进行单元测试(中级篇)

      博客链接:在Eclipse中使用JUnit4进行单元测试(高级篇)

     

    2. JUnit基础

      一段简单的代码

     1 package testing;
     2 
     3 import static org.junit.Assert.*;
     4 import org.junit.BeforeClass;
     5 import org.junit.Test;
     6 
     7 public class TestSimple {
     8     static Simple simple;
     9     
    10     @BeforeClass
    11     public static void setup() {
    12         simple = new Simple();
    13     }
    14 
    15     @Test
    16     public void testAdd() {
    17         int sum = simple.add(1, 2);
    18         assertEquals(sum, 3);
    19     }
    20     
    21     @Test
    22     public void testSub() {
    23         int sub = simple.sub(3, 2);
    24         assertEquals(sub, 1);
    25     }
    26 }

    2.1 需要导入的包

      首先是需要导入的包,都在org.junit里,代码中需要import的有

    import static org.junit.Assert.*;
    import org.junit.BeforeClass;
    import org.junit.Test;

      import static org.junit.Assert.*;        静态导入,含义是导入Assert类中所有的静态方法,如十分常用的assertEquals()

      import org.junit.BeforeClass;            导入使用@BeforeClass标注所需要的类

      import org.junit.Test;                       导入使用@Test标注所需要的类

      应该是每个标注的使用都要先导入对应类,这可由Eclipse自动补上,不过心中要知晓。

     

    下文详情可参考链接博客的高级篇。

    2.2 标注

      标注是Java的一个特性。

      常用标注代表的含义:

      @BeforeClass            在所有测试函数前运行,且一共只运行一次。总的设置工作

      @Before                在各测试函数前都运行一次

      @Test                       代表这是一个测试函数

      @After                      在各测试函数后都运行一次

      @AfterClass           在所有测试函数后运行,且一共只运行一次。总的结尾工作

      只能有一个函数被标记成@BeforeClass@AfterClass,且其必须为public static

      @Test(timeout = 1000)            限时1000ms的测试,超时则认为没有通过测试,可用来检测死循环

      @Test(expected =ArithmeticException.class)            预期捕获某种异常

      以上这些标注都放在函数前。

    2.2.1 运行器Runner

      @RunWith(xxxxxx.class)          放在整个测试类前,用来选择一个运行器,不加这一句会运行默认的运行器。不同的运行器有不同的功能,下面介绍。

    2.2.3 参数化

      这是一个很重要的特性,避免了重复的操作。

      通过以下方式实现:

      1. 在类前加标注,使用参数化运行器:@RunWith(Parameterized.class)

      2. 使用标注@Parameters,标示参数传入函数

      3. 构建构造函数,将参数传入成员变量

      4. 在测试函数中使用由传入参数赋了值的成员变量

    package testing;
    
    import static org.junit.Assert.*;
    
    import java.util.Arrays;
    import java.util.Collection;
    
    import org.junit.Test;
    import org.junit.runner.RunWith;
    import org.junit.runners.Parameterized;
    import org.junit.runners.Parameterized.Parameters;
    
    @RunWith(Parameterized.class)
    public class TestSimple {
        static Simple simple = new Simple();
        int a, b, sum;    
    
        @Parameters
        public static Collection data(){
            return Arrays.asList(new Object[][]{
                {1, 1, 2},
                {2, 1, 3},
                {5, 6, 11}
            });
        }
        
        // constructor, initialize the parameters
        public TestSimple(int a, int b, int sum) {
            this.a = a;
            this.b = b;
            this.sum = sum;
        }    
        
        @Test
        public void testAdd() {
            assertEquals(sum, simple.add(a, b));
        }
    }
  • 相关阅读:
    cf C. Vasya and Robot
    zoj 3805 Machine
    cf B. Vasya and Public Transport
    cf D. Queue
    cf C. Find Maximum
    cf B. Two Heaps
    cf C. Jeff and Rounding
    cf B. Jeff and Periods
    cf A. Jeff and Digits
    I Think I Need a Houseboat
  • 原文地址:https://www.cnblogs.com/yongheng20/p/5879792.html
Copyright © 2011-2022 走看看