What is Unit Test?

Tram Ho

Concept

Unit Tests are a type of software testing in which individual units or components of the software are tested. Unit testing is performed during application development. The goal of Unit Testing is to isolate a piece of code and verify its accuracy.

Each UT will send a message and check if the answer is correct, including:

  • The desired results returned
  • The desired exception error

UT code snippets operate continuously or periodically to detect and detect technical errors during development, so UT is also known as automated testing techniques. UT has the following characteristics:

  • Acting as the first users of the system.
  • Valid only when they can detect potential problems or technical errors.

Is Unit Test necessary?

The obvious answer is a need for a programmer responsible for his own code.

1 – Grow faster. Once developers write test cases, debugging will take less time and will then be more confident about making code changes. Skills in all aspects will develop faster than normal Programmers.

The carefulness and responsibility in unit tests also affirms them outside of their lives.

2 – Better Code Structure. When developers write unit tests, their emphasis is on thinking about how their code will be used across the system, which often leads to better design.

And there are many other benefits such as reducing the work for the tester, reducing the cost of code, helping reduce costs for future maintenance …

Unit Test has drawbacks?

Although the benefits of Unit Test are beginning to be more widely understood, there are still a number of reasons why it is not being fully applied, which is why its potential is not being realized.

1 – There is no time for Unit Test. Writing Unit Tests is time consuming which is why it is so difficult to meet deadlines. In fact, Unit Test can save a lot of time and development effort in the long run.

2 – Unit tests are different from writing code Yes, you think that writing a unit test sometimes takes longer than writing a code function. And there may be programmers who can write code but not necessarily write test cases. There is no guarantee, even if the code is thoroughly tested, that there will be no errors.

Unit Test life cycle

Unit Test has 3 basic states:

  • Fail (error state)
  • Ignore (suspend execution)
  • Pass (working status)
  • The entire UT is operated in a separate system. There are many PMs that support UT implementation with an intuitive interface. Typically, the state of UT is represented by different colors: blue (pass), yellow (ignore) and red (fail).

UT is only really effective when:

  • Operated repeatedly
  • Fully automatic
  • Independent from other UTs.

Unit test design

Each UT is designed in the following order:

  • Establish the necessary conditions: initialize objects, identify necessary resources, build fake data …
  • Summon methods to test.
  • Check the correct functioning of the method.
  • Clean up resources after finishing testing.

Unit test application

  • Tests every smallest unit of attributes, events, procedures, and functions.
  • Examine the state and constraints of the object at deeper levels that are normally not accessible.
  • Test processes and extend rather than workflows (set of multiple processes)

How to code effectively with Unit Test

Analysis of possible situations for the code Don’t ignore the worst possible scenarios, for example, the data entered as a database connection fails, the application hangs because of a zero division operation, the procedures give an exception error. Mistakes can mysteriously ruin an application …

Every UT must start with a “fail” state and change the “pass” state after some reasonable changes to the master code.

Whenever writing an important piece of code, write the corresponding UT until you can think of no more situations.

Enter a sufficiently large number of input values ​​to detect code weaknesses in principle:

  • If a valid input value is entered, the returned result must also be valid
  • If the input value is invalid, the result returned must be invalid
  • Identify the unstable and high-risk codes early on, and write the corresponding UT code to control.

For each business object or data access object, a separate test class should be created because serious errors can arise from these objects.

To prevent errors that may arise back automatically executing all UTs when there is an important change, do this job every day. The error UT tells us which change is the cause of the error.

To increase efficiency and reduce the risk of writing UTs, a variety of test methods are needed. Write as simple as possible.

Finally, writing UT requires the same effort, experience, and creativity as writing PM.

Before I finish this section, I have the advice that writing UT is similar to writing a program, so all you need to do is keep practicing. Remember that UT only really benefits if we put software quality first and foremost to end the work on time. Once you are familiar with writing UT, you can read more about the more complex UT construction techniques, including a virtual object model that will be covered in the next section.

Refer to the article here

Share the news now

Source : Viblo