Testing a microservice with PyMS is very similar to a Flask Application.
The recommended way to start is using Create App Pattern. Using PyMS is like Flask:
from pyms.flask.app import Microservice
def create_app():
ms = Microservice(path=__file__)
return ms.create_app()Then, you can use these functions in your conftest.py with Pytest:
PyMS needs the environment variable PYMS_CONFIGMAP_FILE to look for the configuration file, is recommended
to create a specific config file to testing:
import os
def conf_environment():
if not os.environ.get("PYMS_CONFIGMAP_FILE", False):
os.environ["PYMS_CONFIGMAP_FILE"] = os.path.join(os.path.dirname(os.path.abspath(__file__)), "config-tests.yml")Create an app object to use in tests
import pytest
@pytest.fixture(scope="session")
def app():
conf_environment()
app = create_app()
return appCreate Flask testing client
@pytest.fixture(scope="module")
def client(app, db):
"""A test client for the app."""
return app.test_client()Get the url to use in tests
@pytest.fixture(scope="module")
def base_url(app):
"""Base url of the service."""
return app.config["APPLICATION_ROOT"]()With this configuration, you are ready to create your own tests with PyMS:
def test_list_actors(client, base_url):
response = client.get('{base_url}healthcheck'.format(base_url=base_url))
assert 200 == response.status_codeAs you can see, is very similar to a normal Flask application :)