Unit Testing in Agile - Getting the Basics right
Tags: beginners, todayilearned
Published: 2021-11-12 12:37:57

## **What is agile unit testing?**

A unit test is a few lines of code which tests a small part of the product&#39;s source code and checks the results obtained. This process of checking the code is called agile unit testing. The developers in the product team make use of this when developing a product.

Unit tests can have either one of two results and are hence binary in nature. The result would be &#39;pass&#39; if the code acts in accordance with the expectations. If this is not the case, the result would be &#39;fail&#39;.

Usually, developers write a large number of unit tests to validate the various program behaviors. This makes up a &#39;test suite&#39;. The results are identified by color code by common convention. The green color represents that all unit tests in a program have passed. Red shows that one or more unit tests have failed. These color codes have been accepted for quite a few years now, dating back to JUnit at least.

&#39;Test-runners&#39; are a type of software to execute unit tests. The developer provides the following info in unit tests:

- Brief explanation on what they are testing
- The part of software that they want to test (eg. class, function)
- Test data to use as input
- Expected result

Then, the dev executes their code with the input and compares results with the expected one. You can consider this as a &#39;pass&#39; when the results of the execution match the expected results. If not, the test is a &#39;fail&#39;.

In the event that a code depends on external data, it cannot be fully tested. In such cases, the dev replaces the external dependencies with &#39;mock data&#39;. Mock data is static data that is used to restrict the tests to the functionality of the unit.

![](RackMultipart20211112-4-1aub4e4_html_1fdd8db3f6c3da42.jpg)

## **History**

So how did agile unit testing come about? David J Panzl, a well known agile author, wrote a series of articles talking about tools with features very similar to JUnit. This was in 1976, and these articles testify the long history of automated unit tests.

The years 1988-1990 saw a rise in event-driven GUI software. Testing challenges in these paved the way for capture and replay test automation tools. Companies like Segue and Mercury made these, and they went on to dominate the market for about a century.

In 1997, JUnit, the testing tool, was written by Beck and Gamma. It drew inspiration from Beck&#39;s prior work on &#39;SUnit.&#39; JUnit grew massively popular over the next few years, ending the &#39;capture and replay&#39; age.

## **Purpose behind unit tests**

Unit testing is popular as it serves the following use cases and makes the life of the developer easier. All these make unit tests indispensable to the development process.

1. Use of agile unit testing leads to a reduction in the number of defects in the code.
2. Devs obtain free working documentation of the code through unit tests.
3. Ease of finding and fixing bugs throughout the development so the end user has a perfect product.
4. Developers keep in mind the edge cases related to the product automatically. This helps them to identify the best ways of going about things.
5. The code created is more likely to be modular and reusable, with no dependency.
6. Others working on the same code would also have no issues in taking over if needed.
7. Detecting bugs at early stages also saves a lot of money that would have to be spent on fixes later.
8. Even after the customer uses the product, if a bug appears, unit tests can quickly fix the issue.
9. Since unit tests use routine operations, the time consumed in manual efforts to test is saved.

## **How to get started with unit testing in agile?**

There are two main steps to unit testing in agile. One is to write code that is testable. The other, to use suitable software to test this code.

The first step, to write testable code, means that the functions you write to test the code should be clean. This means that they shouldn&#39;t have side effects and should only work on the given parameters. So, there should be a minimum number of parameters as every new parameter makes testing a lot harder. This is because you then have to test all sorts of combinations with these parameters. There should also be no external dependencies. For this, the mock data spoken of above proves handy.

The next is to use the right tool. A number of tools are used for unit testing in web projects. Some examples are:

- Mocha as a test runner in a NodeJS testing environment.
- JSDOM for testing DOM interactions.
- Enzyme when you want to test ReactJS components.

The app you&#39;re developing would decide the unit testing software that you need.

## **Best Practices for Agile Unit Testing**

Some things to remember while making use of agile unit testing are:

Name the test methods such that it makes the need clear and you don&#39;t have to look at other places for info. There are many popular[unit testing naming conventions](https://vitalflux.com/7-popular-unit-test-naming-conventions/) you can make use of for this.

1. Tests should only pass or fail when the code is correct or has an error respectively. This means that if your test passes even when the code is incorrect or vice versa, there&#39;s an error in the code.

2. Try to keep relevant and meaningful failure messages which show the test parameters. The failure message should make it easy for you to correct the error.

3. Design the test code in such a way that it&#39;s got plenty of comments and is easy to read.

4. Focus on only one test at a time to avoid keeping track of multiple scenarios.

5. Run tests frequently to ensure correct code.

Originally published [here](https://buildd.co/product/agile-unit-testing).

P.S. Make sure to check out these blogs, too:

1. [Product Requirements Document](https://buildd.co/product/prd-meaning-product-requirements-document)
2. [Family Branding](https://buildd.co/marketing/family-branding)
3. [Five Ws and One H](https://buildd.co/product/the-five-ws-one-h)
4. [Impact Effort Matrix](https://buildd.co/product/2x2-impact-effort-matrix)
5. [Niche Marketing](https://buildd.co/marketing/concentrated-marketing)
6. [Key Competitive Differentiator](https://buildd.co/marketing/competitive-differentiator)