3 Fighting with Complexity Flashcards
How to you mark a function as a test helper?
Include a call to t.Helper()
in the function.
What does t.Helper()
do?
The caller, instead of the helper, will report the failure.
What is the idiomatic solution to the duplicate tests problem?
table-driven testing.
What are the advantages of table-driven-testing?
- Less code
- Less cognitive load
- Easy to add new test cases
- Easier to see corner cases are covered
- Avoids adding helper methods for shared logic
What is a test that you can run within a top-level test function in isolation?
A subtest.
What Go command runs tests and shuffles the order they run in?
go test -v -shuffle=on
After running a shuffled test how to I rerun again in that same order?
Take the number output in the first run and use it with the shuffle flag.
go test -v -shuffle=1624629565232133000
How do I run a subtest?
t.Run(name string, func (t *testing.T) {…} )
What is a higher order function?
a function that returns another function.
Declare a table test who’s subtest would be run in random order?
Use a map instead of an array struct.
~~~
tests := map[string]struct{..}
~~~