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.

WSGI Apps

To run contract tests with a stub for a wsgi app (like Flask):

  • Create an instance of a WSGIAppServer by passing it your app object, host, port:
    app_server = WSGIAppServer(app, app_host, app_port)
  • Note:
    • The host and port are optional. If they are not specified, the app will be started on a random available port on 127.0.0.1.
    • You would need a specmatic.json file to be present in the root directory of your project.
  • To run tests using a stub for your dependencies:
class TestContract:
    pass
    
    
app_server = WSGIAppServer(app, app_host, app_port)
Specmatic() \
    .with_project_root(PROJECT_ROOT) \
    .stub(stub_host, stub_port, [expectation_json_file]) \
    .app(app_server) \
    .test(TestContract) \
    .run()
  • In this, we are passing:
    • an instance of the WSGIAppServer class.
    • an empty test class.
    • stub_host, stub_port, optional list of json files to set expectations on the stub.
      The stub_host, stub_port will be used to run the specmatic stub server.
      If they are not supplied, the stub will be started on a random available port on 127.0.0.1.
      Click here to learn more about stubbing/service virtualization.
  • 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 without a stub:

class TestContract:
    pass
    
    
app_server = WSGIAppServer(app, app_host, app_port)
Specmatic() \
    .with_project_root(PROJECT_ROOT) \
    .app(app_server) \
    .test(TestContract) \
    .run()

ASGI Apps

To run contract tests with a stub for an asgi app (like sanic):

  • Create an instance of a ASGIAppServer by passing it a string in the 'module:app' format for your asgi app, host, port:
    app_server = ASGIAppServer('main:app', app_host, app_port)
  • You can now use the ASGIAppServer object to run tests just like we do it for WSGI apps:
class TestContract:
    pass
    
    
app_server = ASGIAppServer('test.apps.sanic_app:app', app_host, app_port)
Specmatic() \
    .with_project_root(PROJECT_ROOT) \
    .stub(stub_host, stub_port, [expectation_json_file]) \
    .app(app_server) \
    .with_test_class(TestContract) \
    .test() \
    .run()

Common Issues

  • 'Error loading ASGI app' This error occurs due to incorrect module being specified in the app module parameter 'module:app' string.

    Solutions:

    • Try to identify the correct module in which your app variable is instantiated/imported.
      For example if your 'app' variable is declared in main.py, try passing 'main:app'.
    • Try running the app using uvicorn directly:
      uvciron 'main:app'
      If you are able to get the app started using uvicorn, it will work with specmatic too.

Sample Projects

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.8.0.tar.gz (79.1 MB view hashes)

Uploaded Source

Built Distribution

specmatic-0.8.0-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