| name | elixir-test-runner |
| description | Run ExUnit tests with smart filtering, debugging options, and proper error reporting. Use when running tests, debugging failures, or validating specific test cases. |
| allowed-tools | Bash, Read, Grep |
Elixir Test Runner
This skill helps run ExUnit tests efficiently with proper filtering, debugging, and error analysis.
When to Use
- Running full test suite
- Testing specific files or tests
- Debugging test failures
- Running tests with coverage
- Testing in different environments
Basic Test Execution
Run All Tests
mix test
Run Specific Test File
mix test test/my_app/accounts_test.exs
Run Specific Test by Line Number
mix test test/my_app/accounts_test.exs:42
Run Tests Matching Pattern
mix test --only user
Test Filtering
By Tag
# In test file
@tag :integration
test "complex integration test" do
# ...
end
mix test --only integration
mix test --exclude slow
mix test --exclude integration
By Module Pattern
mix test test/**/controllers/*_test.exs
mix test test/**/*_live_test.exs
Umbrella App Filtering
mix test apps/my_app/test
mix test --only apps
Debugging Options
With Trace (Detailed Output)
mix test --trace
Shows each test as it runs - useful for hanging tests.
With Verbose Failures
mix test --max-failures 1
Stops after first failure for faster debugging.
With Test Seed
mix test --seed 123456
With IEx for Debugging
mix test --trace
Test Output Control
Show Only Failures
mix test --failed
Reruns only previously failed tests.
Stale Tests
mix test --stale
Only runs tests for changed files.
With Coverage
mix test --cover
Generates coverage report in cover/ directory.
Quiet Mode
mix test --quiet
Less verbose output.
Test Environments
Standard Test Run
mix test
Uses MIX_ENV=test automatically.
With Database Reset
mix ecto.reset && mix test
Fresh database for each run.
CI Mode
mix test --warnings-as-errors --cover
Common Test Patterns
Test Suite Organization
Unit Tests (fast, isolated):
mix test test/my_app/accounts/user_test.exs
Integration Tests (slower, with database):
mix test --only integration
Controller Tests:
mix test test/my_app_web/controllers/
LiveView Tests:
mix test test/my_app_web/live/
Parallel Testing
# In test_helper.exs - already default
ExUnit.start()
# In DataCase
use MyApp.DataCase, async: true # Parallel execution
mix test --max-cases 8
Analyzing Test Failures
Read Failure Output Carefully
Example failure:
1) test creates user with valid attrs (MyApp.AccountsTest)
test/my_app/accounts_test.exs:42
** (RuntimeError) Database not started
What to check:
- Test name: "creates user with valid attrs"
- Module: MyApp.AccountsTest
- File and line: test/my_app/accounts_test.exs:42
- Error: Database not started
Common Failure Patterns
Database Not Started:
mix ecto.create
MIX_ENV=test mix ecto.migrate
Async Test Conflicts:
# Change to synchronous if tests conflict
use MyApp.DataCase, async: false
Missing Setup:
# Check for missing setup block
setup do
:ok = Ecto.Adapters.SQL.Sandbox.checkout(Repo)
end
Factory/Fixture Issues:
mix test test/support/fixtures.exs --trace
Performance Optimization
Identify Slow Tests
mix test --trace | grep -E "^\s+test.*\([0-9]+\.[0-9]+s\)"
Profile Test Suite
mix test --profile
Parallel Execution
# Enable async for fast unit tests
use MyApp.DataCase, async: true
# Disable for integration tests
use MyApp.DataCase, async: false
Test Coverage
Generate Coverage Report
mix test --cover
View report: cover/excoveralls.html
Coverage Configuration
# In mix.exs
def project do
[
test_coverage: [tool: ExCoveralls],
preferred_cli_env: [
coveralls: :test,
"coveralls.detail": :test,
"coveralls.post": :test,
"coveralls.html": :test
]
]
end
With ExCoveralls
mix coveralls.html
mix coveralls.detail
mix coveralls --min-coverage 80
CI/CD Integration
GitHub Actions
- name: Run tests
run: mix test --warnings-as-errors --cover
GitLab CI
test:
script:
- mix ecto.create
- mix ecto.migrate
- mix test --cover
Pre-commit Hook
#!/bin/bash
mix test --failed || exit 1
Troubleshooting
Tests Hang
mix test --trace
Tests Pass Locally, Fail in CI
Common causes:
- Missing MIX_ENV=test
- Database not created
- Dependencies not fetched
- Different Elixir/OTP versions
- Async test conflicts
Debug:
MIX_ENV=test mix do deps.get, ecto.create, ecto.migrate, test
Flaky Tests
mix test test/my_app/accounts_test.exs:42 --trace
mix test test/my_app/accounts_test.exs:42 --trace
mix test test/my_app/accounts_test.exs:42 --trace
Memory Issues
elixir --erl "+hms 4294967296" -S mix test
Best Practices
- Run tests frequently during development
- Use --stale for fast feedback loop
- Tag slow tests and exclude during dev
- Fix failures immediately - don't accumulate
- Use --trace when debugging
- Run full suite before committing
- Check coverage for critical code paths
- Keep tests fast - mock external services
- Use factories for consistent test data
- Run in CI to catch environment issues
Quick Reference
mix test --stale
mix test test/my_app/file_test.exs
mix test --failed
mix test --trace
mix test --max-failures 1
mix test --seed 123456
mix test --cover
mix coveralls.html
mix test --only integration
mix test --exclude slow
mix test --warnings-as-errors --cover