"Wouldn't be more efficient and secure to perform a battery of tests first,
instead of assembling and driving the vehicle to see what happens? "
Inside the paradigm of Software Testing, there are a variety of techniques that are applied for evaluating
all kinds of aspects from a software. Basically, it's a paradigm that defines methods for evaluating functionality,
quality and performance of a software against a myriad of situations that could come across during the path of its
execution, verifying the reliability of its operation and asserting results, against proposed inputs. Besides this,
to write tests, establishes a skeleton of proofs to be performed against a section of code, allowing reusability and
improvement of these tests for Continous Integration or for single test of separate modules (not very efficient
with large applications).
The purpose of this article is to demonstrate the application of Unit Testing concept, which is part of the
Functional Testing category. In a nutshell, Unit Testing is a type of testing focused on evaluating
functionality for specific sections of code. It could be used to evaluate performance at some point
(time consumed during execution of each section of code), but the focus is, to evaluate the functionality
of a code, according to business requirements expressed as inputs against expected outputs for each test.
Let's take as an example this simple module written in Python called calculator.py:
As a good practice, a separate module called calculation_test.py was created, containing tests for the example:
Basically, this module contains a class called Probe which inherits the class TestCase from unittest module
and for each method defined in Probe, a specific method (assertEqual()) from the inherited class (TestCase())
is called, in order to evaluate the functionality of a specific method from Calc, defining specific inputs
for each method and expected outputs.
The output of calculation_test.py is straightforward:
- Probe class method executed
- docstring + test result
- number of executed tests with time elapsed (calulation.py -> time.sleep(5))
- final message
In case of Exceptions, the output is different (of course).
For demonstration purposes, a non-existent variable was added for one of the Calc methods. Such scenario represents the evaluation of functionality of the code:
As another demonstration, the expected value for one of the Calc methods was modified in one of the assertEqual() calls. This scenario represents the evaluation of Business Requirements VS Expected Output, asserting that a required Input, will produce an expected Output:
In summary, Software Testing is a crucial step regarding the guarantee of functionality, performance and all
expected behaviours of a software, depending of which approach is applied, and is directly related with the
efficiency for the bug fixing process, in order to reach the expected results for a product.
We could say that, Unit Testing is the keystone of Software Testing (but also the beginning of the process),
in order to evaluate the behaviour of our code (Business Requirements VS Expected Output) and identifying all
possible bugs that could happen during the routine of developing softwares.