Writing unit tests is an essential practice for ensuring code quality, reliability, and maintainability. This week, I wrote test cases for my open source project in Python.
Why I chose unittest framework
I chose unittest framework because it is included in Python standard library, which means there is no need to install external dependencies. It provides a structured way to write tests using classes, and a variety of assertions, which makes it easier to compare expected and actual results. Moreover, unittest has a well established community and documentation, making it easy to learn and find examples when needed.
How I set up the tests
To set up testing in my proje…
Writing unit tests is an essential practice for ensuring code quality, reliability, and maintainability. This week, I wrote test cases for my open source project in Python.
Why I chose unittest framework
I chose unittest framework because it is included in Python standard library, which means there is no need to install external dependencies. It provides a structured way to write tests using classes, and a variety of assertions, which makes it easier to compare expected and actual results. Moreover, unittest has a well established community and documentation, making it easy to learn and find examples when needed.
How I set up the tests
To set up testing in my project, I first created a test.py file to keep all my test cases organized. I imported the necessary unittest module, and other modules when writing the tests, such as tempfile, os, time, Path from pathlib, as well as the modules from my project, namely util and ContentWriter. I also organized the tests into two classes:
- TestUtil, for testing utility functions in util.py
- TestContentWriter, for testing methods in ContentWriter in content_writer.py
Then, I wrote individual test methods for each function, to include the valid, invalid, and edge cases. Once the tests were written, I could run them all at once with the command python3 -m unittest test.py -v, which provided a clear report of which tests passed or failed.
Difficulties writing the tests
I initially struggled with finding the right modules and functions to use in my tests. For instance, I had to research the tempfile module to create temporary files and directories safely. I also learned how to use os.utime() to modify file timestamps in order to test the is_recently_modified() function. These challenges show the importance of searching the right tools in testing, not only the testing framework, but also modules that are useful in testing.
Lessons learnt
Overall, the process of writing tests demonstrates the value of testing in software development. It forced me to think about edge cases I might have overlooked and gave me confidence that my code behaves as expected. Using unittest is straightforward and powerful enough for the needs of this project. From this experience, I have learned that testing should be an essential part of every project, and I plan to study how to automate the testing process in future projects to maintain code quality and catch bugs early.