capsul-flask/docs/testing.md

1.7 KiB
Raw Blame History

automated testing

Automated tests could make it safer to contribute code, easier to review new code, and much easier to refactor, or upgrade Python dependencies.

To run tests:

  1. create a Postgres database called capsulflask_test
  • e.g.: docker exec -it d1702306f409 psql -U postgres createdb -O postgres capsulflask_test;
    • (d1702306f409 is the docker container ID of the postgres container)
  1. run python -m unittest

Architecture

I tried to make the absolute minimal changes to be able to override settings in tests possible alternative approaches include accepting an argument to create_app() to define which env file to load, or adding conditional logic to create_app() to pre-load specific settings before running load_dotenv() but allowing env vars to override dotenv vars seemed cleanest. (Thanks @forest for improving on this approach)

Creating test databases

One outstanding question is how to initialise/reinitialise the test database.

Currently, the tests rely on the existence of a capsulflask_test database on localhost, accessible by the postgres user with password dev.

I create this manually using: docker exec -it d1702306f409 psql -U postgres createdb -O postgres capsulflask_test;

where d1702306f409 is the docker container ID of the postgres container.

In between test runs, you can either drop and recreate that database, or manually clear data using:

docker exec -it d1702306f409 psql -U postgres capsulflask_test -c "DELETE FROM vms; DELETE FROM login_tokens; DELETE FROM ssh_public_keys; DELETE FROM api_tokens; DELETE FROM accounts;

Test coverage

This tests the "landing" (public) pages, login, capsul index and creation. I didn't add automated coverage reporting yet, unclear if that seems useful.