Introduce overview of Unit Test

Tram Ho

What will you learn in this article:

  • Introduction, think about Unit Test
  • Benefits of Writing Unit Test
  • Learn about Test cases
  • Common terms used in Unit Test

Introduction, think about Unit Test

Unit Test is a technique that tests the smallest elements or units of code (usually functions or methods). This is one of the simplest testing levels and can start early in the software development lifecycle. Even in the world, some companies are currently applying software development models that allow to write Unit Test before writing Code.

For many programmers, regardless of the programming language, whether novice or advanced, unit testing is one of the indispensable skills when working. If you have never heard of or have not had the conditions to practice, then take the first steps through this article!

Benefits of Writing Unit Test

  • Increased flexibility in the development process
  • Improve code quality and code architecture
  • Early detection of errors in development, minimizing costs
  • Minimize risk when there is a change, new addition or integration of features

Learn about Test cases

Test case

are test cases with specified input and output. A test case usually has the following two components:

  • Expected value: The value we expect the block to return
  • Actual value: The actual value returned by the block After executing the block under test, we will get actual value. Compare that value with expected value. If the two values ​​match, the result of the test case is PASS. In contrast, the result is FAIL.

Test case classification

  • Positive test cases: These are test cases that ensure the user can perform manipulations with valid data.
  • Negative test cases: These are test cases that try to crash the application using invalid data.

Let’s clarify the above types of test cases through a simple example as follows. Suppose, we are designing a hotel reservation application and there is a requirement: The system allows customers to make a new reservation at a specified time.With the above requirement, we have several cases. Need to test as follows:

The positive case is to ensure that you can add a room with valid data such as room code to book, valid time, valid customer code, price calculated with the number of days booked, …

And the negative cases will try to make a reservation with invalid data such as:

  • New booking without room code
  • New booking with an invalid period of time (past stay period)
  • New booking with customer code does not exist in the database
  • New booking at negative price (less than 0).
  • … and many other cases Hopefully through the above example, you can classify test cases and self-identify test cases for software requirements that you are implementing.

Structure of a test case

The code structure that we should follow in a test case is the AAA structure. This structure consists of 3 components:

  • Arrange – Prepare input data and other conditions to execute test cases.
  • Act – Performs the method / function call with the prepared input at Arrange and gets the actual result.
  • Assert – Compare the expected value and the actual value received in Act step.

The result of the test case will be one of two states:

  • PASS: if expected results and actual results match
  • FAIL: if the expected result is different from the actual result

Sometimes you will come across some articles that use the word Given-When-Then structure. In essence, the same AAA structure as above.

Common terms used in Unit Test

Some essential terms and concepts that need to be understood in UnitTest are as follows:

  • Application (or Code) Under Test Application Under Test (AUT) is the term commonly used to refer to the system / application being tested. With unit tests, our test units are the smallest components in the system so we can use more suitable terms like Code Under Test (CUT).
  • Mock and Stub These are the external components that are emulated or emulated in the context of the test operation. Normally, in order for AUT to function properly, external components such as Web Service, Database, etc. are needed. At the unit test level, we need to separate these dependencies in order to be able to easily implement them. test case test. This section will be explained more in the section Using Mockito (Mocking framework).

Note: In addition to the term mock and stub, you will occasionally come across other words like Spy and Fake.

  • Fixtures are elements that are repeated through each test case and can share common operations between test cases. Example: configuring or preparing data before the test is executed, and cleaning up memory after completion. The fixed component shall be placed on top of the test suite.

There are four main types of fixing components:

Setup Is the component that is executed before the test case executes. In some xUnit libraries (a tool for writing and executing unit tests) we often come across a method / function, or annotion, called BeforeEach. This component is Setup.

One-Time Setup Is the component to be executed first (before both setup and test case are executed). In some xUnit libraries (the tool for writing and executing unit tests) we often come across methods / functions, or annotion named BeforeAll. This component is the One-Time Setup.

Teardown The component to be executed after the test case is executed. In some xUnit libraries (a tool for writing and executing unit tests) we often come across methods / functions, or annotations, called AfterEach. This ingredient is Teardown.

One-Time Teardown The component that was executed last (after all test cases and teardowns have been executed). In some xUnit libraries (a tool for writing and executing unit test) we often come across methods / functions, or annotion named AfterAll. This component is the One-Time Teardown.

Do not worry if you still do not understand the terms just mentioned, they will be clarified in the next articles of this Series.

In the next article, we will continue to learn about commonly used frameworks in Unit Test and especially the XUnit framework. In this article we have got an overview of Unit Test, Testcase as well as some common terms used in Unit Test.

Share the news now

Source : Viblo