Writing the functional test

First, we will declare imports in the tests.py file. Nameko has good support testing and provides the worker_factory function, which causes the elements identified by Nameko to be performed without using a real server:

import osimport pytestfrom .service import Commandfrom nameko.testing.services import worker_factoryfrom sqlalchemy import create_enginefrom sqlalchemy.orm import sessionmaker

After the imports declaration, we'll create a fixture to connect to the test database. For this, we are using PyTest. With this fixture, Nameko injects this connection to the desired element:

@pytest.fixture def session(): db_engine = create_engine(os.environ.get('COMMANDDB_TEST_HOST')) Session = sessionmaker(db_engine) return ...

Get Microservice Patterns and Best Practices now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.