Tests
Executing your code in a command line and visually verifying the result consists in testing. However some parts of the code may be hard to reach from the cli, and are hard to test this way. Also, we want to automate this step and make it repeatable.
So basically, we need to write code to execute our code and verify the output. And to simplify the organization we will use existing tools to write them. In this section you will write tests and execute them with pytest tool.
The test test_volume_0
has already been written as an example. Run it
with pytest:
$ pytest
============================================================ test session starts =============================================================
platform linux -- Python 3.6.7, pytest-4.0.1, py-1.7.0, pluggy-0.8.0
rootdir: /home/user/gitlabciintroduction/python, inifile:
collected 1 item
gitlabciintroduction/test/sphere_test.py . [100%]
========================================================== 1 passed in 0.07 seconds ==========================================================
Now go in gitlabciintroduction/test/sphere test.py to see the implementation. You can have a look to pytest good practices to understand pytest discovery strategy. This framework helps configuring a test environment.
Exercise
test_volume_0
is not enough to validate your code.
- Add another test to validate the volume computation with: radius = 1.5 where volume = 14.137166941154069
- Commit your code with the new test.
Home | Python Home | << Python Previous - Packaging | >> Python Next - GitLab CI setup