Talking about those things about unit testing

Part 01 What is unit testing

Unit testing is a software testing method used to test the smallest testable unit of a software system, such as the behavior of a function, method, or class. Unit tests are usually written by developers and executed when the code is written. This guarantees real-time detection of errors, defects, and potential problems in the code, ensuring that the code meets expected behavior and output.

Unit testing can be divided into the following steps, and then you can continuously write, execute, analyze test cases, and fix problems during development.

Identify test goals: Before writing unit tests, developers need to be clear about the test goals and expected results. This helps ensure the accuracy and completeness of the test.

Writing test cases: Test cases are the core of unit testing. Test cases should cover various situations and conditions of the code and examine its behavior and output.

Execution of test cases: Test cases can be executed manually or through an automated testing framework. An automated testing framework can help developers execute test cases more quickly and efficiently, and automatically report test results.

Analyzing test results: Analyzing test results can help developers better understand the behavior and output of the code, find problems and improve the code.

Fix the problem: After discovering the problem, developers need to fix the problem in time to ensure the quality and stability of the code. After fixing a problem, the test cases need to be re-run to ensure that the problem has been resolved and no new problems have been introduced.

Part 02 The role of unit testing

Ensuring code quality: Unit testing helps developers detect bugs, defects, and potential problems in the code. By finding and fixing these problems in a timely manner, the quality and stability of the code can be guaranteed.

Improves code maintainability: Unit testing helps developers better understand the code, its behavior and expected output, which makes the code easier to maintain and modify.

Improve development efficiency: Through early detection and resolution of problems, later debugging time and resource costs can be reduced, and development efficiency can be improved.

Facilitates teamwork: Unit testing can serve as a communication and collaboration tool for development teams. Team members can share code and test results, and solve problems together.

Improve design and architecture: Unit testing can promote better design and architecture practices. By writing testable code and test cases, it can help developers better understand the components of the system and facilitate the optimization of design and architecture.

Part 03  Recommended unit test scheme in Java projects

- Junit5

JUnit is the most popular unit testing framework in the Java field. Junit testing, also known as white box testing , aims to verify how (How) and what (What) functions the software under test completes. The latest version of Junit, Junit 5, integrates Junit Platform, Junit Jupiter, Junit Vintage, etc. Among them, Junit Platform is the basis for starting the test framework on the JVM; Junit Jupiter provides a new programming model, including a test engine, running on the Junit Platform; Junit Vintage provides a test engine compatible with JUnit4.x and Junit3.x , to help old projects rely on excessive upgrades of packages. Default integration in Springboot2.2.0+:

<dependency>
      <groupId>org.openjdk.jmh</groupId>
      <artifactId>jmh-core</artifactId>
      <version>1.23</version>
  </dependency>
  <dependency>
      <groupId>org.openjdk.jmh</groupId>
      <artifactId>jmh-generator-annprocess</artifactId>
      <version>1.23</version>
  </dependency>

Commonly used notes and instructions are as follows:

@test  : Indicates that the method is a test method (that is, the framework operation object). Unlike JUnit4's @Test, JUnit5's @Test is very single and cannot declare any attributes. The extended tests are provided by Jupiter

@DisplayName : Set the displayed name for the test class or test method

@BeforeAll : means to execute before all unit test methods

@AfterAll : means to execute after all unit test methods

@BeforeEach: Indicates that it is executed before each unit test method

@AfterEach: means to execute after each unit test method

@timeout  : Indicates that the test method will throw a TimeoutException if it runs for more than the specified time

@Disabled: Indicates that the test class or test method is not executed, similar to @Ignore in JUnit4

@RepeatedTest : Indicates the number of times the method needs to be repeated

@ExtendWith : Provides @Autowired IOC injection for test classes or test methods

- JMH

JMH (Java Microbenchmark Harness) is a tool suite for code micro-benchmark testing in the java field. It is mainly based on method-level benchmark testing, and the accuracy can reach nanosecond level. It is developed by the Java virtual machine team. When you locate a hotspot method and want to further optimize the performance of the method, you can use JMH to quantitatively analyze the optimization results

The springboot integration method imports dependent packages as follows (latest version 1.36):

<dependency>
      <groupId>org.openjdk.jmh</groupId>
      <artifactId>jmh-core</artifactId>
      <version>1.23</version>
  </dependency>
  <dependency>
      <groupId>org.openjdk.jmh</groupId>
      <artifactId>jmh-generator-annprocess</artifactId>
      <version>1.23</version>
  </dependency>

The application scenarios of JMH are as follows:

(1) Want to know exactly how long a method needs to execute and the correlation between execution time and input;

(2) Compare the throughput of different implementations of the interface under given conditions;

(3) See what percentage of requests are completed within how long.

The former of the above two solutions is to ensure the correctness of the code operation, and the latter aims to test the performance of the code operation. In the actual project, it can be combined to ensure the quality and stability of the code, help to improve or improve the design and architecture, and write High-quality unit tests need to follow best practices.

Part 04 Summary

The purpose of unit testing is to verify the functionality, performance, and integrity of software development. When software changes, unit testing helps developers determine which parts are affected and how to change the code. It can also help developers understand their code, get feedback from unit tests, and continue to develop software better. The two complementary solutions based on JUnit5 and JMH mentioned in this article can guarantee software delivery results from two perspectives of code function and performance.

Finally, I would like to thank everyone who has read my article carefully. Reciprocity is always necessary. Although it is not a very valuable thing, you can take it away if you need it:

These materials should be the most comprehensive and complete preparation warehouse for [software testing] friends. This warehouse has also accompanied tens of thousands of test engineers through the most difficult journey, and I hope it can help you! Partners can click the small card below to receive   

Guess you like

Origin blog.csdn.net/OKCRoss/article/details/131286806