From aa2b8411d357141c1510d584e5157e8cae99e6b8 Mon Sep 17 00:00:00 2001 From: Dan Lipsitt <578773+DanLipsitt@users.noreply.github.com> Date: Wed, 9 Feb 2022 17:44:56 -0800 Subject: [PATCH 1/3] Add schemathesis tests. --- tests/dev.requirements.txt | 1 + tests/test_schemathesis.py | 25 +++++++++++++++++++++++++ 2 files changed, 26 insertions(+) create mode 100644 tests/test_schemathesis.py diff --git a/tests/dev.requirements.txt b/tests/dev.requirements.txt index 4ecb350e..d32ffcfc 100644 --- a/tests/dev.requirements.txt +++ b/tests/dev.requirements.txt @@ -18,6 +18,7 @@ requests asynctest psycopg2 pyodbc +schemathesis # Linting flake8 diff --git a/tests/test_schemathesis.py b/tests/test_schemathesis.py new file mode 100644 index 00000000..e4cb8aaa --- /dev/null +++ b/tests/test_schemathesis.py @@ -0,0 +1,25 @@ +import pytest +import schemathesis +from fastapi import FastAPI +from schemathesis.specs.openapi.schemas import BaseOpenAPISchema + + +@pytest.fixture() +def app_schema(client) -> BaseOpenAPISchema: + """ + Get an OpenAPI schema instance for the app created by the `client` fixture. + """ + app: FastAPI = client.app + assert client.app is not None + openapi = app.openapi() + result = schemathesis.from_dict(openapi) + return result + + +schema = schemathesis.from_pytest_fixture("app_schema") + + +@schema.parametrize() +def test_api(case): + """Run tests automatically generated by schemathesis.""" + case.call_and_validate() From 9f33eb42e875b43221c9e6e5a754884a0c1e6ca9 Mon Sep 17 00:00:00 2001 From: Dan Lipsitt <578773+DanLipsitt@users.noreply.github.com> Date: Thu, 10 Feb 2022 08:15:32 -0800 Subject: [PATCH 2/3] Workaround for pytest bug. pytest 7.0.0 does not work with python 3.9. Exclude that specific combo. From: https://github.com/pytest-dev/pytest/issues/9608#issuecomment-1030370532 --- tests/dev.requirements.txt | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/tests/dev.requirements.txt b/tests/dev.requirements.txt index d32ffcfc..842e5a72 100644 --- a/tests/dev.requirements.txt +++ b/tests/dev.requirements.txt @@ -12,7 +12,9 @@ sqlalchemy_utils==0.36.8 gino-starlette==0.1.1 # Testing -pytest +openapi-spec-validator +pytest; python_version != '3.9' +pytest !=7.0.0; python_version == '3.9' pytest-virtualenv requests asynctest From f1ed7a0c687fcc71daa3fb1306637700038ecccd Mon Sep 17 00:00:00 2001 From: Dan Lipsitt <578773+DanLipsitt@users.noreply.github.com> Date: Tue, 22 Mar 2022 18:27:55 -0700 Subject: [PATCH 3/3] Use test client as session in schemathesis test function This change was suggested by @Stranger6667 --- tests/test_schemathesis.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/tests/test_schemathesis.py b/tests/test_schemathesis.py index e4cb8aaa..813b2842 100644 --- a/tests/test_schemathesis.py +++ b/tests/test_schemathesis.py @@ -20,6 +20,6 @@ def app_schema(client) -> BaseOpenAPISchema: @schema.parametrize() -def test_api(case): +def test_api(case, client): """Run tests automatically generated by schemathesis.""" - case.call_and_validate() + case.call_and_validate(session=client)