The Art of Seamless Integration: A Comprehensive Guide to Integration Testing
Published: 2023-07-07 18:22:12


Integration testing is the backbone of robust software development. It ensures that different components of an application work harmoniously together, preventing issues when they interact. It is a crucial part of software development but it was never as easy as it is right now, to unleash the power of seamless system interactions and conquer integration testing like a pro with the help of docker. 

First thing first, let's create a server with a couple of apis.
```
// main.go
package main

import (...)

func main() {
	router := chi.NewRouter()

	router.Route("/", handler())

	log.Println("Server starting on port 8848...")
	err := http.ListenAndServe(":8848", router)
	if err != nil {
		log.Fatal("Server error:", err)
	}
}

func handler() func(r chi.Router) {
	return func(r chi.Router) {
		r.Get("/", getAll())
		r.Post("/", post())
		r.Get("/{id}", get())
		r.Put("/{id}", update())
		r.Delete("/{id}", delete())
	}
}
```
Now let's create a `Dockerfile` so that we can push the docker image from GitHub action.

```
FROM golang:latest

WORKDIR /app

COPY go.mod go.sum ./
RUN go mod download

COPY . .
RUN go build -o myapp

CMD ["./myapp"]
```
Here is a sample of How you can create a test case on Postman. Later on, we can export a `json` file of the Postman collection.

![postman api call](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/79fd7hg4itm2hj4niatj.png)

![postman testcases](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/xqj8bn4x8a1ecfscgby4.png)
Now we need to create a github action that should be able to build `test-app:develop` image and run postman collection over it. For the sake of simplicity, I like to split the functionality. 

`ci-cd.yml`
```
name: Build, publish image and test

on:
  push:
    branches: [ "main" ]
  pull_request:
    branches: [ "main" ]

jobs:
  build:
    runs-on: ubuntu-latest
    steps:
      - name: Cleanup workspace
        run: |
          echo "Cleaning up previous run from ${{ github.workspace }}"
          set -e # fail on error
          shopt -s dotglob # include hidden files
          sudo rm -rf *

      - name: Checkout repository code
        uses: actions/checkout@v3
    
      - name: Build image
        run: docker build -t vhndaree/test-app:develop .
        
      - name: Run integration tests
        run: ./run-integration-tests.sh develop
```

`run-integration-tests.sh`
```
#!/usr/bin/env bash
set -eo pipefail

DIR="$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd)"
cd "${DIR}"

function clean_up() {
  docker stop test-app >/dev/null 2>&1 || true
  docker network rm test-app-bridge >/dev/null 2>&1 || true
}

trap clean_up EXIT
docker network create -d bridge test-app-bridge
docker run -d --rm --name test-app --net=test-app-bridge vhndaree/test-app:develop

sleep 5 # This could fail some time if docker took more than 5 seconds to start

docker run --rm --name postman-newman -v "${DIR}/postman_collection.json:/postman_collection.json" --net=test-app-bridge postman/newman run --env-var "host=test-app" /postman_collection.json
```
And that's it! Integration testing, when done right, can make the process of ensuring seamless integration seem surprisingly easy. I hope you've gained valuable insights into the art of seamless integration. 🚀 Integration testing plays a crucial role in ensuring components work harmoniously together, uncovering potential issues early on. 😉 So go ahead, apply what you've learned and watch your projects thrive! 🌟

Feel free to explore the repository and find more information about the integration test demo on [Vhndaree/integration-test-demo](https://github.com/Vhndaree/integration-test-demo).

If you have any questions or confusion, feel free to comment below. 😊