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+C to stop watching

  • Monitors both src/ and spec/ directories

Filtering Tests

Filter tests by name or pattern to run specific test cases:

Options

Option
Short
Description

--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:

Variable
Description
Default

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:

  1. Displays changed file path

  2. Shows separator line for visual clarity

  3. Reruns the test suite

  4. Displays results

  5. Waits for next change

Monitored file patterns:

  • src/**/*.cr - Source files

  • spec/**/*.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:

  1. Use a separate test database

  2. Set CRYSTAL_ENV=test

  3. Run 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 directories

  • test_*.cr - Test script files (outside spec/)

  • /123*/ and /*invalid*/ - Invalid names

Always clean up after manual testing:

Troubleshooting

Tests Not Running

Ensure your test files:

  • Are in the spec/ directory

  • End with _spec.cr

  • Require spec_helper

Watch Mode Not Detecting Changes

Check that:

  • Files are in src/ or spec/ directories

  • Files have .cr extension

  • File 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:

See Also

Last updated