Supertester.Assertions (supertester v0.1.0)
View SourceCustom assertions for OTP patterns and system behavior.
This module provides specialized assertions that understand OTP patterns, making tests more expressive and providing better error messages.
Usage
import Supertester.Assertions
test "my test" do
assert_process_alive(server_pid)
assert_genserver_responsive(server_pid)
end
Summary
Functions
Asserts that all children of a supervisor are alive.
Asserts that a supervisor has the expected number of children.
Asserts that a GenServer handles a message correctly.
Asserts that a GenServer is responsive.
Asserts that a GenServer has the expected state.
Asserts that memory usage remains stable during an operation.
Asserts that no processes are leaked during an operation.
Asserts that performance is within expected bounds.
Asserts that a process is alive.
Asserts that a process is dead.
Asserts that a process has been restarted.
Asserts that a supervisor has the expected strategy.
Functions
@spec assert_all_children_alive(Supervisor.supervisor()) :: :ok
Asserts that all children of a supervisor are alive.
Parameters
supervisor
- The supervisor pid or name
Example
assert_all_children_alive(supervisor)
@spec assert_child_count(Supervisor.supervisor(), non_neg_integer()) :: :ok
Asserts that a supervisor has the expected number of children.
Parameters
supervisor
- The supervisor pid or nameexpected_count
- The expected number of children
Example
assert_child_count(supervisor, 3)
@spec assert_genserver_handles_message(GenServer.server(), term(), term()) :: :ok
Asserts that a GenServer handles a message correctly.
Parameters
server
- The GenServer pid or namemessage
- The message to sendexpected_response
- The expected response
Example
assert_genserver_handles_message(server, :get_counter, {:ok, 5})
@spec assert_genserver_responsive(GenServer.server()) :: :ok
Asserts that a GenServer is responsive.
Parameters
server
- The GenServer pid or name
Example
assert_genserver_responsive(server)
@spec assert_genserver_state(GenServer.server(), term() | (term() -> boolean())) :: :ok
Asserts that a GenServer has the expected state.
Parameters
server
- The GenServer pid or nameexpected_state
- The expected state or a function to test the state
Example
assert_genserver_state(server, %{counter: 5})
assert_genserver_state(server, fn state -> state.counter > 0 end)
Asserts that memory usage remains stable during an operation.
Parameters
operation_fun
- Function to executetolerance
- Memory tolerance as a percentage (default: 0.1)
Example
assert_memory_usage_stable(fn ->
Enum.each(1..1000, fn _ -> GenServer.call(server, :increment) end)
end, 0.05)
@spec assert_no_process_leaks((-> any())) :: :ok
Asserts that no processes are leaked during an operation.
Parameters
operation_fun
- Function to execute
Example
assert_no_process_leaks(fn ->
{:ok, server} = GenServer.start_link(MyServer, [])
GenServer.stop(server)
end)
Asserts that performance is within expected bounds.
Parameters
benchmark_result
- Result from performance testingexpectations
- Map of performance expectations
Example
expectations = %{max_time: 1000, max_memory: 1_000_000}
assert_performance_within_bounds(result, expectations)
@spec assert_process_alive(pid()) :: :ok
Asserts that a process is alive.
Parameters
pid
- The process ID to check
Example
assert_process_alive(server_pid)
@spec assert_process_dead(pid()) :: :ok
Asserts that a process is dead.
Parameters
pid
- The process ID to check
Example
assert_process_dead(old_pid)
Asserts that a process has been restarted.
Parameters
process_name
- The registered name of the processoriginal_pid
- The original PID before restart
Example
original_pid = GenServer.whereis(MyServer)
GenServer.stop(MyServer)
assert_process_restarted(MyServer, original_pid)
@spec assert_supervisor_strategy(Supervisor.supervisor(), atom()) :: :ok
Asserts that a supervisor has the expected strategy.
Parameters
supervisor
- The supervisor pid or nameexpected_strategy
- The expected supervision strategy
Example
assert_supervisor_strategy(supervisor, :one_for_one)