Does your software team struggle with testing that frequently fails or has been disabled for the sake of pushing code out in a timely manner? How confident are you that your pipelines have a well-defined process to ensure that Unit Testing is incorporated as part of new code development in your Agile or software development methodology?

Unit testing is a critical aspect of the software development lifecycle. Prioritizing testing via in-line pipelines can save many headaches downstream.

One of the critical aspects of QA is unit testing, a process that tests the smallest units of code to ensure that they function correctly. Unit testing plays a crucial role in ensuring the quality of software. A common downfall of many organizations is to disable existing unit tests, or refrain from authoring new tests, that integrate with existing software & future feature development.

Why Test At All?

A telltale sign of a mature and stable codebase is one with a robust, validated and regularly-run set of unit tests. Unit testing offers numerous values, some of which are outlined below:

  • Early detection of bugs: Spotting issues is easier and bugs are less expensive to fix when defects are identified immediately. When developers write code, they can test it immediately through unit testing to identify issues in the code in their local development environments, reducing development time downstream when merging features into a mainline branch for integration testing. This reduces the cost and time needed for fixing defects later in the software development process, likely mitigating production release issues and the need for later hotfixes.

  • Improved code quality & compliance: Ensuring that code meets requirements and specifications, whether for security or compliance, is a frequent use case for unit testing. By testing the smallest units of code, developers can verify the functionality of the code and identify areas that require improvement. This helps to ensure that the software meets the expected quality standards and specifications. Verification outputs in testing can be used for compliance audits & submissions or to mitigate risks identified in the testing process.

  • Facilitate future refactoring: Building a safety net that ensures code changes do not break existing functionality is a core value of unit testing. This helps to improve the maintainability of the codebase over time. Immediate feedback advises where code changes have measured impacts to the broader codebase and new functionality may have unintended causes. By using unit tests to improve the readability of code and understanding of how changes impact functionality, maintainability and extensibility of the codebase becomes a standard practice.

  • Support continuous integration & delivery: Tests are an essential part of a CI/CD pipeline and by running them in an automated fashion, feedback is fast and programmatic. These feedback loops work to ‘shift left’ the testing process in the pipeline, identifying defects on local developer workstations or integration environments long before code makes it into customer testing or production ecosystems. It allows a collaborative approach to testing, showcasing test passes and failures to the broader development team that inform group efforts on development initiatives. Unit Testing can also be leveraged in regression testing, ensuring that previously working code functions as expected and eliminate unintended consequences of code changes such as introduction of failures into previously working components.

Common Unit Testing Missteps

Anti-patterns are common mistakes or bad practices that can lead to poor quality in the codebase, slower development cycles and difficult long-term maintenance. While unit testing can add immense value, there are several missteps to watch for:

  • Testing bloat: When there are too many tests, or tests are overly complex, the entire testing suite can be slowed to a halt. If your unit testing ecosystem takes hours to complete for each build, development staff will lose productivity. Consider moving bulk tests to occur later in the development workstream (only run a fully featured test suite in an integration or user acceptance testing environment) and focusing on relevant tests to the immediate feature development for local development or early-stage integration environments.

  • Brittle tests: Tests that fail frequently may be too specific, making them sensitive to smaller changes in the codebase. These can be difficult to maintain, fail builds unnecessarily, or lead developers to lack trust in the testing suite itself.

  • Over-reliance on mocks: Mocking is a useful technique for unit testing that can be unintrusive to test intended behavior, but over-reliance on mocks can lead to tests that don’t accurately reflect how the code behaves in production. This can result in false positives or negatives leading to issues in integration environments or production.

  • Ignoring edge cases: When tests only cover the most common use cases, important fringe scenarios may be overlooked. This can result in production issues or lengthy integration cycles. Edge cases often involve input values that are outside a typical range. Many times, if an edge case is ignored early, it can be challenging to rewrite or add testing for these cases later. Determining when to write unit tests for edge cases is a careful exercise to ensure that testing bloat does not occur.

  • Utilizing Misleading Metrics for Validation: Many metrics exist that can be quantified in software development systems which may be misleading or misinterpreted as validators of quality. Code coverage is often used as a sanity check on whether a volume of tests exist for a set of code or features, but does not audit the validity, brittleness, or bloat factor of testing. Similarly, lines of code, number of bugs and cyclomatic complexity are all often misinterpreted.

Architect for Integrated Testing

Bringing a quality unit testing process into your Agile or other software development lifecycle methodology (SDLM) process is critical to long-term success. Consider whether the following factors are regularly accounted for in your development teams.

  • Test as part of Definition of Done: The Definition of Done (DoD) is a set of criteria that must be met before a feature can be considered complete. Including unit testing as a requirement in the DoD can ensure that developers prioritize testing during feature development.

  • Test-Driven Development: Test-Driven Development is a methodology that requires developers to write tests before writing code. By writing tests first, developers are forced to think about how the code should behave and what inputs and outputs are required. TDD ensures that unit testing is accounted for during feature development from the outset.

  • Conduct Code Reviews: Code reviews are an effective way to ensure that unit testing is accounted for during feature development. Code reviews can help identify areas of the code that are not properly tested and can provide an opportunity to discuss testing strategies and best practices.

  • Pair/Peer Programming: Pair programming involves two developers working together on the same codebase. This approach can help ensure that testing is accounted for during feature development. Pair programming allows developers to discuss testing strategies and ensures that both developers are writing and testing the code.

  • Automate: Automated testing can be used to ensure that unit testing is accounted for during feature development. Developers can create automated tests for new features as they are developed, ensuring that the tests are integrated into the development process from the outset. Be cautious to how metrics are evaluated as part of automation pipelines. (See: misleading metrics above).

If your testing cycles are taking longer than usual, or have been disabled to facilitate shipping code, it may be time to evaluate the symptoms and address the root cause. Sidelining active development, or repurposing developer cycles to fix unit testing issues, can be a large hit to development velocity. Working with a trusted partner is key. Eastwall has a worked with dozens of leading software organizations to refactor and re-enable unit tests, as well as build processes that ensure unit testing is encapsulated in effort estimations and prioritized appropriately. We help design, build, and operate innovative cloud solutions on the Azure platform. Please contact us for a free consultation on how various developer strategy & Azure cloud services can help transform your business.

tagList [devops, software-development]

Looking for more content?

Get updates sent to your inbox