A Python module for using the Specmatic Library.
Project description
This is a Python library to run Specmatic.
Specmatic is a contract driven development tool that allows us to turn OpenAPI contracts into executable specifications.
-
The specmatic python library is geared towards integrating Specmatic capabilities in PyTest tests.
It provides three main functions:- The ability to start and stop a python web app like flask/sanic.
- The ability to run specmatic in test mode against a local contract/spec file, or against a spec defined in a Central Contract Repository (as defined in specmatic.json).
- The ability to stub out an api dependency using the specmatic stub feature.
-
To run Specmatic against a wsgi app like flask:
class TestContract:
pass
stub = None
app_server = None
try:
stub = Specmatic.start_stub(stub_host, stub_port, contract_file_path=stub_contract_file)
stub.set_expectations([expectation_json_file])
app_server = Specmatic.start_wsgi_app(app, app_host, app_port)
Specmatic.test(TestContract, app_host, app_port, contract_file_path=service_contract_file)
except Exception as e:
print(f"Error: {e}")
raise e
finally:
if app_server is not None:
app_server.stop()
if stub is not None:
stub.stop()
if __name__ == '__main__':
pytest.main()
-
Specify absolute paths to the 'contract_file_path' parameter to point to the respective open api specification file.
-
Run this test class either from your IDE or from command line:
pytest test -v -s
-
NOTE: Please ensure that you set the '-v' and '-s' flags while running pytest as otherwise pytest may swallow up the console output.
-
Check out this repo to see more examples of how to use specmatic with a Flask app.
-
Click here to learn more about Specmatic tests.
-
To run Specmatic against an awsgi app like sanic, just replace the 'app_server' creation code above with:
app_server = Specmatic.start_asgi_app(app, app_host, app_port)
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Hashes for specmatic-0.5.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | b23ee72d8a124c71993bc69dcc108f0cca5190c3c083d1c065a5acd6d0521f9e |
|
MD5 | e62c371003e89e34585007327391a5db |
|
BLAKE2b-256 | dec5807fec04ad94a3bde582ff51bd1400d8debe6a5d8da8c395ffcae0122be6 |