zoukankan      html  css  js  c++  java
  • Junit技巧

    测试套件:

    @RunWith(Suite.class)
    @Suite.SuiteClasses({TaskTest1.class, TaskTest2.class, TaskTest3.class})
    public class SuiteTest{
           
    }

    //测试套件就是组织测试类一起运行,这个类里不包含其它方法,将要测试的类作为数组传入到Suite.SuiteClasses修饰符中。

    @Test(timeout=毫秒)  @Test(expected=异常类  xxxException.class)

    @Ignore 所修饰的测试方法会被测试运行器忽略,可以加上注释信息@Ignore("该类无效")

    @RunWith: 可以修改测试运行器 org.junit.runner.Runner

    @BeforeClass: 修饰的方法会在其它的方法运行前被执行,该方法需被static修饰,整个测试类运行期间,只执行一次。

    @AfterClass: 修饰的方法会在其它的方法运行结束后被执行,该方法需被static修饰,整个测试类运行期间,只执行一次。

    @Before:会在每一个测试方法被运行前执行一次,运行期间执行次数等同于测试方法数。

    @After 会在每个测试方法被运行后执行一次,运行期间执行次数等同于测试方法数。


    假设有一个方法计算两数之和,我们一次测试多个用例时,可以使用以下方法:

    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 ParameterTest {
        /*
         * 1.更改默认的测试运行器为RunWith(Parameterized.class)
         * 2.声明变量来存放预期值 和结果值
         * 3.声明一个返回值 为Collection的公共静态方法,并使用@Parameters进行修饰
         * 4.为测试类声明一个带有参数的公共构造函数,并在其中为之声明变量赋值
         */
        int expected =0;
        int input1 = 0;
        int input2 = 0;
        
        @Parameters
        public static Collection<Object[]> t() {
            return Arrays.asList(new Object[][]{
                    {3,1,2},
                    {4,2,2}
            }) ;
        }
        
        public ParameterTest(int expected,int input1,int input2) {
            this.expected = expected;
            this.input1 = input1;
            this.input2 = input2;
        }
        
        @Test
        public void testAdd() {
            assertEquals(expected, new Calculate().add(input1, input2));
        }
    
    }
  • 相关阅读:
    AngularJS Insert Update Delete Using PHP MySQL
    Simple task manager application using AngularJS PHP MySQL
    AngularJS MySQL and Bootstrap Shopping List Tutorial
    Starting out with Node.js and AngularJS
    AngularJS CRUD Example with PHP, MySQL and Material Design
    How to install KVM on Fedora 22
    Fake_AP模式下的Easy-Creds浅析
    河南公务员写古文辞职信
    AI
    政协委员:最大愿望是让小学生步行上学
  • 原文地址:https://www.cnblogs.com/tuifeideyouran/p/5422964.html
Copyright © 2011-2022 走看看