|
| 1 | +import json |
| 2 | + |
| 3 | +import pytest |
| 4 | +import schemathesis |
| 5 | +from schemathesis.checks import ( |
| 6 | + content_type_conformance, |
| 7 | + negative_data_rejection, |
| 8 | + not_a_server_error, |
| 9 | + response_headers_conformance, |
| 10 | + response_schema_conformance, |
| 11 | + status_code_conformance, |
| 12 | +) |
| 13 | + |
| 14 | +schemathesis.experimental.OPEN_API_3_1.enable() |
| 15 | + |
| 16 | +SCHEMA_URL = "https://raw.githubusercontent.com/stapi-spec/stapi-spec/refs/heads/main/openapi.yaml" |
| 17 | +schema = schemathesis.from_uri(SCHEMA_URL) |
| 18 | + |
| 19 | +BASE_URL = "http://localhost:8000" |
| 20 | + |
| 21 | + |
| 22 | +@schema.parametrize() |
| 23 | +def test_api(case): |
| 24 | + response = case.call_and_validate(base_url=BASE_URL) |
| 25 | + case.validate_response(response) |
| 26 | + |
| 27 | + not_a_server_error(response, case) |
| 28 | + status_code_conformance(response, case) |
| 29 | + content_type_conformance(response, case) |
| 30 | + response_schema_conformance(response, case) |
| 31 | + response_headers_conformance(response, case) |
| 32 | + negative_data_rejection(response, case) |
| 33 | + |
| 34 | + |
| 35 | +def test_openapi_specification(): |
| 36 | + assert schema.validate() |
| 37 | + |
| 38 | + |
| 39 | +@pytest.hookimpl(tryfirst=True, hookwrapper=True) |
| 40 | +def pytest_runtest_makereport(item, call): |
| 41 | + outcome = yield |
| 42 | + rep = outcome.get_result() |
| 43 | + if rep.when == "call": |
| 44 | + item.session.results = getattr(item.session, "results", {}) |
| 45 | + item.session.results[item.nodeid] = rep |
| 46 | + |
| 47 | + |
| 48 | +def pytest_sessionfinish(session, exitstatus): |
| 49 | + if hasattr(session, "results"): |
| 50 | + with open("test_results.json", "w") as f: |
| 51 | + json.dump( |
| 52 | + { |
| 53 | + nodeid: { |
| 54 | + "outcome": rep.outcome, |
| 55 | + "longrepr": str(rep.longrepr) if rep.longrepr else None, |
| 56 | + } |
| 57 | + for nodeid, rep in session.results.items() |
| 58 | + }, |
| 59 | + f, |
| 60 | + indent=2, |
| 61 | + ) |
0 commit comments