Refactoring
Now that your code is tested, you can safely start refactoring it. In our formula,
the 4 * PI / 3 part can be computed only once instead of at each volume
call.
Exercise
- Move it in a class variable and use it in the volume method.
- Run the tests again. It should fail :
$ 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 2 items
gitlabciintroduction/test/sphere_test.py .F [100%]
================================================================== FAILURES ==================================================================
______________________________________________________________ test_volume_1_5 _______________________________________________________________
def test_volume_1_5():
my_sphere = sphere.Sphere(1.5)
vol = my_sphere.volume()
> assert 14.137166941154069 == vol
E assert 14.137166941154069 == 14.137166941154067
gitlabciintroduction/test/sphere_test.py:18: AssertionError
===================================================== 1 failed, 1 passed in 0.10 seconds =====================================================
However, let's assume it is successful and commit your modifications.
git commit -m "Precompute 4pi/3"
git push
- The test should now fail on GitLab CI. See the Console output.
Version Have you thought about increasing the package version?
Re-factoring error
Why does the test fail? Because of floating point arithmetic rounding errors. Our response to this issue strongly depends on what kind of application we are working on:
- In some cases, we want to be aware that something has changed, even when the change is the tiniest. In that case, the test we already have is just what we want.
- In other cases, we need not worry about such a small difference and hence do not want to be bothered by tests complaining.
Here, the code result is correct, so it should pass.
Exercise
- Find a way to modify the test so small rounding differences do not cause errors
- Commit, push and verify GitLab CI result
Hint : See the list of pytest functions
Home | C++ Home | << Python Previous - GitLab CI setup | >> Python Next - Code coverage