- 如何运行测试代码?
//执行当前目录下所有测试文件
➜ sqlc git:(main) ✗ go test -v
//或者,测试特定函数
➜ sqlc git:(main) ✗ go test -v -run TestMain
建议使用go get github.com/stretchr/testify进行测试代码的编写,这里if else更加简洁
- 执行TestCreateAccount
手写测试params太麻烦了,可不可以自动生成呢?
在/util下编写random.go,随机生成各种参数,类似 int,string,owner,currency
- 在Makefile中添加测试命令
test:
go test -v -cover ./...
- 如何用github actions测试代码
# This workflow will build a golang project
# For more information see: https://docs.github.com/en/actions/automating-builds-and-tests/building-and-testing-go
name: ci-test
on:
push:
branches: [ "main" ]
pull_request:
branches: [ "main" ]
jobs:
test:
name: Test
runs-on: ubuntu-latest
services:
postgres:
image: postgres:12
env:
POSTGRES_USER: root
POSTGRES_PASSWORD: secret
POSTGRES_DB: simple_bank
ports:
- 5432:5432
options: >-
--health-cmd pg_isready
--health-interval 10s
--health-timeout 5s
--health-retries 5
steps:
- uses: actions/checkout@v3
- name: Set up Go
uses: actions/setup-go@v4
with:
go-version: '1.20'
- name: Install golang-migrate
run: |
curl -L https://github.com/golang-migrate/migrate/releases/download/v4.14.1/migrate.linux-amd64.tar.gz | tar xvz
sudo mv migrate.linux-amd64 /usr/bin/migrate
which migrate
- name: Run migrations
run: make migrateup
- name: Test
run: make test