name: cairo-testing
description: Explain how to write and run Cairo tests, including #[test], assert macros, #[should_panic], #[ignore], and #[available_gas]; use when a request involves writing or running unit tests in Cairo.
Cairo Testing
Overview
Guide writing test functions, using assertion macros, and running tests with Scarb.
Quick Use
- Read
references/testing.mdbefore answering. - Put tests in a
#[cfg(test)]module insrcfiles. - Use the right assert macro for the expected condition.
Response Checklist
- Add
#[test]above each test function. - Use
assert!,assert_eq!,assert_ne!, or comparison macros as needed. - Assert message arguments must be string literals, not variables (e.g.,
assert!(x, "msg")notassert!(x, msg)). - Use
#[should_panic(expected: "...")]for panic-based tests. - Use
#[ignore]for slow tests andscarb test --include-ignoredwhen needed. - Add
#[available_gas(n)]for recursion or long loops.
Example Requests
- "How do I write a basic test in Cairo?"
- "How do I assert a panic message?"
- "How do I run a single test?"