Testing Python Code with pytest

Testing Python Code with pytest

The best-practices section starts with the streaming model service from Module 4. The first step is to make its behavior testable without deploying the entire cloud architecture.

Separate logic from infrastructure

Keep feature preparation and model loading in ordinary Python methods. The service can then be tested with a small input record and a known model artifact. Calls to Kinesis or another cloud client should stay at the integration boundary.

The stream, Lambda model service, and prediction path

The example tests the feature-preparation function and the model-service behavior with pytest. A test should make its expected input and output explicit, so a failure identifies the behavior that changed.

The model service and its pytest test result

Run pytest tests/ locally so unit tests can run on every change without AWS credentials or a network connection.

Optional analytics

Optional analytics are off unless you allow them. Your choice does not affect access to DataTalks.Club, Slack, events, or courses.

Current choice: not chosen.

Privacy Policy

If you allow analytics, self-hosted Umami counts anonymous public pageviews without analytics cookies. Private account and learning pages are excluded. Read the Privacy Policy for the complete data boundary.