pytest-api/README.md
Greg Gauthier a727c58cf3
All checks were successful
Pylint / build (3.12) (push) Successful in 9s
execute / build (3.12) (push) Successful in 25s
code cleanup for pylint
2024-07-22 19:26:27 +01:00

131 lines
8.6 KiB
Markdown

# pytest-api
This is a proof-of-concept project, showing how PyTest could be used in combination with a custom API client, to quickly
and easily build api tests for a data-delivery service that is deployed and active on an existing environment.
## Instructions
1. Add a `.env` file to your local copy of the repo. By default, this demo project will look for `.env.qa` in the root
of the project. You can find a template to base this on, in `apiclient/env_template`. Info on how to set the values
in that file can be found by asking Greg.
2. Create your virtual env:
```shell
python3 -m venv venv
```
3. Activate the environment:
```shell
source venv/bin/activate
```
On Windows:
```shell
.\venv\Scripts\activate
```
4. Install requirements (pip will come from your venv)
```shell
pip install -r requirements.txt
```
Once this is done, executing the tests is just a matter of invoking pytest:
```
(.venv) PS C:\Users\GregGauthier\Projects\local\pytest-api> pytest
======================================================================================================== test session starts ========================================================================================================
platform win32 -- Python 3.12.4, pytest-8.2.2, pluggy-1.5.0
rootdir: C:\Users\GregGauthier\Projects\local\pytest-api
configfile: pytest.ini
collected 5 items
tests\test_datadelivery.py ..... [100%]
========================================================================================================= warnings summary ==========================================================================================================
tests/test_datadelivery.py::test_datadelivery_role_get
tests/test_datadelivery.py::test_datadelivery_acms_redaction_get
tests/test_datadelivery.py::test_datadelivery_client_applications_get
tests/test_datadelivery.py::test_datadelivery_endpoint_get
tests/test_datadelivery.py::test_datadelivery_redaction_type_get
C:\Users\GregGauthier\Projects\local\pytest-api\.venv\Lib\site-packages\urllib3\connectionpool.py:1099: InsecureRequestWarning: Unverified HTTPS request is being made to host 'api-spectrumqa.teledynecontrols.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
============================================================================================================== PASSES ===============================================================================================================
======================================================================================================= slowest 25 durations ========================================================================================================
4.41s call tests/test_datadelivery.py::test_datadelivery_role_get
2.16s call tests/test_datadelivery.py::test_datadelivery_acms_redaction_get
1.98s call tests/test_datadelivery.py::test_datadelivery_redaction_type_get
1.79s call tests/test_datadelivery.py::test_datadelivery_client_applications_get
1.79s call tests/test_datadelivery.py::test_datadelivery_endpoint_get
1.04s setup tests/test_datadelivery.py::test_datadelivery_role_get
1.01s setup tests/test_datadelivery.py::test_datadelivery_acms_redaction_get
1.00s setup tests/test_datadelivery.py::test_datadelivery_endpoint_get
0.99s setup tests/test_datadelivery.py::test_datadelivery_client_applications_get
0.97s setup tests/test_datadelivery.py::test_datadelivery_redaction_type_get
(5 durations < 0.005s hidden. Use -vv to show these durations.)
====================================================================================================== short test summary info ======================================================================================================
PASSED tests/test_datadelivery.py::test_datadelivery_role_get
PASSED tests/test_datadelivery.py::test_datadelivery_acms_redaction_get
PASSED tests/test_datadelivery.py::test_datadelivery_client_applications_get
PASSED tests/test_datadelivery.py::test_datadelivery_endpoint_get
PASSED tests/test_datadelivery.py::test_datadelivery_redaction_type_get
================================================================================================== 5 passed, 5 warnings in 17.20s ===================================================================================================
```
PyTest is configured to show the top 25 test durations, as well as the pass/fail status of all the tests. The tests have
been marked with various tags, to allow for granular test selection. To see all the available markers:
```
(.venv) PS C:\Users\GregGauthier\Projects\local\pytest-api> pytest --markers
@pytest.mark.get: marks a test as a get request test (deselect with '-m "not get"')
@pytest.mark.role: marks a test as a role test (deselect with '-m "not role"')
@pytest.mark.endpoint: marks a test as an endpoint test (deselect with '-m "not endpoint"')
@pytest.mark.redaction: marks a test as a redaction test (deselect with '-m "not redaction"')
@pytest.mark.client_application: marks a test as a client application test (deselect with '-m "not client_application"')
. . . (etc) . . .
```
To run tests with a specific marker:
```
(.venv) PS C:\Users\GregGauthier\Projects\local\pytest-api> pytest -m "redaction"
======================================================================================================== test session starts ========================================================================================================
platform win32 -- Python 3.12.4, pytest-8.2.2, pluggy-1.5.0
rootdir: C:\Users\GregGauthier\Projects\local\pytest-api
configfile: pytest.ini
collected 5 items / 3 deselected / 2 selected tests\test_datadelivery.py .. [100%]
========================================================================================================= warnings summary ==========================================================================================================
tests/test_datadelivery.py::test_datadelivery_acms_redaction_get
tests/test_datadelivery.py::test_datadelivery_redaction_type_get
C:\Users\GregGauthier\Projects\local\pytest-api\.venv\Lib\site-packages\urllib3\connectionpool.py:1099: InsecureRequestWarning: Unverified HTTPS request is being made to host 'api-spectrumqa.teledynecontrols.com'. Adding certificate verification is strongly advised. See: https://urllib3.readthedocs.io/en/latest/advanced-usage.html#tls-warnings
warnings.warn(
-- Docs: https://docs.pytest.org/en/stable/how-to/capture-warnings.html
============================================================================================================== PASSES ===============================================================================================================
======================================================================================================= slowest 25 durations ========================================================================================================
2.32s call tests/test_datadelivery.py::test_datadelivery_acms_redaction_get
1.96s call tests/test_datadelivery.py::test_datadelivery_redaction_type_get
1.09s setup tests/test_datadelivery.py::test_datadelivery_acms_redaction_get
0.99s setup tests/test_datadelivery.py::test_datadelivery_redaction_type_get
(2 durations < 0.005s hidden. Use -vv to show these durations.)
====================================================================================================== short test summary info ======================================================================================================
PASSED tests/test_datadelivery.py::test_datadelivery_acms_redaction_get
PASSED tests/test_datadelivery.py::test_datadelivery_redaction_type_get
============================================================================================ 2 passed, 3 deselected, 2 warnings in 6.38s ============================================================================================
(.venv) PS C:\Users\GregGauthier\Projects\local\pytest-api>
```
That's it!