From 54f5eb212aad8121a16027975331c5a444158dec Mon Sep 17 00:00:00 2001 From: Oussama Jarrousse Date: Wed, 27 Dec 2023 11:36:25 +0100 Subject: [PATCH] added markers; edited README.md --- README.md | 39 +++++++++++++++++++++++++++++++++++++++ pytest.ini | 3 +++ tests/test_views.py | 6 +++++- 3 files changed, 47 insertions(+), 1 deletion(-) diff --git a/README.md b/README.md index 2cae41f..d0c5002 100644 --- a/README.md +++ b/README.md @@ -196,6 +196,44 @@ function some_func() { ```` +# Testing + +We use `pytest` and several pytest plugins, especially the `pytest-django` and `pytest-cov` plugins that provide Django fixtures, and test coverage analysis. + +In the root folder, `pytest.ini` contains configurations for running the tests, `requirements_testing.txt` contains the python packages required for running tests, and the folder `tests` contains the actual test files. + +To run the tests, install the packages in requirements and requirements_testing.txt: + +```bash +pip install -r requirements.txt +pip install -r requirements_testing.txt +``` + +then simply run pytest +``` +pytest +``` + +to generate the coverage html pages: + +``` +pytest --cov=. --cov-report html -v +``` + +the coverage html files will be generated in the `htmlcov` folder + +We use `tox` to test the package against different isolated environments. `tox.ini` contains the configurations for tox. To run the tests in the environments defined in the `tox.ini` file, make sure the python package tox is installed: + +``` +pip install tox +``` + +then run tox in the project root: + +``` +tox +``` + # Contributors * [mahmoodnasr](https://github.com/mahmoodnasr) * [d3cline](https://github.com/d3cline) @@ -207,6 +245,7 @@ function some_func() { * [ezrajrice](https://github.com/ezrajrice) * [Spitfireap](https://github.com/Spitfireap) * [peterthomassen](https://github.com/peterthomassen) +* [oussjarrousse](https://github.com/oussjarrousse) # Security contact information diff --git a/pytest.ini b/pytest.ini index e3e533a..f4a4976 100644 --- a/pytest.ini +++ b/pytest.ini @@ -95,6 +95,9 @@ markers = NON_PRIVILEGED_USER: tests for non-privileged users PERMISSIONS: tests related to permissions + ANNONYMOUS_USER: tests for non-authenticated users + AUTHENTICATED_USER: tests for authenticated users + ENDPOINTS: tests for endpoints (API nodes) SERIALIZERS: tests for serializers VIEWS: tests for DRF viewsets diff --git a/tests/test_views.py b/tests/test_views.py index c39366a..144946a 100644 --- a/tests/test_views.py +++ b/tests/test_views.py @@ -1,6 +1,9 @@ import pytest from django.urls import reverse +@pytest.mark.VIEWS +@pytest.mark.DJANGO +@pytest.mark.ANNONYMOUS_USER @pytest.mark.django_db def test_index_unauthenticated(client): url = reverse("mfa_home") @@ -9,7 +12,8 @@ def test_index_unauthenticated(client): assert response.status_code == 302 assert response.url=="/accounts/login/?next=/" - +@pytest.mark.VIEWS +@pytest.mark.AUTHENTICATED_USER @pytest.mark.django_db def test_index_authenticated(client, authenticated_user): url = reverse("mfa_home")