Skip to content
Ebenezer Emelogu edited this page Jul 19, 2024 · 5 revisions

Testing

Overview

This section provides an overview of the tests included in the test folders and instructions on how to run them.

Test Structure

The hng_boilerplate_golang_web project includes tests to ensure the functionality and reliability of the codebase. Tests are organized in the test directory, which includes unit tests, integration tests, and end-to-end tests.

Directory Structure

The hng_boilerplate_golang_web project includes tests to ensure the functionality and reliability of the codebase. Tests are organized in the test directory, which includes unit tests, integration tests, and end-to-end tests.

hng_boilerplate_golang_web/
├── test/
│   ├── unit/
│   │   └── example_test.go
│   ├── integration/
│   │   └── example_integration_test.go
│   └── e2e/
│       └── example_e2e_test.go
  • unit/: Contains unit tests that test individual components or functions in isolation.
  • integration/: Contains integration tests that test the interactions between multiple components.
  • e2e/: End-to-end tests testing the entire application flow.

Writing Tests

Tests in Go are written using the built-in testing package. Each test file should follow the naming convention *_test.go, and each test function should start with the word Test.

Example Unit Test

package unit

import (
    "testing"
)

func TestExampleFunction(t *testing.T) {
    result := ExampleFunction()
    expected := "expected result"
    
    if result != expected {
        t.Errorf("Expected %s but got %s", expected, result)
    }
}

Example Integration Test

package integration

import (
    "testing"
)

func TestExampleIntegration(t *testing.T) {
    // Setup code
    
    // Call the function that integrates multiple components
    
    // Assertions
}

Example End-to-End Test

package e2e

import (
    "testing"
)

func TestExampleE2E(t *testing.T) {
    // Setup code
    
    // Simulate user actions
    
    // Assertions
}

Running Tests

Running All Tests

To run all tests in the project, navigate to the project root directory and execute the following command:

go test ./...

Running Specific Tests

To run tests in a specific directory, navigate to that directory and execute the go test command. For example, to run all unit tests:

cd test/unit
go test