Go to file
Greg Gauthier f80812d953
Some checks failed
build / build (push) Failing after 11s
Update .gitea/workflows/.build.yaml
2024-07-21 20:28:19 +00:00
.gitea/workflows Update .gitea/workflows/.build.yaml 2024-07-21 20:28:19 +00:00
apiclient clear rest sessions between tests;reset config data between tests;other bits and bobs 2024-07-20 20:17:47 +01:00
tests clear rest sessions between tests;reset config data between tests;other bits and bobs 2024-07-20 20:17:47 +01:00
.gitignore update gitignore 2024-07-20 16:30:36 +01:00
pytest.ini add markers to pytest.ini; add additional example tests;add proper readme 2024-07-20 14:12:47 +01:00
README.md add markers to pytest.ini; add additional example tests;add proper readme 2024-07-20 14:12:47 +01:00
requirements.txt initial commit 2024-07-20 11:47:11 +01:00

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:

python3 -m venv venv
  1. Activate the environment:
source venv/bin/activate 

On Windows:

.\venv\Scripts\activate
  1. Install requirements (pip will come from your venv)
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!