azu test
The azu test command runs your application's test suite with support for watch mode, filtering, and continuous testing.
Synopsis
azu test [files] [options]Description
Execute your application's test suite using Crystal's built-in spec framework. The command provides a wrapper around crystal spec with additional features like file watching, filtering, and enhanced output formatting.
Usage
Basic Testing
# Run all tests
azu test
# Run specific test file
azu test spec/models/user_spec.cr
# Run all tests in a directory
azu test spec/models/Watch Mode
The watch mode automatically reruns tests when source or spec files change, providing immediate feedback during development:
When running in watch mode:
Tests run initially on startup
File changes trigger automatic test reruns
Press
Ctrl+Cto stop watchingMonitors both
src/andspec/directories
Filtering Tests
Filter tests by name or pattern to run specific test cases:
Options
--watch
-w
Watch mode - automatically rerun tests on file changes
--coverage
-c
Enable coverage reporting (requires additional setup)
--verbose
-v
Verbose output with detailed test information
--parallel
-p
Run tests in parallel (if supported by test framework)
--filter <pattern>
-f
Filter tests by name pattern
Environment Variables
The test command respects the following environment variables:
CRYSTAL_ENV
Test environment
test
DATABASE_URL
Test database URL
Examples
Development Workflow
Run tests in watch mode during development:
Focused Testing
Run specific tests with verbose output:
Pre-Commit Testing
Run all tests before committing:
Test File Structure
The test command works with the standard Crystal spec structure:
Output Format
Test output includes:
Test Results: Pass/fail indicators for each test
Duration: Total test execution time
Errors: Detailed error messages and stack traces
Summary: Total tests, passed, failed counts
Example output:
Watch Mode Behavior
When file changes are detected:
Displays changed file path
Shows separator line for visual clarity
Reruns the test suite
Displays results
Waits for next change
Monitored file patterns:
src/**/*.cr- Source filesspec/**/*.cr- Spec files
Coverage Reporting
Coverage reporting requires additional setup:
Note: Coverage reporting may require installing additional tools like crystal-coverage.
Performance Considerations
Parallel Testing
Enable parallel test execution for faster results:
Note: Parallel testing requires proper test isolation and may not be supported by all test frameworks.
Test Database
For tests using the database:
Use a separate test database
Set
CRYSTAL_ENV=testRun migrations before tests:
Best Practices
1. Use Watch Mode During Development
2. Keep Tests Fast
Use factories instead of fixtures
Mock external dependencies
Minimize database operations
3. Organize Tests Logically
4. Use Filters for Focused Development
5. Run Full Suite Before Commits
6. Test in Temporary Directories
When testing CLI commands that generate files or projects, always use temporary directories:
Manual testing should also use temporary directories:
Patterns automatically ignored by .gitignore:
/test_*/- Test projects/tmp_*/- Temporary projects/playground_*/- Playground projects*_test_project/- Test project directoriestest_*.cr- Test script files (outsidespec/)/123*/and/*invalid*/- Invalid names
Always clean up after manual testing:
Troubleshooting
Tests Not Running
Ensure your test files:
Are in the
spec/directoryEnd with
_spec.crRequire
spec_helper
Watch Mode Not Detecting Changes
Check that:
Files are in
src/orspec/directoriesFiles have
.crextensionFile system permissions are correct
Parallel Tests Failing
If parallel tests fail but sequential tests pass:
Check for shared state between tests
Ensure proper test isolation
Use separate database connections
Database Connection Errors
Verify test database setup:
Integration with CI/CD
GitHub Actions Example
GitLab CI Example
Aliases
The test command can also be invoked using:
Related Commands
azu serve- Development server for running applicationazu db:migrate- Run database migrationsazu generate- Generate test files with scaffolds
See Also
Last updated