Why niltest?
When behavior is copied across implementation, tests, mocks, and a wiki, every change requires synchronization. niltest brings small behavioral examples back beside the implementation.
- See representative inputs and results where the function is defined
- Develop without a database or external API by returning fixed examples
- Check the real implementation with those same cases
The core pattern in 30 seconds
Add @scenario, declare cases inside if expect:, then continue with an ordinary Python implementation.
python
import niltest
from niltest import Mode, expect, scenario
niltest.configure(mode=Mode.TEST) # @scenarioより前
@scenario("配送料")
def shipping_fee(subtotal: int, premium: bool = False) -> int:
if expect:
expect.case(
"プレミアム会員は無料",
given={"subtotal": 1_000, "premium": True},
returns=0,
)
return 0 if premium or subtotal >= 5_000 else 500A good fit when you want to
- Share representative behavior for functions that call APIs or databases
- Reduce drift between specifications and tests
- Validate random IDs or tokens by type and conditions
- Make business rules and small Python libraries easier to understand