Skip to main content
Kit provides testing utilities that make it easy to write tests for your actions, services, and controllers with full database support using in-memory SQLite. Kit also offers Jest-like testing macros for better test organization and clearer failure output.

Quick Start

The simplest way to write a test with database support using Jest-like syntax:
Or using the traditional attribute macro:
Kit provides Jest-like macros for better test organization and clearer assertion output.

The describe! Macro

Group related tests with descriptive names:

The test! Macro

Define individual test cases with three syntax options:

The expect! Macro

Fluent assertions with clear failure output:

Clear Failure Output

When an assertion fails, you get clear output with the test name:

Testing Approaches (Traditional)

Kit also provides traditional ways to write database-enabled tests: The #[kit_test] attribute macro is the cleanest way to write tests:

2. Helper Macro

For more control, use the test_database! macro:

How It Works

When you use #[kit_test]:
  1. Services Bootstrapped: All services marked with #[injectable] are automatically registered, so App::resolve::<T>() works just like in production
  2. Fresh Database: A new in-memory SQLite database is created for each test
  3. Migrations Applied: Your crate::migrations::Migrator runs automatically
  4. Automatic Integration: The test database is registered in the DI container, so any code using DB::connection() or #[inject] db: Database automatically uses the test database
  5. Complete Isolation: Each test is fully isolated - no data leaks between tests
The #[kit_test] macro calls App::init() and App::boot_services() before your test runs, ensuring all injectable services are available.

Testing Actions

Actions marked with #[injectable] can be resolved from the container in tests:

Custom Migrator

By default, both macros use crate::migrations::Migrator. If your migrator is in a different location:

Direct Database Access

The TestDatabase struct provides methods for direct database queries:

Test Without Database Parameter

If you don’t need direct database access in your test but still want the database set up:

Best Practices

1. One Assertion Per Test

Keep tests focused on a single behavior:

2. Test Edge Cases

3. Use Factories for Test Data

Create helper functions to generate test data:

Running Tests

Run your tests using cargo: