@@ -188,44 +188,22 @@ source of the panic, you need to spawn a new unrecovered go-routine.
188188hard to recreate. Do not try it.
189189
190190
191- ## Test result builder
192-
193- Setting up tests and comparing test results is most efficient, when you
194- directly can set up and compare the actual objects. However, this is sometimes
195- prevented by the objects not being open for construction and having private
196- states.
197-
198- To cope with this challenge the ` test ` -package supports helpers to access, i.e.
199- read and write, private fields of objects using reflection.
200-
201- * ` test.NewBuilder[...]() ` allows constructing a new object from scratch.
202- * ` test.NewGetter(...) ` allows reading private fields of an object by name.
203- * ` test.NewSetter(...) ` allows writing private fields by name, and finally
204- * ` test.NewAccessor(...) ` allows reading and writing of private fields by name.
205-
206- The following example shows a real world example of how the private properties
207- of a closed error can be set up using the ` test.NewBuilder[...]() ` .
191+ ## Out-of-the-box test patterns
208192
209- ``` go
210- err := test.NewBuilder [viper.ConfigFileNotFoundError ]().
211- Set (" locations" , fmt.Sprintf (" %s " , " ...path..." )).
212- Set (" name" , " test" ).Build ()
213- ```
193+ Currently, the package supports two _ out-of-the-box_ test patterns:
214194
215- Similar we can set up input objects with private properties to minimize the
216- dependencies in the test setup, however, using this features exposes the test
217- to internal changes.
195+ 1 . ` test.Main(func()) ` - allows to test main methods by calling the main
196+ method with arguments in a well controlled test environment.
197+ 2 . ` test.Recover(Test,any) ` - allows to check the panic result in simple test
198+ scenarios where ` test.Panic(any) ` is not applicable.
218199
219200
220- ## Out-of-the-box test patterns
201+ ### Main method tests pattern
221202
222- Currently, the package supports only one _ out-of-the-box_ test pattern to test
223- the ` main ` -methods of commands.
224-
225- The pattern executes the ` main ` method in a separate process to protect the
226- test execution against ` os.Exit ` calls while allowing to capture and check the
227- exit code against the expectation. The following example demonstrates how to
228- use the pattern to test a ` main ` method:
203+ The ` test.Main(func()) ` pattern executes the ` main ` method in a separate test
204+ process to protect the test execution against ` os.Exit ` calls while allowing to
205+ capture and check the exit code against the expectation. The following example
206+ demonstrates how to use the pattern to test a ` main ` method:
229207
230208``` go
231209mainTestCases := map [string ]test.MainParams {
@@ -253,4 +231,26 @@ e.g. as follows:
253231however, it is focused on testing the ` main ` methods with and without parsing
254232command line arguments.
255233
234+ ** Note:** In certain situations, ` test.Main(func()) ` currently fails to obtain
235+ the coverage metrics for the test execution, since ` go test ` is using the
236+ standard output to collect results. We are investigating how we can separate
237+ these in the test execution from expected test output.
238+
239+
240+ ## Convenience functions
241+
242+ The test package contains a number of convenience functions to simplify the
243+ test setup and apply certain test patterns. Currently, the following functions
244+ currently supported:
245+
246+ * ` test.Must[T](T, error) T ` - a convenience method for fluent test case setup
247+ that converts an error into a panic.
248+ * ` test.Cast[T](T) T ` - a convenience method for fluent test case setup that
249+ converts an casting error into a panic compliant with linting requirements.
250+ * ` test.Ptr[T](T) *T ` - a convenience method for fluent test case setup that
251+ converts a literal value into a pointer.
252+ * ` test.First[T](T, ...any) ` - a convenience method for fluent test case setup
253+ that extracts the first value of a response ignoring the others.
254+
255+
256256[ gomock ] : < https://go.uber.org/mock >
0 commit comments