zoukankan      html  css  js  c++  java
  • Junit学习

      1 Junit——用于编写运行可重复发自动化测试的开源测试框架
      2 这里用Eclipse IDE
      3 其实不管是Eclipse还是Intellij IDEA都是差不多的
      4 
      5 使用步骤:
      6 1.new a project,then 
      7 Build Path
      8 Add Library
      9 Junit 4
     10 
     11 2.编写好将要测试的类及方法
     12 3.创建另一个类,用于测试(一般都需要建一个和src并行的文件夹,因为不要把测试文件和源文件放在一起)
     13 
     14 测试类注解介绍
     15 @Test+方法:
     16 指示一个测试用例
     17 
     18 @Before+方法:
     19 表示该方法必须在每个测试之前运行,一遍测试某些先决条件(注意:每个测试用例之前)
     20 
     21 @BeforeClass+方法:
     22 指示附着的静态方法,在所有的测试之前必须运行一次,发生这种情况时一般是测试计算共享配置方法如连接到数据库(注意:是整个类执行之后)
     23 
     24 @After+方法:
     25 测试后执行的内容,比如重置某些必要的变量、删除某些变量等必要操作。(注意:每个测试用例之后)
     26 
     27 @AfterClass+方法:
     28 在所有测试类执行完之后执行,此方法为静态(注意:是整个类执行之后)
     29 
     30 @Ignore+方法:
     31 不执行该方法
     32 
     33 注意:必须要将@BeforeClass、@AfterClass声明为静态
     34 
     35 
     36 需要导入的包:
     37 org.junit.*
     38 static org.junit.Assert.*; //静态的
     39 
     40 
     41 断言
     42 1.void assertEquals(String 可选message,expectedValue,actualValue)
     43 2.void assertTrue(String 可选message,boolean value)
     44   void assertFalse(String 可选message,boolean value)
     45 3.void assertNotNull(String 可选message,Object o)
     46   void assertNull(String 可选message,Object o)
     47 4.void assertSame(String 可选message,Object expect,Object actual)
     48   void assertNotSame(String 可选message,Object expect,Object actual)
     49 5.void assertArrayEquals(String mes,expectedArr, actualArr)
     50 
     51 
     52 
     53 套件测试
     54 ——捆绑几个单元测试用例并且一起执行它们
     55 使用注解:
     56 @RunWith  
     57 @Suite(中文:一套)
     58 
     59 使用:
     60 1.建立需要捆绑测试的所有测试用例(多个不同的测试类):Testi(i=1,2,3...)
     61 2.建立新类:TestSuite:
     62 import org.junit.runner.RunWith;
     63 import org.junit.runner.Suite;
     64 @RunWith(Suite.class)
     65 @Suite.SuiteClass({Test1.class,Test2.class,,,})
     66  //使用{},将需要捆绑的类.class用逗号分隔,形成数组。
     67  //注意:上面两条注解是在主类之前的,未在其中。因为它们注释的是类,而不是方法
     68 public class Main{
     69 //something
     70 }
     71 
     72 package Junit;
     73 
     74 import org.junit.runner.RunWith;
     75 import org.junit.runners.Suite;
     76 
     77 @RunWith(Suite.class)
     78 @Suite.SuiteClasses({
     79         FuncTest.class,
     80         FuncTest2.class,
     81         AtExplaination.class
     82 })
     83 
     84 public class SuiteTest {
     85 }
     86 
     87 
     88 
     89 时间测试
     90 如果一个测试用例在指定的毫秒级别时间里为执行完,我们可以人为认定它为失败
     91 ————timeout参数、@Test,
     92 即@Test(timeout=n)
     93 
     94 package Junit;
     95 import org.junit.Test;
     96 
     97 public class Timeout {
     98     @Test(timeout = 100)
     99     public void timeTest(){
    100         while (true){
    101 
    102         }
    103     }
    104 }
    105 
    106 
    107 
    108 异常测试——@Test(expected = AClass.class)
    109 eg:@Test(expected=ArithmeticException.class)
    110 
    111 
    112 
    113 参数化测试
    114 将一系列参数用于一个测试类的测试
    115 1.该类注解为@RunWith(Parameterized.class)
    116 2.该类有一个构造方法,用于存储测试数据
    117 3.该类有一个静态方法生成并返回测试数据,必须有注解@Parameters (注意:必须是静态)
    118 4.有一个测试用例@Test
    119 
    120 package Junit;
    121 
    122 import org.junit.Test;
    123 import org.junit.runner.RunWith;
    124 import org.junit.runners.Parameterized;
    125 import static org.junit.Assert.*;
    126 import java.util.Arrays;
    127 import java.util.Collection;
    128 
    129 @RunWith(Parameterized.class)
    130 public class ParameterizedTest {
    131     int expected;
    132     int first;
    133     int second;
    134     public ParameterizedTest(int expected,int first,int second){
    135         this.expected =expected;
    136         this.first =first;
    137         this.second = second;
    138     }
    139 
    140     @Parameterized.Parameters
    141     public static Collection add(){
    142         return Arrays.asList(new Integer[][]{{5,2,3},{9,4,5},{8,3,5},{1,2,1}});//为什么用int不行
    143     }
    144     @Test
    145     public void sum(){
    146         assertEquals(expected,add(first,second));
    147     }
    148 
    149     public static int add(int a,int b){
    150         return a+b;
    151     }
    152 
    153 }
    154 
    155 
    156 测试规则。。。

  • 相关阅读:
    【卷影副本】文件属性“以前的版本”中无法看到历史文件的解决方案
    合理的网间结算和互联网转接服务,电信敢正视吗?
    FTP无法连接可能是安全狗设置的原因
    播放器播放视频画面均变暗(但网页视频正常)的解决方案
    安装国际版firefox(火狐浏览器)并设置语言为中文
    navicat for mysql注册码:NAVN-LNXG-XHHX-5NOO
    "COM Surrogate 已停止工作"解决方案(windows7 64位及32位)
    IIS7 http自动跳转到https(通过编辑Web.config实现)
    sql 时间处理
    制作时间戳和时间戳转标准日期时间等
  • 原文地址:https://www.cnblogs.com/XT-xutao/p/10260570.html
Copyright © 2011-2022 走看看