In our latest guide covering the essentials and best practices for software development we are detailing some of the necessary basics that you need to know about unit testing before you get started.
To explain examples, best practices and some of the top queries around unit testing we’ve invited contributors Nelson Ford, Founder and Principal Solutions Architect at Pilotcore Systems & Ryo Chikazawa, CEO of Autify to share their thoughts below.
What is unit testing?
“Unit testing is a type of testing method that defines inputs to the specific unit and expected outputs from the unit then tests if they match.”
“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”, stated Ryo in his response.
Best practises for unit-testing
“It is critical to ensure your team takes the time to write tests for each unit of code they produce. Unit tests are quick to write, and staying up to date on your tests will pay off in terms of delivering code that performs as expected”, Nelson added.
“From a DevOps point of view, you'll want to use a code coverage tool as part of a complete CI/CD pipeline, failing the build if test coverage falls below a strict threshold. Be sure to run the tests themselves in your CI/CD pipeline and fail the build if any tests fail.”
“This way, you can ensure that tests always cover a minimum percentage of your code, and that code failing tests can't reach production.”
In addition to this Doc Norton, author of Escape Velocity: Better Metrics for Agile Teams also added “Another primary consideration I'd add is to write the tests backwards. Every test needs the Arrange, Act, and Assert components.”
“Start writing the test with the assert. This makes it very clear what we want as the outcome of the test. If the assert won't compile, first resolve the compilation issues in as light-weight a manner as possible.”
“Then move to the act and then arrange. This technique helps to ensure that each test has no more setup than necessary and helps to drive out the new behavior in even smaller steps.”
For the next three commonly asked questions around unit testing, Nelson solely contributed his feedback based on over twenty years of experience working in software development and DevOps.
What is the purpose of unit testing?
Testing individual units in the early development stages helps save time and cost in the long run as developers can find defects and fix bugs early in the development process.
Testing of individual units of code is a critical step before integration testing, which tests how the units of code interact with each other.
Thorough unit test coverage is the first step to a complete testing pipeline which ultimately results in fewer bugs in production.
Who is responsible for unit testing?
Software developers are largely responsible for writing and running unit tests, however, independent testers and automated test pipelines as part of continuous integration can also run unit tests.
Ideally though, as each function or method of code is written, a corresponding unit test is also produced and run by the developer to ensure the code works before committing this to a source repository.
What is an example of unit testing?
Consider the case where a function takes two arguments and runs a calculation, returning a result. The simplest case would be returning the sum of two numbers.
A unit test for this function would simply verify that the function returns the correct sum for the two numbers provided in the test. So given the arguments 2 and 9, the value returned by the function should be 11. If that check evaluates to true, the test is passed.
In the case of a function that creates a user in a database, the test might be: given a data structure containing a first name, last name, and email address, the result should be a success code with a corresponding record verified as created in the database.
Ryo solely responded to our last two queries around this topic. Ryo has worked across Japan, Singapore and San Francisco over the last ten years developing software and founding multiple startups including Autify in 2016.
How do you write unit tests?
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.
What is the difference between a unit test vs an integration test?
A unit test, tests a single module while an integration test, tests multiple modules in its integration. For example, API testing is an integration test as it calls different modules inside of the API.
If you feel that we've missed out any other essential queries that you would like to see answered on unit testing then feel free to send your questions to [email protected] to see them answered in a future follow up post.
We are always on the lookout for new software development, logging and data security questions to answer on our blog so feel free to get in touch.
If you enjoyed this article on the best practises for unit testing then why not check out another one of our articles on the meaning of CI/CD or why not read our article on the top Elasticsearch interview questions?