Testing your code often is a good thing to do. Imagine running a big project without any tests, what could be the implications if you change a feature it affects the functionality of the overall app? Will that be a good thing to do? For many, it is not. Let’s see how we can perform and write tests right from the start.
When starting out as a developer often write small unit tests to guarantee that the feature you have introduced really doing the intended function. Also, make sure you write some failing tests.
Rules of Testing
- Use long and descriptive names for testing functions, this often helps those working on your code to know where to change if something is not working right.
- Whenever you are creating tests, make sure it’s running fast, this will ensure that you don’t slow down the development process.
- Only focus on one tiny bit of functionality. Write tests to test only one single function and make sure it passes successfully.
- Always write failing tests to guarantee that you are on the right path of development. This will improve your testing and development strategies.
- Ensure that each unit test is fully independent and run each test alone.
- Properly implement ways to run tests before pushing the code to a shared repository.
Types of Tests to perform
In Python, we have different types of tests such as:
- Unittest
- Pytest
Unittest
Unittest
is built into the Python standard library. It supports test automation, sharing of setup and shutdown code for tests, aggregation of tests into collections, and independence of tests from the reporting framework.
Unittest provides a rich set of tools for constructing and running tests. Let’s look at an example.
import unittest
class Test_If_Sum_Equal(unittest.TestCase):
def test_sum(self):
self.assertEqual(sum([45,4,1,20]), 70, "Should be 70")
if __name__ == '__main__':
unittest.main()
To run the above code use the following command:
python -m unittest test
If you run the above code, it will pass the test because if you add up it will be 70, but if you change a figure, tests will not pass because it will not be the same as the intended outcome.
Tests need to be validated against the output, you need to be aware of the output so that what you have in your code will give you the intended response, this is called assertion
.
Types of assert in unittest
Method | Equivalent to |
---|---|
Method | Equivalent to |
.assertEqual(a, b) | a==b |
.assertFalse(x) | bool(x) is False |
assertTrue(x) | bool(x)is True |
.assertIs(a, b) | a is b |
.assertIn(a, b) | a in b |
.assertIsInstance(a, b) | isinstance(a,b) |
.assertIsNone(x) | x is None |
Pytest
Pytest supports the execution of unittests. Pytest is a Python testing framework, it can be used to write various types of software tests such as integration tests, unit tests, functional tests, etc.
Features of pytest
- Has support for test case filtering
- Has support for a built-in assert statement
- Has support for many plugins to extend its functionality.
To start using pytest, you need to install it first.
pip install -U pytest
Let’s create our first test case
def test_sum():
assert sum([45,4,1,20]) == 80, "Should be 70")
To run the following test use command pytest
pytest
The above test will fail because if you add up the figures it’s not the same as the figure given.
Ways of specifying tests in Pytest.
Run tests in a directory. Specify a directory to run tests. Mostly named the directory test, and from there pytest will pick it automatically.
pytest testing/
Run tests in a module. You can also specify which module you want specifically started. This is when you want that one specific test
pytest test_mod.py
Conclusion
We have seen how to write simple tests using unittest and pytest, build on this foundation to write more tests because testing is a key part of development.