A Python module for using the Specmatic Library.
Project description
Specmatic Python
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.
Click below to learn more about Specmatic and Contract Driven Development
The specmatic python library 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 an open api contract/spec.
- The ability to stub out an api dependency using the specmatic stub feature.
Running Contract Tests
A contract test validates an open api specification against a running api service.
The open api specification can be present either locally or in a Central Contract Repository
Click here to learn more about contract tests.
How to use
- Create a file called test_contract.py in your test folder.
- Declare an empty class in it called 'TestContract'. Specmatic will use this class to inject tests dynamically into it when you run it via say PyTest.
To run contract tests with a stub for a wsgi app (like Flask):
class TestContract:
pass
Specmatic.test_wsgi_app(app,
TestContract,
app_contracts=[app_contract_file],
stub_contracts=[stub_contract_file],
app_host=app_host,
app_port=app_port,
stub_host=stub_host,
stub_port=stub_port,
expectation_files=[expectation_json_file])
- The
Specmatic.test_wsgi_app()
method accepts:- an instance of a wsgi app like class
- an empty test class
- app_host, app_port: the host and port on which your app/service is currently running
- a list of open api spec file paths which define and describe your app end points
- stub_host, stub_port: the host and port on which your dependency service is currently running (which needs to be stubbed).
- a list of open api spec file paths for the stubbed service.
- An optional list of json files to set expectations on the stub.
- You can run this test from either your IDE or command line by pointing pytest to your test folder :
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.
To run contract tests with a stub for an asgi app (like sanic):
Specmatic.test_asgi_app('main:app',
TestContract,
app_contracts=[app_contract_file],
stub_contracts=[stub_contract_file],
app_host=app_host,
app_port=app_port,
stub_host=stub_host,
stub_port=stub_port,
expectation_files=[expectation_json_file])
- The
Specmatic.start_asgi_app()
method accepts:- a string in the 'module:app' format for your asgi app like sanic.
- the rest of the arguments are similar to that of the
Specmatic.test_wsgi_app()
method.
- [Click here](https://specmatic.in/documentation/service_virtualization_tutorial.html) to learn more about stubbing/service virtualization. - [Check out the Specmatic Order BFF Python repo](https://github.com/znsio/specmatic-order-bff-python/) to see more examples of how to use specmatic with a Flask app. - [Check out the Specmatic Order BFF Python Sanic repo](https://github.com/znsio/specmatic-order-bff-python-sanic/) to see more examples of how to use specmatic with a Sanic app. - [Check out the Specmatic Order API Python repo](https://github.com/znsio/specmatic-order-api-python/) to see an examples of how to just run tests without using a stub.
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
specmatic-0.7.5.tar.gz
(79.1 MB
view hashes)
Built Distribution
specmatic-0.7.5-py3-none-any.whl
(79.1 MB
view hashes)
Close
Hashes for specmatic-0.7.5-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 295000a4b5847461bfbb0267ec46f4291f7fd8c9d197086ae5167cd571531876 |
|
MD5 | f7fa51dcd91f050baa05c759f33b675b |
|
BLAKE2b-256 | 17405f5ecc5715413f843874d892683665b87b6cb24e46697b01491f85ca9863 |