Skip to content

the-code-genin/golang_integration_testing

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

45 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Golang Integration Testing Example

This repository accompanies the article series A Comprehensive Guide to Software Testing in Golang and demonstrates how to build a simple CRUD application in Golang with robust testing practices.

The project covers unit, integration and system testing against real infrastructure using Docker and Testcontainers.

Features

  • Create, Read, Update, Delete (CRUD) operations for notes with an id, title, description and timestamps.
  • Layered architecture:
    • Database Access Layer (DBAL): Handles all database interactions using pgx.
    • Service Layer: Contains business logic.
    • HTTP Layer: Exposes REST API endpoints.
  • Integration tests against ephemeral PostgreSQL containers using Testcontainers.
  • Unit tests with mocked dependencies.
  • System tests with httptest.

Prerequisites

  • Go 1.20 or higher.
  • Docker.
  • make (optional).

Getting Started

  1. Clone the repository
git clone https://github.com/the-code-genin/golang_integration_testing.git
cd golang_integration_testing
  1. Initialize Go modules
go mod download
  1. Set up PostgreSQL

(Optional) Using Docker:

docker run -p 5432:5432 -e POSTGRES_PASSWORD=password -d postgres:16.11

Running migrations:

make migrate-up

This ensures we have a database with all migrations applied.

  1. Running the Server

Start the application:

go run .

The server should start on port 8080 (or the port specified via env variables):

# [GIN-debug] Listening and serving HTTP on :8080

Project Structure

  • migrations/ - SQL migration files.
  • repository/ - Database Access Layer (DBAL).
  • service/ - Business logic layer.
  • http/ - REST API layer.
  • tests/- Test helpers.
  • main.go- Application entry point.
  • Makefile - Optional automation commands.

API Endpoints

  • POST /v1/notes - Create a note with title and description.
  • GET /v1/notes/:id - Fetch a single note by ID.
  • GET /v1/notes - Fetch all notes.
  • PUT /v1/notes/:id - Update a note by ID.
  • DELETE /v1/notes/:id - Delete a note by ID.

About

A sample project accompanying an article series which showcases how to setup Go codebases for unit, integration and system testing.

Topics

Resources

Stars

Watchers

Forks

Contributors