It’s very important to have automated tests for your applications. You never know which component will fail at what time. Sometimes, while using an application, when the application depends on several external services, it is possible that one or more of them might change their behaviour. This can be really difficult for developers to detect unless there are automated tests.
In a recent project that I worked on (Yoda PA), I used to get many bug reports that simply turned out to be some behaviour changes in a service that I was using earlier. Hence, I planned to write some tests for the project, so that the next time I am better aware of the problem.
I wrote a simple test that would check the chat command. The test would just say hello and expect to get a response.
# coding=utf-8from unittest import TestCasefrom click.testing import CliRunnerimport yodaclass TestChat(TestCase): """ Test for the following commands: | Module: chat | command: chat """ def __init__(self, methodName='runTest'): super(TestChat, self).__init__() self.runner = CliRunner() def runTest(self): result = self.runner.invoke(yoda.cli, ['chat', 'hello']) self.assertEqual(result.exit_code, 0) result = self.runner.invoke(yoda.cli, ['chat']) self.assertEqual(result.exit_code, 0)
I think the code doesn’t need much explanation here.
Help from others
As you can see, the project’s code base has grown pretty big now and it would take me a lot of time to thoroughly test the whole project. So, I turned to the open source community and created an issue with some details regading which classes need to be tested, and provided an example test file.
Then one fine day, some good guy wrote some basic tests for the project and helped to increase the test coverage from ~
40%! That was amazing.
After some days, when I got time, I enhanced those tests and added some more, and took the coverage to around
What I learned
While writing tests, I realized how messed up some of the code was, and how in some places some dirty hacks were used to get the work done. I made appropriate changes to the code base to make it testible, but there is still a large proportion of the code that needs to be cleaned up so that it can be tested and understood properly.