• Memo


    一.新建测试类注意点

    1.测试方法必须用@Test修饰

    2.测试方法必须用public void修饰不能带任何参数

    3.新建一个源代码目录来存放测试代码

    4.测试类包名和被测试类包名保持一致

    5.测试单元中的每个方法必须可以独立测试,测试方法间不能有任何依赖

    6.测试类使用使用Test作为类名的后缀(非必须)

    7.测试方法使用test作为方法名的前缀(非必须)

    二.Failure和error

    1.Failure表示测试点输出的结果和预期不一样

    2.error是由代码本声的bug引起的

    3.测试用例不是证明你是对的而是证明你没有错

    三.运行流程

    1.@BeforeClass修饰的方法会在所有方法被调用前被指执行,该方法是静态的,所以当测试类被加载后接着就会运行它,而且在内存中它只会存在一个实例,它比较适合加载配置文件。

    2.@AfterClass所修饰的方法通常用来对资源清理,比如关闭数据库

    3.@Before和@After会在每个测试方法前后各执行一次

    四.注解

     1.@Test把一个普通方法变成测试方法

      @Test(expected=XX.class)例:@Test(expected=ArithmeticException.class)

      @Test(timeout=毫秒)

    2.@BeforeClass,在所有的方法运行前被执行,static 修饰

    3.@AfterClass,在所有的方法结束后b被执行,static 修饰

    4.@Before和@After会在每个测试方法前后各执行一次

    5.@Ignore修饰的方法会被测试忽略

    五.测试套件

     1 @RunWith(Suite.class)
     2 @Suite.SuiteClasses({CalculateTest.class,Calculate2.class})
     3 public class SuitTest {
     4  /**
     5   * 测试套件就是组织测试类一起运行的
     6   * 写一个作为测试套件的入口类,这个类里不包含其他方法
     7   * 更改测试运行器Suite.class
     8   * 将要测试的类作为数组传入到@Suite.SuiteClasses({})中
     9   */
    10     
    11 
    12 }

    六.JUnit参数化设置

     1 package xom.imooc;
     2 
     3 import static org.junit.Assert.*;
     4 
     5 import java.util.Arrays;
     6 import java.util.Collection;
     7 
     8 import org.junit.Test;
     9 import org.junit.runner.RunWith;
    10 import org.junit.runners.Parameterized;
    11 import org.junit.runners.Parameterized.Parameters;
    /**
    1.更改默认的测试运行器为 @RunWith(Parameterized.class)
    2.声明变量存放预期值和结果值
    3.声明一个返回值为Collection的公共静态方法,并用 @Parameters修饰
    4.为测试类声明一个带有参数的公共构造函数,并在其中为其声明变量赋值

    **/
    12 @RunWith(Parameterized.class) 13 public class ParameterTest { 14 15 int expected=0; 16 int input1=0; 17 int input2=0; 18 19 @Parameters 20 public static Collection<Object[]>t(){ 21 22 return Arrays.asList(new Object[][]{ 23 {3,1,2}, 24 {4,2,2} 25 }); 26 } 27 28 public ParameterTest(int expected,int input1,int input2){ 29 this.expected=expected; 30 this.input1=input1; 31 this.input2=input2; 32 } 33 34 @Test 35 public void testAdd(){ 36 assertEquals(expected, new Calculate().add(input1, input2)); 37 } 38 }

      

  • 相关阅读:
    为什么很多程序员都选择跳槽?
    程序员牛人跳槽
    批处理学习教程
    linux操作命令
    apache配置访问限制
    不常见使用的css
    input中的内容改变时触发的事件
    order by 特殊排序技巧
    CSS设置input placeholder文本的样式
    GoodUI:页面布局的技巧和设计理念
  • 原文地址:https://www.cnblogs.com/yanweichen/p/8681027.html
Copyright © 2020-2023  润新知