./dev test command provides a unified interface for running tests.
Running Tests
Basic Test Commands
Use the./dev test command to run tests:
Common Test Scenarios
Running Fast Unit Tests
Running Fast Unit Tests
Running Slow Integration Tests
Running Slow Integration Tests
Some packages have slow tests (SQL tests can take 10+ minutes):
The KV and SQL packages have particularly comprehensive test suites. Use filters to run specific tests during development.
Running Logic Tests
Running Logic Tests
Logic tests are SQL correctness tests:
Stress Testing
Stress Testing
Detect race conditions and flaky tests:
Test Options
View all available options:Verbose Output
Short Mode
testing.Short()).Race Detector
Test Coverage
Test Infrastructure
Test Types
CockroachDB uses several types of tests:Logic Tests
SQL correctness tests in
pkg/sql/logictest/testdata/:- Test SQL semantics and correctness
- Compare output against expected results
- Run in various configurations (local, distributed, multi-region)
Data-Driven Tests
Tests using the
datadriven framework:- Test data in
testdata/directories - Allows easy addition of test cases
- Common in SQL optimizer and KV tests
Test Utilities
CockroachDB provides testing utilities inpkg/testutils/:
Test Server
Create lightweight test clusters:
SQL Utilities
Execute SQL in tests:
Assertions
Common test assertions:
Cleanup
Automatic cleanup:
Code Generation
Many CockroachDB components rely on generated code:What Gets Generated?
Protocol Buffers
.proto files → .pb.go filesUsed for serialization and RPC.SQL Parser
Grammar files → Parser codeGenerates the SQL parser.
SQL Optimizer
Optgen rules → Optimizer codeGenerates optimizer transformations.
Stringer
Type definitions → String methodsGenerates
String() methods for enums.Generating Code
Use./dev generate to regenerate code:
When to Regenerate
Regenerate code when you modify:.protofiles (protobuf definitions)sql.yorsql.bnf(SQL grammar)*.optfiles (optimizer rules)- Go dependencies (run
./dev generate bazel) - Stringer annotations
Writing Effective Tests
Best Practices
Test at the Right Level
- Use unit tests for logic and edge cases
- Use integration tests for component interaction
- Use logic tests for SQL correctness
- Use roachtests for distributed scenarios
Use Testdata Files
For complex test cases, use data-driven tests:Easier to add cases and review changes.
SQL Logic Tests
When adding SQL features, add logic tests:Continuous Integration
CI Test Runs
When you open a PR, several CI checks run:- Unit Tests: All package tests
- Logic Tests: SQL correctness tests
- Roachtests: Distributed integration tests (subset)
- Linters: Code style and quality checks
- Build Tests: Ensure code builds on all platforms
Handling CI Failures
Test Failures
Test Failures
If tests fail in CI:
- Reproduce locally:
./dev test pkg/failing/test -f=TestName -v - Fix the issue
- Verify fix:
./dev test pkg/failing/test --count=10 - Push the fix
Flaky Tests
Flaky Tests
If a test fails intermittently:
- Report it by opening an issue
- Tag with
A-flaky-test - Include CI logs and failure details
- Consider adding stress testing
Lint Failures
Lint Failures
Fix formatting and style issues:
Debugging Tests
Common Debugging Techniques
Next Steps
Building from Source
Learn how to build CockroachDB
Code Structure
Understand the codebase organization
Contributing Guide
Read the full contribution guidelines
Test Engineering
Advanced testing documentation on wiki