Skip to main content

Testing

This guide covers the testing infrastructure and practices for CoW Protocol Services.
Always use cargo nextest run instead of cargo test. The CI environment uses nextest, which handles global state differently. Tests that pass locally with cargo test may fail in CI.

Test Categories

Unit Tests

Validate individual functions without external dependencies.

Database Tests

Require PostgreSQL and must run serially to avoid race conditions. Prerequisites:
  1. Start Docker Compose: docker compose up -d db
  2. Use the --test-threads 1 flag

End-to-End Tests

Two types of E2E tests:

Local Node Tests

Deploy contracts on a clean Ethereum node:

Forked Network Tests

Require setting environment variables for RPC endpoints:

Documentation Tests

Verify code examples in comments stay current:

Driver Tests

Require increased stack size due to complex simulation requirements:

Running Tests Locally

Summary of just commands for each test type:

Best Practices

  • Keep tests fast
  • Test one behavior per test
  • Use descriptive names like test_order_fails_when_expired
  • Avoid flaky tests dependent on timing or external state
  • Write tests before implementation when possible

Troubleshooting

Using cargo test Instead of cargo nextest

Tests may pass with cargo test but fail in CI. Always use cargo nextest run.

Database Connection Problems

Ensure Docker Compose is running:

Missing RPC URLs for Forked Tests

Set the required environment variables:
Last modified on March 4, 2026