The Benefits of Using Constants in Python Unit Tests: A Beginner's Perspective 🔢
Tags: python, testing, beginners, tutorial
Published: 2023-03-23 15:59:55

## {% details Outlines %}
- [Introduction 🌟](#introduction)
- [Python unittest Example 🤖](#python-unittest-example)
- [Testing Expected Exceptions 🤚](#testing-expected-exceptions)
- [Conclusion 🤝✅](#conclusion)
{% enddetails %}

## Introduction 🌟

As a beginner in unit testing, I recently learned from [PythonTutorial.net](https://www.pythontutorial.net/python-unit-testing/python-unittest/) that when writing Python unit tests, it's important to ensure that the code is not only functional but also readable. Readability makes it easier for other developers to understand the code and make changes, which ultimately leads to more maintainable code. In this blog post, I want to share my insights on how using constants in Python unit tests can promote readability and improve the overall quality of the code.

> *Note: I've recently gained new knowledge about unit testing, and I'm excited to share it with you. Please consider that the information below represents my current understanding, which may evolve over time. I'll continue to update and refine this post as I learn more about the intricacies of unit testing. Your understanding and patience are greatly appreciated.*

## Python unittest Example 🤖

Square class:
{% embed https://gist.github.com/ranggakd/eca4181c6cef7e819f2bed0378be7300 %}

The original code snippet tests the `Square` class, where it initializes an instance of the class with a length of `10` and checks if the area is equal to `100`. While this test case is simple, it doesn't take into account different values that can be passed to the `Square` class. By using constants, we can easily modify the test cases to cover a wider range of values.

Original:
{% embed https://gist.github.com/ranggakd/afcbe7c6ff39bc2bea6bdeafd9bf8e47 %}

In the suggested code snippet, we define a constant `VALUE_RANGE` to set the maximum value for the test cases. We then define constants for each test case, such as `POSITIVE_INT_VALUE` and `POSITIVE_FLOAT_VALUE`, which represent a positive integer and float within the `VALUE_RANGE`, respectively. We also define `POWER_OF_TWO_PIV` and `POWER_OF_TWO_PFV`, which represent the expected area for the positive integer and float, respectively.

My suggestion:
{% embed https://gist.github.com/ranggakd/c8672a6e31a46774921ca23f91a4929c %}

By using constants, we can easily modify the range of values to be tested and the expected results without having to modify each individual test case. This makes the test cases more maintainable and easier to understand, especially when we need to update the values or add new test cases.

## Testing Expected Exceptions 🤚

In the original fixed code snippet, the `Square` class raises a `TypeError` if the length is not an integer or float and raises a `ValueError` if the length is negative. 

Original fixed class:
{% embed https://gist.github.com/ranggakd/c06f1c63be7e381faf4af6539c9338d8 %}

However, this approach may not be ideal in real-world scenarios since __modifying a class directly can have unintended consequences and should be avoided__. In such scenarios, __it's better to communicate with the developer responsible for the module and provide feedback on the failed test cases__. We still can test it by making a new test class file called `fixed_square.py`.

Suggested class:
{% embed https://gist.github.com/ranggakd/eca4181c6cef7e819f2bed0378be7300 %}

In the suggested code snippet, we modify the `Square` class to only accept a length parameter and remove the exception handling code. We then define constants for each test case, such as `POSITIVE_INT_VALUE` and `POSITIVE_FLOAT_VALUE`, which represent a positive integer and float within the `VALUE_RANGE`, respectively. We also define `POWER_OF_TWO_PIV` and `POWER_OF_TWO_PFV`, which represent the expected area for the positive integer and float, respectively. Take a look at the final original `unittest` for comparison.

Final original `unittest`:
{% embed https://gist.github.com/ranggakd/e2f9d43ef5ed0dc9318d43dc222ccec7 %}

By using constants, we can easily modify the test cases to cover a wider range of values without having to modify the class. Additionally, we can easily modify the range of values to be tested and the expected results without having to modify each individual test case. This makes the test cases more maintainable and easier to understand, especially when we need to update the values or add new test cases.

Final suggestion `unittest`:
{% embed https://gist.github.com/ranggakd/b8cddef652c596494502cd0f86e304d4 %}

## Conclusion 🤝✅

Based on the new knowledge gained from [PythonTutorial.net](https://www.pythontutorial.net/python-unit-testing/python-unittest/), using constants in Python unit tests can promote readability and improve code quality in the following ways:
1. Defining constants for the range of values to be tested and the expected results can easily modify the test cases to cover a wider range of values without having to modify each individual test case.
1. Constants make test cases more maintainable and easier to understand, especially when updating values or adding new test cases.
1. Modifying a class directly may not be a good practice in real-world scenarios, and we should communicate with the developer responsible for the module instead.

As a beginner in unit testing, I hope that sharing my insights can help others who are also starting their journey in writing Python unit tests. I am always looking to improve my skills and welcome feedback and suggestions in the comments section below. Let's learn and grow together!

Follow me anywhere {% embed https://beacons.ai/ranggakd %}