Skip to content

How to write a unit test

Jörg Menke edited this page Dec 2, 2020 · 2 revisions

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:

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...)

  1. Is there already a related testcase existing? If so you can just check if an existing test needs to check for another situation.
  2. 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.
  3. Add a test-function that runs icloudpd with the necessary start parameters, referencing to a new cassette file.
  4. VERY IMPORTANT: the real iCloud response is cached, so every image is saved in the cassette. That means:
    1. Don't use private photos!
    2. keep the dataset small (p.e. using --recent)
    3. Remove your personal information from the cached REST-response (Name, email adresses)
  5. 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.

Clone this wiki locally