existential-birds/beagle / plugins/beagle-elixir/skills/exunit-code-review
exunit-code-review
VerifiedReviews ExUnit test code for proper patterns, boundary mocking with Mox, and test adapter usage. Use when reviewing _test.exs files or test helper configurations.
| File | Kind | Size |
|---|---|---|
| SKILL.md | skill md | 3.4 KB |
| references/exunit-patterns.md | markdown | 3.2 KB |
| references/integration-tests.md | markdown | 4.1 KB |
| references/mox-boundaries.md | markdown | 3.3 KB |
| references/test-adapters.md | markdown | 3.8 KB |
references/exunit-patterns.md
View on GitHub# ExUnit Patterns
## Async Tests
### Default to Async
```elixir
# GOOD - isolated test
defmodule MyApp.CalculatorTest do
use ExUnit.Case, async: true
test "adds numbers" do
assert Calculator.add(1, 2) == 3
end
end
```
### When to Disable Async
```elixir
# Sharing database with other tests
defmodule MyApp.UserTest do
use MyApp.DataCase # Sets async: false if needed
test "creates user" do
assert {:ok, _} = Accounts.create_user(%{email: "test@example.com"})
end
end
```
## Describe Blocks
### Group Related Tests
```elixir
defmodule MyApp.UserTest do
use MyApp.DataCase
describe "create_user/1" do
test "with valid attrs creates user" do
assert {:ok, user} = Accounts.create_user(valid_attrs())
assert user.email == "test@example.com"
end
test "with invalid email returns error" do
assert {:error, changeset} = Accounts.create_user(%{email: "invalid"})
assert "is invalid" in errors_on(changeset).email
end
test "with duplicate email returns error" do
Accounts.create_user(valid_attrs())
assert {:error, changeset} = Accounts.create_user(valid_attrs())
assert "has already been taken" in errors_on(changeset).email
end
end
describe "authenticate_user/2" do
# ...
end
end
```
## Setup
### Shared Setup
```elixir
defmodule MyApp.PostTest do
use MyApp.DataCase
setup do
user = insert(:user)
{:ok, user: user}
end
test "creates post for user", %{user: user} do
assert {:ok, post} = Posts.create_post(user, %{title: "Test"})
assert post.user_id == user.id
end
end
```
### Setup per Describe
```elixir
describe "admin functions" do
setup do
admin = insert(:user, role: :admin)
{:ok, admin: admin}
end
test "admin can delete", %{admin: admin} do
# ...
end
end
describe "user functions" do
setup do
user = insert(:user, role: :user)
{:ok, user: user}
end
test "user cannot delete", %{user: user} do
# ...
end
end
```
## Tags
### Skip Tests
```elixir
@tag :skip
test "not implemented yet" do
end
# Run: mix test --exclude skip
```
### Slow Tests
```elixir
@tag :slow
test "integration with external service" do
end
# Run: mix test --exclude slow
# Or: mix test --only slow
```
### Custom Tags
```elixir
@tag :integration
test "full workflow" do
end
# In test_helper.exs
ExUnit.configure(exclude: [:integration])
# Run: mix test --include integration
```
## Assertions
### Pattern Matching
```elixir
# GOOD - precise matching
assert {:ok, %User{email: "test@example.com"}} = Accounts.create_user(attrs)
# GOOD - extract for further assertions
assert {:ok, user} = Accounts.create_user(attrs)
assert user.email == "test@example.com"
assert user.confirmed_at == nil
```
### Refute
```elixir
test "does not include deleted" do
refute deleted_user in Accounts.list_active_users()
end
```
### Assert Raise
```elixir
test "raises on invalid input" do
assert_raise ArgumentError, ~r/must be positive/, fn ->
Calculator.sqrt(-1)
end
end
```
## Review Questions
1. Are tests async when possible?
2. Are describe blocks used to group related tests?
3. Is setup used to reduce duplication?
4. Are assertions using pattern matching effectively?
Related skills
| Skill | Stars | Updated | Works with | Status | Install |
|---|---|---|---|---|---|
| finishing-a-development-branchobra/superpowersUse when implementation is complete, all tests pass, and you need to decide how to integrate the work | Stars: 321k | Updated | Claude CodeCodex | Verified | Install |
| systematic-debuggingobra/superpowersUse when encountering any bug, test failure, or unexpected behavior, before proposing fixes | Stars: 321k | Updated | Claude CodeCodex | Verified | Install |
| test-driven-developmentobra/superpowersUse when implementing any feature or bugfix, before writing implementation code | Stars: 321k | Updated | Claude CodeCodex | Verified | Install |
| skill-creatoranthropics/skillsCreate new skills, modify and improve existing skills, and measure skill performance. Use when users want to create a skill from scratch, edit, or optimize an existing skill, run evals to test a skill, benchmark skill performance with variance analysis, or optimize a skill's description for better triggering accuracy. | Stars: 197k | Updated | Claude CodeCodex | Verified | Install |