Testing simply means to the code you have written passes all the scenarios for which it is written. When we say this, we all know it’s in theory. There is nothing called ‘perfect’ code. It’s just that your code may or may not cover the scenario which as a developer you haven’t thought of.
Product development is becoming more matured than it used to be before. Testing approach has been evolved from manual testing to automated testing.
In automated testing, the developers write the code scripts to invoke that detects or determines the errors in the existing application code. This testing is not done to check whether the code is correctly written, but to check the conditions or scenarios the code have been handled correctly.
Unit Testing is an automated testing methodology that tests a single unit of code in isolation. A unit in Python can be entire module, a function or a class.
Writing unit tests need more thoughts because a unit is an isolated piece of code that needs to be executed without any dependency. The challenge in writing unit tests is, a code written may be dependent on the other piece of code.
Why Unit Test the Python code?
Unit testing in Python helps detecting 2 main checks:
- Syntax errors
- Logical errors
Errors like putting extra . in the command line or any error that Python doesn’t know how to proceed with.
Unit tests detects this type of error in Python unittest framework.
Errors in algorithm written for the logic such as Python crashes when the user inputs number that is not handled by the code or hangs if the website is not available due to any reason.
In unit tests, these errors can be detected easily.
- Write complete unit tests for all possible conditions
As a developer, you know better what functional features or functionality your code provides.
- Run your unit tests each time you add or modify or enhance your existing code.
Unit tests give you assurance that changes in your code have not broken any existing functionality which was working fine before addition/modification. Unit tests help you refactor the code if required.
- Write unit tests thinking on non-normal conditions –
Writing tests widens your thinking on which condition the code can fail. You may encounter such conditions with the functions or classes you have written. It helps you handling exceptions on invalid input.
- Implement a hook that runs all the code before pushing a code to a shared repository.
- Write a test for the error in your code while debugging. Bug catching tests are the most important and valuable tests.
- Use long and descriptive names for testing functions. When your test fails, it displays these function names. So names should be as descriptive as possible.
- Write unit tests in such a way that any issue occurs in the code should be detected by running the test suite.
- As a good development approach, Python unit tests need to be executed more frequently. Use Stubs and Mocks. These are two major techniques for using fake methods being tested as thinking that these are using a real system resource.
More on Stubs and Mocks
To test application module or component that may have dependency on the other component that may have been developed or are developed, the developer needs to write stubs. In short, stubs are more of application state related tests.
A mock is the testing technique that has more to do for verifying that the unit did what it was supposed to or designed to do. Mocks are more of application behaviour.