nocode.autify.com/blog/what-is-unit-testing
Unit testing is vital in software development as it ultimately aims to deliver superior software products by focusing on testing smaller portions of code. This technique allows developers to analyze code, catch bugs earlier, and fix them faster. In this article, we will define the methodology, its misconceptions, frameworks to use for test automation, benefits, best practices, and finally, leave you with an example of how to perform a unit test.
In software testing, unit testing is a method of testing smaller isolated portions (or units) of code. Unit tests are usually conducted with test automation scripts on the smallest testable portion of the software.
The unit is usually a function of the code. It is also a part of “white box testing,” which is a type of testing that can be written by someone who knows the architecture of the program.
This methodology can test code functions, procedures, or methods whether utilizing procedural programming or object-oriented programming. If it relies on or talks to any other systems, then it cannot be qualified as unit testing. The purpose is to ensure that each unit of code functions as expected. This allows for quality assurance to write test cases only for portions of the software that affect the behavior of the system.
Unit testing cannot be performed on every part of the software. Nor can it have dependencies on other systems to be considered of the ilk. In his book “Working Effectively with Legacy Code” Michael Feathers explains why. “If it talks to the database, it talks across the network, it touches the file system, it requires system configuration, or it can’t be run at the same time as any other test.”
Here are a few examples of common misconceptions:
Unit testing is very important as it allows developers to detect bugs earlier in the lifecycle- thus improving the quality of delivered software. Here is a list of great benefits:
In DevOps, the process of continuous integration automatically runs tests against the code every time someone commits new code to the repository. If one test fails, the entire team can receive an email (or alert on Slack) of the break. Then the responsible person can rectify the issue.
There are a number of testing tools developers can use for testing. Here is a list of top frameworks for unit testing:
There are some best practices developers should follow when writing unit tests for future scalability. Here is the list of best practices:
Integration testing is a methodology ensuring that the application or the external integrations of that application are working properly. This test layer tries to emulate real-life environments for testing an application. Whereas, unit tests check the smallest possible portion of code without dependencies. This is an assembly layer, normally reserved for engineers who know how the code works. For example, API testing is considered integration testing, as it calls different modules inside of an API.
In the diagram above, we speak of Functional Testing as well (more on that in a related article.) Functional testing examines the functionality of the code without peering into the internal systems. It is known as “black-box testing.” Whereas the tester does not have to possess detailed knowledge of how the system application works in order to perform testing.
The pyramid above illustrates the quantity of tests in relation to the application. Unit tests should make up the majority of your automated testing, followed by integration and functional testing.
Unit testing examples – testing for power to the circuit board, testing if the dialer app can execute, testing if a SIM card is inserted, etc.
Integration testing example – testing if the SIM card is activated or testing if the device has a mobile data connection.
Functional testing example – testing that a phone can make a call.
To start writing unit tests, you should choose a unit testing framework first. Some are listed here for your reference.
There are mainly two approaches, bottom-up or top-down. Most of the functions call other functions in its code. i.e. function A calls function B and function B calls function C. The top-down approach is writing tests from function A while the bottom-up approach is starting from function C.
When developing, run a unit testing framework in your local machine. When you commit code to your team’s code repository, the test should be executed too. It is wise to set up a CI tool, like Jenkins or CircleCI to continuously run tests.
Here is a simple example of how to write unit tests…
Let’s say we’re trying to implement the sum
function.
It takes two numbers a
and b
as its arguments and returns the number of the total amount.
def sum(a, b):
return a + b
The simplest way to write a unit test is by using the assert
function. This function can be found in most programming languages.
# It should pass
assert sum(1, 2) == 3
# It also should pass
assert sum(1, 2) != 0
If your shop is performing automated tests on a consistent basis, you can see how beneficial unit testing is for catching bugs early on. Without this technique, a defect could make its way farther into the pipeline. Even worse, into production.
This means time and resources are allocated to finding, analyzing, and fixing defects when a simple automated test could have caught them. If your firm seeks a test automation tool to automatically catch bugs and alert developers via Slack, consider using Autify.