Skip to main content
This guide explains how to write and run tests for SnackBase, covering unit tests, integration tests, and best practices.

Overview

SnackBase uses pytest as its testing framework with comprehensive support for async operations and database testing.

Test Stack

Test Coverage Goals

Testing Philosophy

Test Pyramid

Testing Principles

Test Structure

Directory Layout

Test Naming Convention

Use descriptive test names that explain what is being tested:

Running Tests

Basic Commands

Coverage Report

Test Discovery

Pytest automatically discovers tests:

Writing Unit Tests

What to Unit Test

Unit tests should cover:
  • Business logic (domain layer)
  • Pure functions (no side effects)
  • Data transformations
  • Validation logic

Example: Testing ID Generator

Example: Testing Rule Parser

Writing Integration Tests

What to Integration Test

Integration tests should cover:
  • API endpoints (request/response)
  • Database operations (CRUD)
  • Authentication flows
  • Permission enforcement
  • Hook execution

Example: Testing POST Endpoint

Example: Testing Permissions

Test Fixtures

Fixtures provide reusable test setup.

Available Fixtures

Using Fixtures

Async Testing

Marking Async Tests

Use @pytest.mark.asyncio for async test functions:

Async Test Classes

Best Practices

1. Arrange-Act-Assert Pattern

Structure tests clearly:

2. Use Descriptive Assertions

3. Test Edge Cases

4. Mock External Dependencies

5. Use Factory Boy for Test Data

6. Run Tests Frequently

Summary