Skip to main content

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

Specmatic - 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)

Uploaded Source

Built Distribution

specmatic-0.7.5-py3-none-any.whl (79.1 MB view hashes)

Uploaded Python 3

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page