测试===JUnit单元测试

一,测试分类

测试细致分类:

  1. 白盒测试 具体的跑代码,结合功能。
  2. 黑盒测试,测试人员用,从界面来,看界面,功能有没达到要求,需要写文档,每个细节部分要记录到。
  3. 交叉测试,开发人员开发时,相互测试对方的功能。

功能测试:
1. 包括白盒,黑盒
2. 准备测试数据
3. 多环境测试:(测试环境(局域网数据),预发布环境(外网数据),正式环境)

  1. 相同操作系统、相同版本、相同的软件环境(运行环境、代码、jdk、tomcat、mysql…)
  2. 数据库数据不同(预发布环境的数据一般取是最近正式环境的数据)

自动化测试
使用工具,来测试产品。

性能测试(jmeter)、压力测试
响应速度、主要是模拟高并发场景

编写测试报告
各个具体业务流程,截图,具体时间,哪些地方有bug,哪些error,哪些failure记录下来!

bug跟踪系统
用来记录并跟踪bug,当前bug的数量,当前bug经过多长时间才被解决掉,等… 是一套这样的系统,有第三方的,也有公司自己研发的。

对bug数据进行统计,分析,解决。

二,单元测试Junit,你以为的junit只是@Test注解吗,shallow…

优点:

优点:junit包括junit case和junit suite。能够一次性的测试多个方法,或者多个单元测试类,并设置预期的结果。运行的结果是测试run了多少个方法,哪些error, 哪些failure了。

规范:

1.测试方法上必须使用@Test进行修饰
2.测试方法必须使用public void进行修饰,不能带任何的参数
3.新建一个源代码目录用来存放测试代码
4.测试类的包应该和被测试类保持一致
5.测试单元中的每个方法必须独立测试,测试方法间不能有任何的依赖
6.测试类使用Test作为类的后缀
7.测试方法使用test作为方法名的前缀

断言:

测试结果与设置预期的结果对比。

	
断言
//查看两个数组是否相等。
assertArrayEquals(expecteds, actuals)
//查看两个对象是否相等。类似于字符串比较使用的equals()方法
assertEquals(expected, actual)
//查看两个对象是否不相等。
assertNotEquals(first, second)
//查看对象是否为空。
assertNull(object)
//查看对象是否不为空。
assertNotNull(object)
//查看两个对象的引用是否相等。类似于使用“==”比较两个对象
assertSame(expected, actual)
//查看两个对象的引用是否不相等。类似于使用“!=”比较两个对象
assertNotSame(unexpected, actual)
//查看运行结果是否为true。
assertTrue(condition)
//查看运行结果是否为false。	
assertFalse(condition)
//查看实际值是否满足指定的条件
assertThat(actual, matcher)
fail()	让测试失败

案例demo:

junit test case测试类创建,执行测试,结果反馈

这里使用eclipse,java8

  1. 新建普通Java ee项目,并导入junit测试包,build path
  2. 导入



开始编写德莫,求和,除法

package cn.bitqian.demo;

/**
 * @author echo lovely
 * @date 2020年11月13日 下午6:55:22
 */

public class MyMath {
    
    
	
	// sum
	public int add(int a, int b) {
    
    
		return a + b;
	}
	
	// divide
	public int division(int a, int b) {
    
    
		
		if (b == 0)
			return b;
		
		return a / b;
	}

}

创建junit测试类,包名和src下面的一样,类名在原类名上+Test



上面的下一步

package cn.bitqian.demo;

// 静态导入类,可直接用里面的静态方法
import static org.junit.Assert.*;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Assert;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;

/**
 * junit test class
 * @author echo lovely
 * @date 2020年11月13日 下午6:59:03
 */

public class MyMathTest {
    
    
	
	private MyMath math = new MyMath();
	
	/**
	 * 1. 测试方法 无参无返
	 * 2. 方法前面加上@Test注解
	 * 
	 * 运行测试方法,选中方法,右键run as Junit test 或者 debug as Junit test
	 * 直接右键,会运行所有有@Test注解的方法
	 * Runs 测试了几个方法
	 * Errors 测试方法有问题
	 * Failures 测试方法没达到预期的要求
	 */
	
	
	// 类加载时执行
	@BeforeClass
	public static void beforeClass() {
    
    
	
		System.out.println("init...");
	}
	
	// 每个有@Test方法执行前执行
	@Before
	public void before() {
    
    
		// 如获取mybatis的session工厂
		System.out.println("method before...");
	}
	

	// 每个方法执行之后执行
	@After
	public void after() {
    
    
		// 如关闭mybatis的session工厂
		System.out.println("method after...");
	}
	
	// 类所有的方法执行完后执行
	@AfterClass
	public static void afterClass() {
    
    
		System.out.println("all is done...");
	}
	

	// 1秒 内必须出结果,否则测试失败
	@Test(timeout=1000)
	public void testAdd() {
    
    
		System.out.println("测试了math类的加法");
		
		// 预测值
		int expected = 2 + 3;
		
		// 实际值
		int actual = math.add(2, 3);
		
		// 断言
		Assert.assertEquals(expected, actual);
		
	}

	@Test
	public void testDivision() {
    
    
		System.out.println("测试了math类的除法");
		
		// Errors
		int expected = 5 / 0;
		
		int actual = math.division(5, 2);
		
		assertEquals(expected, actual);
		
	}

}

右键运行测试

控制台

junit test suite 套娃测试,suite套suite,suite套case

这个能一次性的测试更多方法。


结构

package cn.bitqian.suite;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

import cn.bitqian.demo.MyMathTest;

/**
 * suite 测试套件 套娃
 * 测试套件就是组织测试类一起运行
 * 写一个测试套件的入口类,这个类不包含其他的方法
 * @author echo lovely
 * @date 2020年11月13日 下午7:16:57
 */

// 运行器
@RunWith(Suite.class)
// 哪些测试类要包含, 会运行对应类的内容
@SuiteClasses({
    
    MyMathTest.class})
public class MyMathTestSuite {
    
    

}

package cn.bitqian.suite;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

import cn.bitqian.demo.MyMathTest2;

/**
 * 测试套件2  套测试类 MyMathTest2
 * @author echo lovely
 * @date 2020年11月13日 下午7:16:57
 */

@RunWith(Suite.class)
@SuiteClasses({
    
    MyMathTest2.class})
public class MyMathTestSuite2 {
    
    

}

package cn.bitqian.suite;

import org.junit.runner.RunWith;
import org.junit.runners.Suite;
import org.junit.runners.Suite.SuiteClasses;

/**
 * 总套件,套娃,套两个Suite
 * @author echo lovely
 * @date 2020年11月13日 下午7:24:08
 */

@RunWith(Suite.class)
@SuiteClasses({
    
     MyMathTestSuite.class, MyMathTestSuite2.class })
public class AllTests {
    
    

}

运行 AllTests
在这里插入图片描述

当参数和结果有冲突时,测试具体某个方法

package cn.bitqian.demo;

import java.util.Arrays;
import java.util.Collection;

import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.Parameterized;
import org.junit.runners.Parameterized.Parameters;

/**
 * 测试MyMath类中的add方法
 * @author echo lovely
 * @date 2020年11月13日 下午8:08:17
 */

@RunWith(Parameterized.class)
public class MyMathAddTest {
    
    
	
	// 预期值
	int excepted = 0;
	// 参数1
	int input1 = 0;
	// 参数2
	int input2 = 0;
	
	
	public MyMathAddTest(int excepted, int input1, int input2) {
    
    
		super();
		this.excepted = excepted;
		this.input1 = input1;
		this.input2 = input2;
	}
	
	@Parameters
	public static Collection<Object[]> t(){
    
    
		return Arrays.asList(new Object[][]{
    
    
			{
    
    4,2,2},
			{
    
    11,9,2},
			{
    
    8,6,2},
			{
    
    1,-6,7}
		//   res v1 v2
		});
	}
	
	
	@Test
	public void testAdd(){
    
    
		
		MyMath myMath = new MyMath();
		Assert.assertEquals(this.excepted,myMath.add(this.input1, this.input2));
	}
	
	

}

猜你喜欢

转载自blog.csdn.net/qq_44783283/article/details/109682041