junit learning (six) - test suite: let multiple test classes run together

When there are few test classes, it can be executed slowly one by one, but when there are many test classes, it becomes troublesome to execute one by one. Here, it is necessary to write a test suite to allow multiple test classes to run together.

 

A test suite is used to organize test classes to run together. The basic steps are as follows :

1. Write an entry class as a test suite, which is decorated with public and does not contain other methods (such as: public class SuiteTest{})

2. Add an annotation to the class name: @RunWith(Suite.class) to change the test runner

3. Pass the class to be tested as an array into @Suite.SuiteClasses({}) (eg: @Suite.SuiteClasses({TaskTest1.class, TaskTest2.class, TaskTest3.class}))

Examples are as follows:

package com.wjl.junit;

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

/**
 * Junit_demo_6
 * Test suite: let multiple test classes run together
 * **/
@RunWith(Suite.class)//Modify the test runner and modify SuiteTest to the entry class of the test suite
@Suite.SuiteClasses({TaskTest1.class,TaskTest2.class,TaskTest3.class})
public class SuiteTest {
	/**
	 * No code can be written in this class
         **/
}



package com.wjl.junit;

import org.junit.Test;

/**
 * Junit_demo_7
 * Example 1 used in the test suite
 * **/
public class TaskTest1 {
	@Test
	public void test(){
		System.out.println("TaskTest1......test");
	}
	
	@Test
	public void test2(){
		System.out.println("TaskTest1......test2");
	}
}


package com.wjl.junit;

import org.junit.Test;

/**
 * Junit_demo_8
 * Example 2 used in the test suite
 * **/
public class TaskTest2 {
	@Test
	public void test(){
		System.out.println("TaskTest2......test");
	}
	
	@Test
	public void test2(){
		System.out.println("TaskTest2......test2");
	}
}



package com.wjl.junit;

import org.junit.Test;

/**
 * Junit_demo_9
 * Example 3 used in the test suite
 * **/
public class TaskTest3 {
	@Test
	public void test(){
		System.out.println("TaskTest3......test");
	}
	
	@Test
	public void test2(){
		System.out.println("TaskTest3......test2");
	}
}

 

Run SuiteTest, the results are as follows:

Note: No code can be written in this entry class.

Is it convenient?

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=326356395&siteId=291194637