exunit
Execute and generate ExUnit tests for Elixir projects with setup callbacks, describe blocks, and async testing support
What this skill does
# ExUnit Test Framework
## Purpose
Provide ExUnit test execution and generation for Elixir projects, supporting:
- Test file generation from templates (_test.exs files)
- Test execution with Mix test integration
- Setup and setup_all callbacks
- Describe blocks for test organization
- Async testing support
## Usage
### Generate Test File
Create a test file from a bug report or feature description:
```bash
elixir generate-test.exs \
--source lib/calculator.ex \
--output test/calculator_test.exs \
--module Calculator \
--description "Division by zero error"
```
### Execute Tests
Run ExUnit tests and return structured results:
```bash
elixir run-test.exs \
--file test/calculator_test.exs \
--format json
```
## Command Line Options
### generate-test.exs
- `--source <path>` - Source file to test (required)
- `--output <path>` - Output test file path (required)
- `--module <name>` - Module name to test (required)
- `--description <text>` - Bug description or test purpose
- `--async` - Enable async testing (default: false)
### run-test.exs
- `--file <path>` - Test file to execute (required)
- `--format <json|doc>` - Output format (default: json)
- `--trace` - Run with detailed trace
## Output Format
### Test Generation
Returns JSON with generated test file information:
```json
{
"success": true,
"testFile": "test/calculator_test.exs",
"testCount": 1,
"template": "unit-test",
"async": false
}
```
### Test Execution
Returns JSON with test results:
```json
{
"success": false,
"passed": 2,
"failed": 1,
"total": 3,
"duration": 0.234,
"failures": [
{
"test": "test divide by zero raises ArithmeticError",
"error": "Expected ArithmeticError to be raised",
"file": "test/calculator_test.exs",
"line": 15
}
]
}
```
## ExUnit Test Structure
### Basic Test
```elixir
defmodule CalculatorTest do
use ExUnit.Case
test "adds two numbers" do
assert Calculator.add(1, 2) == 3
end
end
```
### With Describe Blocks
```elixir
defmodule CalculatorTest do
use ExUnit.Case
describe "add/2" do
test "adds positive numbers" do
assert Calculator.add(1, 2) == 3
end
test "adds negative numbers" do
assert Calculator.add(-1, -2) == -3
end
end
describe "divide/2" do
test "divides numbers" do
assert Calculator.divide(6, 2) == 3
end
test "raises on division by zero" do
assert_raise ArithmeticError, fn ->
Calculator.divide(1, 0)
end
end
end
end
```
### With Setup Callbacks
```elixir
defmodule UserTest do
use ExUnit.Case
setup do
user = %User{name: "John", email: "[email protected]"}
{:ok, user: user}
end
test "user has name", %{user: user} do
assert user.name == "John"
end
test "user has email", %{user: user} do
assert user.email == "[email protected]"
end
end
```
### Async Testing
```elixir
defmodule FastTest do
use ExUnit.Case, async: true
test "runs in parallel with other async tests" do
assert 1 + 1 == 2
end
end
```
## Common Assertions
- `assert expr` - Ensures expression is truthy
- `refute expr` - Ensures expression is falsy
- `assert_raise Exception, fn -> ... end` - Expects exception
- `assert_received message` - Asserts message was received
- `assert x == y` - Equality assertion (preferred over pattern matching)
## Integration with deep-debugger
The deep-debugger agent uses this skill for Elixir projects:
1. **Test Recreation**: Generate failing test from bug report
2. **Test Validation**: Execute test to verify it fails consistently
3. **Fix Verification**: Re-run test after fix to ensure it passes
Example workflow:
```markdown
1. deep-debugger receives bug report for Elixir project
2. Invokes test-detector to identify ExUnit
3. Invokes exunit-test/generate-test.exs to create failing test
4. Invokes exunit-test/run-test.exs to validate test fails
5. Delegates fix to elixir-phoenix-expert agent
6. Invokes exunit-test/run-test.exs to verify fix
```
## Dependencies
Requires Elixir and Mix to be installed:
```bash
elixir --version # Should be 1.12 or higher
mix --version # Elixir's build tool
```
ExUnit is built into Elixir, no additional installation needed.
## File Naming Conventions
- Test files must end with `_test.exs`
- Mirror source file structure: `lib/calculator.ex` → `test/calculator_test.exs`
- Test helper: `test/test_helper.exs` (required)
## Error Handling
### Test Generation Errors
```json
{
"success": false,
"error": "Source file not found",
"file": "lib/missing.ex"
}
```
### Test Execution Errors
```json
{
"success": false,
"error": "Mix test failed",
"output": "** (CompileError) ..."
}
```
## See Also
- [ExUnit Documentation](https://hexdocs.pm/ex_unit/ExUnit.html)
- [Elixir School Testing Guide](https://elixirschool.com/en/lessons/testing/basics)
- [templates/](templates/) - Test file templates
Related in General
modeling-omnistudio-epc-catalog
IncludedSalesforce Industries CME EPC product-modeling skill for Product2-based catalog creation. Use when creating EPC products, configuring product attributes, building offer bundles with Product Child Items, or reviewing EPC DataPack JSON metadata for product catalog changes. TRIGGER when: user creates or updates Product2 EPC records, AttributeAssignment payloads, AttributeMetadata/AttributeDefaultValues, Offer bundles, or ProductChildItem relationships. DO NOT TRIGGER when: designing OmniScripts/FlexCards/Integration Procedures (use building-omnistudio-omniscript, building-omnistudio-flexcard, or building-omnistudio-integration-procedure), implementing Apex business logic (use generating-apex), or troubleshooting deployment pipelines (use deploying-metadata).
relationship-science-coach
IncludedUse this skill for direct, practical adult relationship coaching: couples conflict, repair, trust, marriage, dating, flirting, attachment patterns, emotional connection, sex, desire differences, eroticism, kink negotiation, affection, love languages, breakups, and long-term passion. Draw on Gottman, EFT and Hold Me Tight, attachment science, modern sex research, Perel, Nagoski, Kerner, Schnarch, Love and Stosny, and flexible love-language tools. Be concrete and low-hedge. Redirect only for imminent danger, abuse, coercive control, minors, non-consent, self-harm, stalking, or medical/legal/psychiatric decisions.
building-sf-integrations
IncludedSalesforce integration architecture and runtime plumbing with 120-point scoring. Use this skill to set up Named Credentials, External Credentials, External Services, REST/SOAP callout patterns, Platform Events, and Change Data Capture. TRIGGER when: user sets up Named Credentials, External Services, REST/SOAP callouts, Platform Events, CDC, or touches .namedCredential-meta.xml files. DO NOT TRIGGER when: Connected App/OAuth config (use configuring-connected-apps), Apex-only logic (use generating-apex), or data import/export (use handling-sf-data).
venue-templates
IncludedAccess comprehensive LaTeX templates, formatting requirements, and submission guidelines for major scientific publication venues (Nature, Science, PLOS, IEEE, ACM), academic conferences (NeurIPS, ICML, CVPR, CHI), research posters, and grant proposals (NSF, NIH, DOE, DARPA). This skill should be used when preparing manuscripts for journal submission, conference papers, research posters, or grant proposals and need venue-specific formatting requirements and templates.
let-fate-decide
IncludedDraws the 12 Houses of the Zodiac Tarot spread to inject entropy into planning when prompts are vague, ambiguous, or casually delegated. Interprets the spread to guide next steps. Use when the user says 'let fate decide', 'YOLO', 'whatever', 'idk', or other nonchalant phrases, makes Yu-Gi-Oh references, or when you are about to arbitrarily pick between multiple reasonable approaches. Prefer over ask-questions-if-underspecified when the user's tone is casual or playful rather than precision-seeking.
net-ops
IncludedCross-platform network troubleshooting (Windows, macOS, Linux) via local or remote shell. Use for: DNS broken, can't resolve hostnames, nslookup/dig works but apps fail, NRPT, WFP, scutil, /etc/resolver, systemd-resolved, /etc/resolv.conf, NetworkManager, VPN DNS leak residue (ProtonVPN/Mullvad/WireGuard/AnyConnect), AV/firewall blocking DNS or DoH, Tailscale DNS interaction, intermittent connectivity, remote diagnostics over SSH.