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.
To run contract tests:
class TestContract:
pass
Specmatic.test(TestContract, app_host, app_port, contract_file_path=service_contract_file)
- 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. - The
Specmatic.test()
method accepts:- an empty test class
- the host and port on which your app/service is currently running
- path to an open api spec file which defines and describes the service end points
- 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.
Setting up Stubs
In many cases, your app/service might require calling another api to perform its operations.
Specmatic provides you with an option of creating a stub for such service dependencies and setup expectations on it (like you would do in any of the mocking frameworks).
To create a stub:
stub = Specmatic.start_stub(stub_host, stub_port, contract_file_path=stub_contract_file)
stub.set_expectations([expectation_json_file])
# run tests ...
stub.stop()
- The
Specmatic.start_stub()
method accepts:- the host and port on which your app/service is currently running (which needs to be stubbed).
- path to an open api spec file for the stubbed service.
- An optional list of json files to set expectations on the stub.
- Click here to learn more about stubbing/service virtualization.
Starting/Stopping a python web app
If you wish to start/stop your python app as part of your tests, here's how you can do it:
-
WSGI Apps:
app_server = Specmatic.start_wsgi_app(app, app_host, app_port)
-
The
Specmatic.start_wsgi_app()
method accepts:- an instance of a wsgi app like flask.
- the host/port to run on
-
ASGI Apps:
app_server = Specmatic.start_asgi_app('app:app', app_host, app_port)
-
The
Specmatic.start_asgi_app()
method accepts:- a string in the 'module:app' format for your asgi app like sanic.
- the host/port to run on.
Putting it all together
This is how it looks when use all these in a single test:
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()
- Check out the Specmatic Order BFF Python repo to see more examples of how to use specmatic with a Flask app.
- Check out the Specmatic Order BFF Python Sanic repo to see more examples of how to use specmatic with a Sanic app.
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.2-py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 190f479cd94ce681da2228c88c7aaca68f638e528be33a610b7455ce75cbe087 |
|
MD5 | 2f2d599332be4d36f96eadf8a25cffda |
|
BLAKE2b-256 | b4ca69e4de9d5cbdc002b7052a9b8aef96df542a9690cfebd3661aff2b3c61db |