Test === JUnit unit test

One, test classification

Test detailed classification:

  1. White box testing specific running code, combined with functions.
  2. Black box testing is used by testers. From the interface, to see if the function meets the requirements, you need to write a document and record every detail.
  3. Cross-testing, developers test each other's functions when developing.

Function test:
1. Including white box and black box
2. Prepare test data
3. Multi-environment test: (test environment (LAN data), pre-release environment (external network data), formal environment)

  1. Same operating system, same version, same software environment (running environment, code, jdk, tomcat, mysql...)
  2. The database data is different (the data of the pre-release environment is generally the data of the latest official environment)

Automated testing
Use tools to test products.

Performance test (jmeter), stress test,
response speed, mainly to simulate high concurrency scenarios

Write a test report.
Each specific business process, screenshots, specific time, where there are bugs, which errors, and which failures are recorded!

The bug tracking system is
used to record and track bugs, the number of current bugs, how long it takes for the current bug to be resolved, etc... It is a set of such a system, either by a third party or by the company itself.

Statistic, analyze and solve the bug data.

Second, unit test Junit, do you think that junit is just @Test annotation, shallow...

advantage:

Advantages: junit includes junit case and junit suite. Able to test multiple methods or multiple unit test classes at one time and set the expected results. The result of running is to test how many methods have been run, which errors and which failures have been run.

specification:

1. The test method must be decorated with @Test
2. The test method must be decorated with public void without any parameters
3. Create a new source code directory to store the test code
4. The package of the test class should be the same as the tested class Keep consistent
5. Each method in the test unit must be tested independently, and there can be no dependencies between the test methods.
6. The test class uses Test as the suffix of the class
7. The test method uses test as the prefix of the method name

Affirmation:

The test result is compared with the expected result of the setting.

	
断言
//查看两个数组是否相等。
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()	让测试失败

Case demo:

junit test case test class creation, test execution, result feedback

Use eclipse, java8 here

  1. Create a new ordinary Java ee project and import the junit test package, build path
  2. Import



Start writing demo, sum, division

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;
	}

}

Create a junit test class, the package name is the same as that under src, and the class name is on the original class name + Test



. The next step

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);
		
	}

}

Right-click to run the test

console

junit test suite doll test, suite suite, suite suite case

This can test more methods at once.


structure

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 {
    
    

}

Run AllTests
Insert picture description here

When the parameters and results conflict, test a specific method

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));
	}
	
	

}

Guess you like

Origin blog.csdn.net/qq_44783283/article/details/109682041