Skip to main content

Overview

CompanyFlow includes a test suite for repositories and core functionality. Tests are written using Go’s built-in testing framework and require a test database.

Prerequisites

Before running tests, ensure you have:
  • Go 1.24 or higher installed
  • PostgreSQL test database configured
  • All dependencies installed (go mod download)

Test Database Setup

1

Create Test Database

Create a separate database for testing:
Never use your production database for testing. Tests may create, modify, or delete data.
2

Configure Test Environment

Set test database environment variables:
3

Run Initial Migrations

Run the application once to execute migrations on the test database:
Stop the application after migrations complete (Ctrl+C).

Running Tests

Run All Tests

Run the entire test suite:
With verbose output:

Run Specific Packages

Run with Coverage

Generate test coverage reports:

Run Specific Tests

Run tests matching a pattern:

Test Structure

CompanyFlow tests are organized by layer:

Writing Tests

Repository Test Example

Here’s an example of how to write a repository test:
repositories/company_repository_test.go

Service Test Example

Example service test with mocked repository:
services/company_service_test.go

Handler Test Example

Example HTTP handler test:
handlers/company_handler_test.go

Test Best Practices

Clean Up Test Data

Always clean up test data after tests:

Use Table-Driven Tests

Test multiple scenarios efficiently:

Use Test Fixtures

Create reusable test data:

Use Subtests

Organize related tests:

Integration Testing

Test API Endpoints

Test complete API workflows:

Use Test Containers

For isolated integration tests, use Docker:

Continuous Integration

GitHub Actions Example

.github/workflows/test.yml

Performance Testing

Benchmark Tests

Write benchmark tests for performance-critical code:
Run benchmarks:

Troubleshooting

Tests Fail to Connect to Database

Ensure test database is running and environment variables are set:

Tests Hang or Timeout

Set a test timeout:

Race Conditions

Detect race conditions:

Database State Issues

If tests fail due to dirty database state:

Next Steps

After testing your API:
  1. Explore the API documentation
  2. Learn about authentication
  3. Review database setup