What is Unit Testing
Automation Best practices Agile
8 mins read
April 27, 2024

Unit Testing: everything you need to know

Unit testing is the smallest yet one of the biggest types of software testing at the same time. Letā€™s look at why unit testing is important and what techniques can take it to the next level.

photo
photo
Robert Weingartz
Denis Matusovskiy

The definition of unit testing is simple: you test the smallest piece of your software that can be isolated. It can be as small as a single line of code or as big as a module. This is, however, one of the times where smaller is better even when working on a large-scale project.

Software unit testing is usually done by developers themselves. Indeed, they may not have the time or quality assurance expertise for a proper unit test plan. Devs, however, know their own code better than any QA specialists could understand it. Testing code before it is passed on to the quality assurance people has been a best practice for years.

Advantages of unit testing

The purpose of unit testing canā€™t be narrowed down to just one thing. Having developers go over small units of code has multiple advantages:

  • Improves understanding of the codebase by the developer. This is largely relevant for new additions to the team. No matter how much documentation you give to new devs, practice always makes things easier.
  • Self-sustainable, meaning neither the devs nor QA specialists wait for others to finish their code first
  • Facilitates good regression testing or rather a good follow-up to regression tests. If they reveal a problem, it is much easier to isolate the cause and resolve the defect when small pieces of code are covered by individual test cases
  • Improves the quality of code. Not involving devs in testing is flawed on many levels, and depriving the project of better code is one of them. Hasty rewrites and last-minute debugging contribute to tech debt, making the software prone to crutches over real solutions. Regular testing in small chunks brings greater attention to detail and doesnā€™t let flaws pile up
  • Saves time and money. Itā€™s simple: good automated unit testing means that a lot of issues are spotted without human effort. A one-time investment plus some maintenance considerably free up schedule of devs and testers

Advantages of unit testing

image
3zbdcc601729bfa1d4e33335cfb5176b61c737a68bafd4b4a38a8ef653a7771392
testing strategy template

Get a testing strategy template that enables us to release 2 times faster

Unit testing techniques

The unit testing techniques correspond with general testing techniques.Ā 

  • Black box technique covers software behaviour from usersā€™ point of view: the QA specialist is unaware of how the solution is designed
  • White box technique refers to testing with knowledge of how the software was designed to work
  • Grey box technique is a middle-ground: it tests user-facing functionality like black box testing would but done with the knowledge of the solutionā€™s architecture

As for how unit testing is done, both manual and automated approaches are thriving. Just like with other types of testing, the latter is good only if you know why youā€™re automating QA. The role of developers in unit testing means that thereā€™s a QA workload for non-testers, so increased automation could be more cost-effective than ever.Ā 


Interested in different types of testing?

Read our article on user acceptance testing definition, usage, and challenges


Getting devs invested into unit testing can be challenging. Hereā€™s how Alan Mellor, the author of Java OOP Done Right with 30+ years of software engineering experience, describes the benefits for devs:

A unit test always has three parts: Arrange, Act and Assert.The last two are always one-liners. They also express a piece of design thinking that youā€™ll be familiar with: the API of your code and how it talks with the caller. Unit tests really only get tough to write in the arrange step. This is where you create the graph of objects and initial states needed to run the test. If your code has got into a tangle, this can be really difficult to do. This is your first sign that your design sucks.

Youā€™ve most likely kept bolting pieces together, tinkering about and doing too much in one step. We all do. The answer is to redesign that part of code. Split apart some objects so each one does a smaller piece of work. This reduces the complexity of the Arrange step and of the production code as well. By writing tests earlier, you shorten the feedback cycle. You become aware of the mess sooner. You also start to get an intuition about how to design your code correctly split up in the first place, which also makes it easy to test.

Alan Mellor, author of Java OOP Done Right,

Best practices

Here are some unit testing ideas so you can learn from trial and error done by others.

  • Apply the ā€œGiven/When/Thenā€ protocol to organising unit tests. This approach is also known as Arrange (the test setup), Act (on the unit for the test), Assert (verify the outcome) described by Alan Mellor
  • Keep tests simple. For example, if you try to account for all possible inputs in one test case, you may have very well written a test that largely repeats the code being tested.Ā 
  • Get a good naming convention going. Navigating all your tests can be daunting at times, and itā€™s even more so when itā€™s developers who are making tests as well. Getting on the same page with naming goes a long way

aqua tool for unit testing

Test management systems and Application Lifecycle Management solutions like aqua are not quite used for unit testing. Developers write tests and execute them in IDE (integrated development environment) that is not linked to the test management solutionā€™s server.

On the other hand, a bug reporting tool helps achieve and reinforce many concepts that unit testing strives for. A good example is tracking test coverage. aqua has provided that for years, but we have just visualised it with a neat column on the Requirements screen. There are plenty of other features promoting collaboration between devs and QA, much like what unit testing does.Ā 

aqua tool of unit testing

Conclusion

Unit testing is a great asset for any software project. Getting developers to test their own code early has both short-term and long-term benefits for your company. While test management solutions have little to do with unit testing, they help you cultivate the same Developmentā€“QA collaboration, thoroughness and conciseness that unit testing entails.Ā 

Unit tests save QAā€™s time on dealing with routine errors and small blunders. Artificial intelligence does the same when it comes to creating new tests. You donā€™t need to spend too much time covering the same easy malfunctions from one feature to another.

aquaā€™s AI Copilot gives you the flexibility to save time on routine tests. You can ask it to make entire tests just from a description or fill out test steps based on what you used in other test cases. Originally launched in 2013, aqua comes with a great suite of non-AI test management functionality as well.

Save time on QA routine with AI testing

Try aqua
On this page:
See more
Speed up your releases x2 with aqua
Start for free
step
FAQ
What is unit testing in Agile?

Unit testing in Agile refers to testing individual units or components of software in isolation to ensure they function correctly and meet the specified requirements. In Agile development, unit testing is typically performed by developers as part of the coding process, often using test-driven development (TDD) practices. Unit tests focus on testing the smallest testable parts of the code, such as functions, methods, or classes, without dependencies on external systems or resources. These tests are automated and executed frequently throughout the development cycle, helping to identify bugs early and maintain code quality.

How do you do unit testing with examples?

Unit testing involves writing automated tests to verify the behavior of individual units or components of code in isolation. Here’s a step-by-step example of how to perform unit testing using a simple JavaScript function: Let’s say we have a function add that adds two numbers:

function add(a, b) {

Ā Ā Ā Ā return a + b;

}

Now, we’ll create unit tests for this function using a testing framework like Jest:Ā 

1. Set Up Testing Environment: Install Jest using npm or yarn:

npm install –save-dev jest

2. Write Test Cases: Create a test file, e.g., add.test.js, and write test cases for the add function:

const add = require(‘./add’);

describe(‘add function’, () => {

Ā Ā Ā Ā test(‘adds 1 + 2 to equal 3’, () => {

Ā Ā Ā Ā Ā Ā Ā Ā expect(add(1, 2)).toBe(3);

Ā Ā Ā Ā });

 

Ā Ā Ā Ā test(‘adds -1 + 5 to equal 4’, () => {

Ā Ā Ā Ā Ā Ā Ā Ā expect(add(-1, 5)).toBe(4);

Ā Ā Ā Ā });

 

Ā Ā Ā Ā // Add more test cases as needed

});

3. Run Tests: Run the tests using Jest:

npx jest

Jest will execute the test cases and provide feedback on whether they passed or failed.Ā 

4. Review Results: Check the test results to ensure that the add function behaves as expected under different scenarios. By following these steps, you can perform unit testing for the add function to verify its correctness and ensure that it produces the expected output for various input values.

What is unit testing?

Unit testing is testing of the smallest chunks of code possible before the build is even shipped for regular QA effort. It helps developers catch and immediately solve issues before they even make it to the testers. Unit tests are usually written by devs, which saves time by not involving QA in pre-testing at all.

What is unit testing tool?

There are no prominent tools specifically for unit testing. Such tests are created in the code environment.Ā 

What are the types of unit testing?

Unit testing can be either manual or automated. Manual tests really get developers thinking about their approach to the code, and this is how they both write cleaner code and spot issues. Automated tests are helpful in catching bugs at scale.

Who will perform unit testing?

You want developers to perform unit testing, as the main purpose is to resolve issues before sending code to Quality Assurance. Depending on the developerā€™s expertise and workload, QA specialists can be involved in making unit tests.Ā 

closed icon