Documentation
¶
Overview ¶
Package testutils provides utilities for testing, including generic test case structures and helper functions for error checking and temporary file handling.
This package is designed to simplify the process of writing tests by providing reusable components that handle common testing scenarios, such as comparing expected and actual results, checking for specific error conditions, and managing temporary files.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func ErrorAs ¶
ErrorAs returns a function that checks if the error is of a specific type T. This is useful for verifying that an error matches a particular interface or concrete type.
func ErrorContains ¶
ErrorContains returns a function that checks if the error message contains the expected substring. This is useful for validating that an error message includes specific information.
func ErrorIs ¶
ErrorIs returns a function that checks if the error is equal to the expected error. This is useful for confirming that an error is exactly the one anticipated.
func WithYAMLFile ¶
WithYAMLFile creates a temporary YAML file with the provided content and executes a function with the file. It ensures the file is removed after the function execution, preventing resource leaks. This utility is helpful for tests that require file-based configuration.
Types ¶
type ConfigTestData ¶
type ConfigTestData struct { // YAML file content to be tested. Yaml string // Environment variables to be used in the test. Env map[string]string }
ConfigTestData holds test data for loading and validating configuration from both YAML files and environment variables.
type TestCase ¶
type TestCase[T any, D any] struct { // Name is the identifier for the test case, used for reporting purposes. Name string // Expected is the anticipated result of the test. It should be left empty if an error is expected. Expected T // Data contains the input or configuration for the test case. Data D // Error is a function that checks the error returned by the test function, if an error is anticipated. Error func(*testing.T, error) }
TestCase represents a generic test case structure. It is parameterized by T, the type of the expected result, and D, the type of the test data. This struct is useful for defining test cases with expected outcomes and associated data.
func (TestCase[T, D]) F ¶
F returns a test function that executes the logic of the test case, suitable for use with t.Run(). It takes a function f that processes the test data and returns an actual result along with an error, if any. After executing f, it verifies the actual result against the expected result or evaluates the error condition.