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.7.0-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3e1dc088de48690a5945745b5600935344afd92df34a5cab1ce7633c3e6f2638 |
|
MD5 | f4bd7a1db0f3cec2f2b41efea82a2331 |
|
BLAKE2b-256 | 2b6ccdd0661b7782646949c03f3a5754826e278fd5351f0d0a97e499199948ca |