0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Goでテスト自動化 & CI/CD 構築を構築してみよう

Posted at

はじめに

ソフトウェア開発において、テストの自動化とCI/CD(継続的インテグレーション / 継続的デリバリー)は、品質向上と開発効率の向上に不可欠です。
Goでは、標準のtestingパッケージを使ったテスト自動化や、GitHubActions、Dockerを活用したCI/CDパイプラインの構築が可能です。

今回は、テストを自動化できる方法を簡単に解説します。

対象読者

  • Goのテストを自動化したい方
  • CI/CDの仕組みを学び、開発効率を向上させたい方
  • 実践的なGitHubActionsを使ったCI/CDの設定を知りたい方

目次

  1. テスト自動化の重要性
    • 手動テスト vs 自動テスト
    • 自動テストのメリット
  2. Goでのテスト自動化
    • go testを使ったユニットテスト
    • gotestsumを使った高度なテストレポート
  3. CI/CDの基本と構築方法
    • そもそもCI/CDとは?
    • GitHub Actionsを使ったCI/CD設定
    • Docker & Kubernetesを利用したデプロイの自動化
  4. まとめ

1. テスト自動化の重要性

1.1 手動テスト vs 自動テスト

項目 手動テスト 自動テスト
実行コスト 高い(毎回手作業) 低い(1 コマンドで実行)
バグ検出 人為的ミスが起こりやすい 一貫した品質保証が可能
開発スピード 遅い 速い

1.2 自動テストのメリット

  • バグの早期発見:デプロイ前に問題を検出できる
  • コードの品質向上:一貫性のあるコードを維持
  • 開発スピードの向上:テストの実行を自動化することで、迅速なデリバリーが可能となる

2. Goでのテスト自動化

2.1 go testを使ったユニットテスト

Goでは、標準ライブラリのtestingパッケージを使用して簡単にテストを自動化できます。

example_test.go

package example

import (
    "testing"
)

func Add(a, b int) int {
    return a + b
}

func TestAdd(t *testing.T) {
    result := Add(2, 3)
    expected := 5
    if result != expected {
        t.Errorf("Expected %d but got %d", expected, result)
    }
}

テストの実行

go test ./...

2.2 gotestsumを使った高度なテストレポート

gotestsumを使うと、テスト結果を見やすく表示できます。

インストール

go install gotest.tools/gotestsum@latest

実行

gotestsum --format testname

3. CI/CDの基本と構築方法

3.1 そもそもCI/CDとは?

項目 説明
CI(継続的インテグレーション) コード変更時に自動でテストを実行し、品質を担保
CD(継続的デリバリー) テストが通過したら、自動でデプロイ

CI/CDを導入することで、「テスト → ビルド → デプロイ」のプロセスを自動化できます。

3.2 GitHubActionsを使ったCI/CDの設定

GitHubActionsを使えば、リポジトリの更新時に自動でテストを実行できます。

.github/workflows/ci.yml

name: Go CI

on:
  push:
    branches:
      - main
  pull_request:
    branches:
      - main

jobs:
  test:
    runs-on: ubuntu-latest

    steps:
    - name: リポジトリをチェックアウト
      uses: actions/checkout@v2

    - name: Go をセットアップ
      uses: actions/setup-go@v2
      with:
        go-version: 1.18

    - name: 依存関係を取得
      run: go mod tidy

    - name: テストの実行
      run: go test ./...

3.3 Docker & Kubernetesを利用したデプロイの自動化

GitHubActionsを使ってDockerイメージをビルドし、Kubernetesにデプロイできます。

.github/workflows/deploy.yml

name: Deploy to Kubernetes

on:
  push:
    branches:
      - main

jobs:
  deploy:
    runs-on: ubuntu-latest
    steps:
    - name: Docker ビルドとプッシュ
      run: |
        docker build -t my-app:latest .
        docker tag my-app:latest myrepo/my-app:latest
        docker push myrepo/my-app:latest

    - name: Kubernetes へのデプロイ
      run: |
        kubectl apply -f k8s/deployment.yaml

4. まとめ

項目 説明
Goのテスト自動化 testingパッケージと go testを活用
CI/CD の導入 GitHubActionsを使ってテストとデプロイを自動化
Docker & Kubernetes 自動デプロイを実現し、本番環境に迅速に反映

次は補足、ちょっと上級者向けの内容として、テストカバレッジの向上についてや、Terraformを活用したクラウド環境の自動構築を解説します!
毎日勉強!!!

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?