
Go Test 에는 native test, testify library 가 대표적으로 사용됨
BDD DSL 기반 test 구성을 한다면 testify library 사용
native test 가 조금 더 쓰기 편하다고 느껴짐

필자는 아직 testify 를 사용해본 적이 없어 추후 활용하게 되면 업데이트하겠음
test 패키지에서 testcontainer 생성하는 모듈을 따로 구성함
package container
import (
"context"
"github.com/testcontainers/testcontainers-go/modules/mysql"
)
func CreatMySQLContainer(ctx context.Context) (*mysql.MySQLContainer, error) {
return mysql.Run(
ctx,
"mysql:8.0.36",
mysql.WithDatabase("old"),
mysql.WithUsername("root"),
mysql.WithPassword("testdbsecret"),
mysql.WithScripts("../test/data/schema.sql"),
)
}
이후 아래와 같이 connection 확인하는 로직 구성함
package db
import (
"context"
container "db-migration/test/container"
"fmt"
_ "github.com/go-sql-driver/mysql"
"github.com/testcontainers/testcontainers-go"
"os"
"testing"
)
func TestConnectDBWithURL(t *testing.T) {
// GIVEN
ctx := context.Background()
mysqlContainer, _ := container.CreatMySQLContainer(ctx)
testcontainers.CleanupContainer(t, mysqlContainer)
connectionString, _ := mysqlContainer.ConnectionString(ctx)
// WHEN
result, err := ConnectDBWithURL(connectionString)
// THEN
if err != nil {
t.Error(err)
t.Fail()
}
if result == nil {
t.Error("ConnectDBWithURL failed")
t.Fail()
}
result.Close()
}
func TestOnFile(t *testing.T) {
readFile, _ := os.ReadFile("../test/data/schema.sql")
fmt.Print(string(readFile))
//fmt.Println(file)
}
https://go.dev/doc/tutorial/add-a-test
https://github.com/junegunn/fzf/blob/master/src/ansi_test.go