Documentation
¶
Overview ¶
Package testutil provides common testing utilities for azd extensions.
This package includes helpers for:
- Capturing stdout during test execution (CaptureOutput)
- Locating test fixture directories (FindTestData)
- Creating temporary directories with automatic cleanup (TempDir)
- Common string assertions (Contains)
All functions use t.Helper() for proper test line reporting.
Example usage:
import (
"testing"
"github.com/jongio/azd-core/testutil"
)
func TestCommand(t *testing.T) {
// Capture stdout from a command
output := testutil.CaptureOutput(t, func() error {
return runCommand()
})
// Check output contains expected text
if !testutil.Contains(output, "success") {
t.Error("expected success message")
}
}
func TestWithFixtures(t *testing.T) {
// Find test data directory
fixturesDir := testutil.FindTestData(t, "tests", "fixtures")
// Create temporary directory for test outputs
tmpDir := testutil.TempDir(t)
// tmpDir is automatically cleaned up after test
}
Package testutil provides common testing utilities for azd extensions. It includes helpers for capturing output, locating test resources, creating temporary directories, and common test assertions.
Index ¶
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
func CaptureOutput ¶
CaptureOutput captures stdout during function execution. It redirects os.Stdout to a pipe, executes the function, and returns the captured output. The original stdout is always restored, even if the function returns an error. This is useful for testing commands that write to stdout.
Example:
output := testutil.CaptureOutput(t, func() error {
fmt.Println("test output")
return nil
})
if !strings.Contains(output, "test output") {
t.Error("expected output not found")
}
func Contains ¶
Contains checks if a string contains a substring. This is a convenience helper for common test assertions.
Example:
if !testutil.Contains(output, "expected text") {
t.Error("output does not contain expected text")
}
func FindTestData ¶
FindTestData finds a test data directory relative to the current working directory. It accepts variadic subdirectory names to construct the path (e.g., "tests", "projects"). It searches common locations relative to typical test directory structures and returns the first valid path found. This helper is useful for integration tests that need to locate test fixtures.
Example:
testsDir := testutil.FindTestData(t, "tests", "projects") scriptPath := filepath.Join(testsDir, "hello-world", "script.sh")
func TempDir ¶
TempDir creates a temporary directory for testing with automatic cleanup. The directory is created with secure permissions (0750) and is automatically removed when the test completes via t.Cleanup().
Example:
tmpDir := testutil.TempDir(t) configPath := filepath.Join(tmpDir, "config.json") // Directory is automatically cleaned up after test
Types ¶
This section is empty.