Principles

When we dive into writing unit tests we can easily fall down the path of doing the wrong things – this implies that generally developers get excited from the unit testing capabilities and find themselves writing unnecessary test cases and bad ones to in the heat of the moment. That’s why it is advised for companies or teams to have an internal guide for their unit testing practices.

Unit testing starts by describing a ‘suite’ of an encapsulated logic/code. This encapsulated logic can either be in a service, utility class or method. That suite then delves deeper into case scenarios for said logic and what they ‘should’ expect from a unit of that logic.

There are a set of basic rules to follow while you are unit testing your code and defining your test suites:

  1. Be concise in the description of your test cases. Never use names of methods within the test case description because it can cause technical debt in your test suite if said method changes its name and the test description gets forgotten to be updated
  2. Try to use the Triple A pattern as much as possible. This pattern is simple in implementation and all we need to do is firstly ‘Arrange’ our variables or dependencies which are needed for the unit of logic to be executed or asserted. We ‘Act’ upon said variables/dependency and initialise the unit of logic and finally we ‘Assert’ the results.
  3. If conditions in a unit test is a big don’t. The reason behind that statement is simple – if you find yourself writing an if condition within the test case you are branching out your business logic and asserting that logic is very difficult to maintain through those test cases.
  4. Test cases, unit tests should do only one thing. It’s in the name after all: UNIT test and note a bunch of methods or classes test.
  5. Do not clutter your test cases with hardcoded expected values stored in variables or constants within the test case. Instead create a reusable mocking structure and data.
  6. Be mindful of your code that you want to test. If you see yourself struggling to write a decent well-structured unit test then you have to get your hands dirty and refactor the code that is being unit tested.

Challenges

Unit testing is a crucial aspect of software development that helps ensure the reliability and maintainability of code. However, despite its numerous benefits, developers often face several challenges when it comes to effectively implementing unit tests. Below are some of the prominent hurdles and how to tackle them:

  1. Setting up the mentality to write unit tests: Introducing a testing mindset among developers can be difficult, especially in environments where testing is seen as a time-consuming process rather than an investment in code quality. To address this challenge, organizations should emphasize the importance of unit testing in the development process, provide training and resources on writing effective tests, and celebrate successful testing efforts to foster a positive testing culture.
  2. Progressing from unit tests to test-driven development: Transitioning from writing unit tests after code implementation to test-driven development (TDD) can be challenging. TDD advocates writing tests before writing the actual code, which helps in better defining requirements and maintaining a testable codebase. Encouraging developers to adopt TDD requires patience, practice, and management support, as the process might initially slow down development but ultimately results in improved code quality and faster development cycles.
  3. Balancing time spent on unit tests and delivering quality code: There is a delicate balance between writing comprehensive unit tests and delivering high-quality code promptly. While writing tests is essential, developers should focus on the critical parts of the codebase and ensure that tests cover potential failure scenarios and edge cases. Employing code review processes and automated testing tools can help identify redundant or less valuable tests, streamlining the unit testing process.
  4. Misunderstanding unit tests with integration tests: Differentiating between unit tests and integration tests can be challenging for some developers. Unit tests focus on individual components in isolation, whereas integration tests assess interactions between various components.
  5. Testing the wrong things: Sometimes, developers may inadvertently write tests that only check implementation details rather than the expected behavior. This can lead to tests becoming brittle and failing frequently when refactoring or modifying the code.

Leave A Comment