Documentation
¶
Overview ¶
Package testutils provides utilities for testing Go applications
Package testutils provides a collection of utilities for testing Go applications including stdout/stderr capture, file handling, HTTP test helpers, and test containers.
It consists of several main components:
Capture Utilities:
- CaptureStdout, CaptureStderr, CaptureStdoutAndStderr: Functions to capture output from standard streams during test execution. These are useful for testing functions that write directly to stdout/stderr. Note: These functions are not thread-safe for parallel tests.
File Utilities:
- WriteTestFile: Creates a temporary file with specified content for testing purposes, with automatic cleanup after the test completes.
HTTP Utilities:
- MockHTTPServer: Creates a test HTTP server with the provided handler
- HTTPRequestCaptor: Captures and records HTTP requests for later inspection
Test Containers: The 'containers' subpackage provides Docker containers for integration testing:
- SSHTestContainer: SSH server container with file operation support (upload, download, list, delete)
- FTPTestContainer: FTP server container with file operation support
- PostgresTestContainer: PostgreSQL database container with automatic DB creation
- MySQLTestContainer: MySQL database container with automatic DB creation
- MongoTestContainer: MongoDB container with support for multiple versions
- LocalstackTestContainer: LocalStack container with S3 service for AWS testing
All container implementations support a common pattern:
- Container creation with NewXXXTestContainer
- Automatic port mapping and connection configuration
- Graceful shutdown with the Close method
- File operations where applicable (SaveFile, GetFile, ListFiles, DeleteFile)
These utilities help simplify test setup, improve test reliability, and reduce boilerplate code in test suites, especially for integration tests.
Index ¶
- func CaptureStderr(t *testing.T, f func()) string
- func CaptureStdout(t *testing.T, f func()) string
- func CaptureStdoutAndStderr(t *testing.T, f func()) (stdout, stderr string)
- func MockHTTPServer(t *testing.T, handler http.Handler) (serverURL string, cleanup func())
- func WriteTestFile(t *testing.T, content string) string
- type RequestCaptor
- type RequestRecord
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CaptureStderr ¶
CaptureStderr captures os.Stderr output from the provided function.
func CaptureStdout ¶
CaptureStdout captures os.Stdout output from the provided function.
func CaptureStdoutAndStderr ¶
CaptureStdoutAndStderr captures os.Stdout and os.Stderr output from the provided function.
func MockHTTPServer ¶ added in v0.2.0
MockHTTPServer creates a test HTTP server with the given handler. Returns the server URL and a function to close it.
Types ¶
type RequestCaptor ¶ added in v0.2.0
type RequestCaptor struct {
// contains filtered or unexported fields
}
RequestCaptor captures HTTP requests for inspection in tests
func HTTPRequestCaptor ¶ added in v0.2.0
HTTPRequestCaptor returns a request captor and HTTP handler that captures requests The returned handler will forward requests to the provided next handler if not nil
func (*RequestCaptor) GetRequest ¶ added in v0.2.0
func (c *RequestCaptor) GetRequest(idx int) (RequestRecord, bool)
GetRequest returns the request at the specified index
func (*RequestCaptor) GetRequests ¶ added in v0.2.0
func (c *RequestCaptor) GetRequests() []RequestRecord
GetRequests returns all captured requests
func (*RequestCaptor) Len ¶ added in v0.2.0
func (c *RequestCaptor) Len() int
Len returns the number of captured requests
func (*RequestCaptor) Reset ¶ added in v0.2.0
func (c *RequestCaptor) Reset()
Reset clears all captured requests
Directories
¶
Path | Synopsis |
---|---|
Package containers implements various test containers for integration testing
|
Package containers implements various test containers for integration testing |