DEV Community

Cover image for Unit Testing in Agile - Getting the Basics right
Rebecca Ferrao
Rebecca Ferrao

Posted on • Updated on • Originally published at buildd.co

Unit Testing in Agile - Getting the Basics right

What is agile unit testing?

A unit test is a few lines of code which tests a small part of the product'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 'pass' if the code acts in accordance with the expectations. If this is not the case, the result would be 'fail'.

Usually, developers write a large number of unit tests to validate the various program behaviors. This makes up a 'test suite'. 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.

'Test-runners' 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 'pass' when the results of the execution match the expected results. If not, the test is a 'fail'.

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 'mock data'. Mock data is static data that is used to restrict the tests to the functionality of the unit.

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's prior work on 'SUnit.' JUnit grew massively popular over the next few years, ending the 'capture and replay' 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'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'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't have to look at other places for info. There are many popularunit testing 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'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'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.

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

  1. Product Requirements Document
  2. Family Branding
  3. Five Ws and One H
  4. Impact Effort Matrix
  5. Niche Marketing
  6. Key Competitive Differentiator

Top comments (0)