From b90bae79daf495d4802adc44fdadce3311c6f18e Mon Sep 17 00:00:00 2001 From: Ryan Roden-Corrent Date: Fri, 21 May 2021 07:18:13 -0400 Subject: [PATCH] doc: document that test_ functions may propagate errors (#10162) When testing a function that can return an error, writing a test like this allows you to more easily test the non-error flow by just propagating any errors and letting them fail the test. I didn't realize this was a feature at first, so I was writing tests like: ``` fn test_atoi() { assert strconv.atoi('1') or { panic("atoi failed") } == 1 assert strconv.atoi('one') or { panic("atoi failed") } == 1 } ``` --- doc/docs.md | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/doc/docs.md b/doc/docs.md index 789c4f972c..7c7612ac0a 100644 --- a/doc/docs.md +++ b/doc/docs.md @@ -3101,6 +3101,17 @@ You can also define special test functions in a test file: * `testsuite_begin` which will be run *before* all other test functions. * `testsuite_end` which will be run *after* all other test functions. +If a test function has an error return type, any propagated errors will fail the test: + +``` +import strconv + +fn test_atoi() ? { + assert strconv.atoi('1') ? == 1 + assert strconv.atoi('one') ? == 1 // test will fail +} +``` + #### Running tests To run test functions in an individual test file, use `v foo_test.v`.