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
Ryan Roden-Corrent 2021-05-21 07:18:13 -04:00 committed by GitHub
parent 04ea2824d3
commit b90bae79da
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 11 additions and 0 deletions

View File

@ -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`.