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 } ```pull/10163/head
parent
04ea2824d3
commit
b90bae79da
11
doc/docs.md
11
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_begin` which will be run *before* all other test functions.
|
||||||
* `testsuite_end` which will be run *after* 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
|
#### Running tests
|
||||||
|
|
||||||
To run test functions in an individual test file, use `v foo_test.v`.
|
To run test functions in an individual test file, use `v foo_test.v`.
|
||||||
|
|
Loading…
Reference in New Issue