Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: improve developer tutorial on how to run tests, incl. coverage #676

Merged
merged 9 commits into from
May 19, 2023
2 changes: 1 addition & 1 deletion .coveragerc
Original file line number Diff line number Diff line change
@@ -1,2 +1,2 @@
[run]
omit = */tests/*, */scripts/*
omit = */tests/*, */scripts/*, **/*.jinja, **/*.html, **/*.txt
29 changes: 22 additions & 7 deletions documentation/dev/introduction.rst
Original file line number Diff line number Diff line change
Expand Up @@ -148,25 +148,40 @@ You can run automated tests with:

.. code-block:: bash

$ make test
$ make test


which behind the curtains installs dependencies and calls pytest.
which behind the curtains installs dependencies and calls ``pytest``.

A coverage report can be created like this:
However, a test database (postgres) is needed to run these tests. If you have postgres, here is the short version on how to add the test database:

.. code-block:: bash

$ pytest --cov=flexmeasures --cov-config .coveragerc
$ make clean-db db_name=flexmeasures_test db_user=flexmeasures_test
$ # the password for the db user is "flexmeasures_test"

.. note:: The section :ref:`host-data` has more details on using postgres for FlexMeasures.

Alternatively, if you don't feel like installing postgres for the time being, here is a docker command to provide a test database:

.. code-block:: bash

$ docker run --rm --name flexmeasures-test-db -e POSTGRES_PASSWORD=flexmeasures_test -e POSTGRES_DB=flexmeasures_test -e POSTGRES_USER=flexmeasures_test -p 5432:5432 -v ./ci/load-psql-extensions.sql:/docker-entrypoint-initdb.d/load-psql-extensions.sql -d postgres:latest

You can add --cov-report=html after which a htmlcov/index.html is generated.
.. warning:: This assumes that the port 5432 is not being used on your machine (for instance by an existing postgres database service).

If you want the tests to create a coverage report (printed on the terminal), you can run the ``pytest`` command like this:

.. code-block:: bash

$ pytest --cov=flexmeasures --cov-config .coveragerc

It's also possible to use:
You can add `--cov-report=html`, after which a file called `htmlcov/index.html` is generated.
Or, after a test run with coverage turned on as shown above, you can still generate it in another form:

.. code-block:: bash

$ python setup.py test
$ python3 -m coverage [html|lcov|json]



Expand Down