-
Notifications
You must be signed in to change notification settings - Fork 573
How to write a unit test
The unit tests are a very important asset of this project. Due to our 100% test coverage we can savely use great tools like Dependabot and be sure that the implementation of a new feature or fixing of a bug does not lead to further issues.
We're relying on pytest for the creation of our tests and VCR.py for automatic mocking of the communication to iCloud. This makes the creation of testcases rather simple as you don't have to deal with the communication to iCloud itself and can just fokus on the "real test". Both tools maintain great howtos that can be found here:
- pytest documentation: https://docs.pytest.org/en/stable/
- VCR.py documentation: https://vcrpy.readthedocs.io/en/latest/
It is highly recommented to have a look at those.
The process is mostly like this (assuming we're talking about a bug fix here...)
- Is there already a related testcase existing? If so you can just check if an existing test needs to check for another situation.
- If not, then you need to make sure you have correspnding test-data at hand; that means: your iCloud photos library should have a constellation that leads to the error in
icloudpd
. - Add a test-function that runs
icloudpd
with the necessary start parameters, referencing to a new cassette file. -
VERY IMPORTANT: the real iCloud response is cached, so every image is saved in the cassette. That means:
- Don't use private photos!
- keep the dataset small (p.e. using
--recent
) - Remove your personal information from the cached REST-response (Name, email adresses)
- Go back to the previous step and verify again that you followed the recommendations! Then you can start adding tests.
Refer to the existing tests for inspiration. A very simple test to to understand the basic idea might be the test for the listing of albums option in tests/test_listing_albums.py
.
When testing a bugfix it is important to test the faulty behavior and also the expected behavior.