Testify
Testify
Comprehensive testing toolkit for Go applications
Testify is a toolkit for assertions and mocks used extensively in the Control Plane for testing.
Testing framework of choice
Testify enhances Go's native testing capabilities with expressive assertions, mocking, and test suite organization, making it easier to write comprehensive and maintainable tests.
Overviewโ
Testify is a set of packages that extend Go's built-in testing package with additional functionality for assertions, mocking, and test suite organization. The Control Plane uses Testify extensively across its codebase to ensure robust testing and maintainability.
Why Testify?โ
โ Expressive Assertions
Rich set of assertion functions that clearly express intent and provide helpful error messages.
๐งช Comprehensive Testing
Supports unit, integration, and end-to-end testing with the same consistent API.
๐ ๏ธ Mocking Framework
Built-in mocking capabilities for isolating components during testing.
๐ฆ Test Organization
Suite functionality for grouping related tests with shared setup and teardown.
๐ Go Integration
Seamlessly integrates with Go's built-in testing framework and tools.
๐งฉ Extensibility
Can be extended with custom assertions and test helpers to fit project needs.
Integration in the Control Planeโ
Key integration point
Testify provides a consistent assertion and testing framework used throughout the Control Plane codebase.
The Control Plane uses Testify for multiple testing purposes, providing a unified approach to testing across various components:
Unit Testing
Core component testing:
- Service functions
- Controller logic
- Utility functions
- Models and data structures
Integration Testing
Component interaction testing:
- API endpoints
- Service interactions
- Database operations
- External service integrations
Configurationโ
Test organization and execution
Testify provides several approaches to organize and run tests, which are used throughout the Control Plane.
Running Testsโ
Standard Commands
Run all tests:
go test ./...
Run specific tests:
go test ./pkg/controller -run TestUpload
Run with verbose output:
go test -v ./...
Coverage Reports
Run tests with coverage:
go test -cover ./...
Generate HTML coverage report:
go test -coverprofile=coverage.out ./... go tool cover -html=coverage.out
Core Packagesโ
assert
Non-fatal assertions that allow tests to continue:
assert.Equal(t, expected, actual)
require
Fatal assertions that terminate test execution:
require.NoError(t, err)
mock
Test double creation and expectations:
mock.On("Method").Return(result)
Best Practices Used in Control Planeโ
Testing best practices
The Control Plane follows these testing best practices to ensure reliable, maintainable, and comprehensive tests.
๐ซ Use require for fatal assertions
Prevents further test execution when critical assumptions fail, avoiding confusing cascade failures.
๐ Use descriptive error messages
Clear messages help quickly identify and fix test failures without having to dig through code.
๐๏ธ Create test suites for related functionality
Organizes tests logically and enables sharing of common setup and teardown code.
๐ Mock external dependencies
Isolates the code under test and enables testing of specific behaviors without external services.
๐ Aim for high coverage
The Control Plane targets at least 80% test coverage for critical components.
๐งช Test edge cases
Explicitly test error conditions, empty inputs, and boundary values for robust code.
Related Resourcesโ
Mockery
Learn about the mock code generation tool used alongside Testify.
Go Language
Explore the foundational language used in the Control Plane.