Unit testing is an important concept and practice in software development. As a term, unit tests are pieces of code that test specific functionality in a certain software component. Usually, they are not written by QA engineers but from the developers that wrote the code. Unit tests are part of the product source code and aim to improve the code quality, reliability, and maintainability.
Unit testing frameworks simplify, structure, and organize the unit testing process. It executes the test and generates reports. The developers organize tests in a hierarchy using classes and functions. After that is done, they assert the execution result and exit all conditions for correctness. Examples of unit testing frameworks are: JUnit for Java and Mocha for JavaScript.
The Unit testing techniques are mainly categorized into three parts which are Black box testing that involves testing of user interface along with input and output, White box testing that involves testing the functional behaviour of the software application and Gray box testing that is used to execute test suites, test methods, test cases and performing risk analysis.
Unit test developers usually follow the “AAA pattern“. It stands for:
- arrange the input data and entrance conditions
- act – execute the function for testing
- assert whether the results and the exit conditions are as expected
In the arrange section, you have the code required for setting up the specific test. Here is where we create objects and potentially set expectations. Then there is the act, which should be the invocation of the method being tested. In the assert, you would check whether the expectations are met.
By implementing unit testing, it’s easier to catch and fix issues early before they cost extra time and money. It can become an important part of the overall software development process, helping to foster development more efficiently and productively. We should aim to write unit tests that focus solely on a single unit of code. With having more test reports, it will be easy to find the error in our code.
Lesson Topics
- Unit Testing
- Unit Testing Framework