原文连接:在 Go 语言项目中使用 Travis CIgit
Travis CI 是一种免费的持续集成服务,而 持续集成(CI, Continuous integration) 是一种软件工程流程,归纳来说就是多提交小的 Commit 来更快的发现软件的 Bug,从而提升软件质量。github
本文会详细介绍如何在 Go 语言项目中使用 Travis CI。golang
hello.go:svg
package hello func Hello() string { return "Hello, World!" }
hello_test.go:post
package hello import ( "testing" ) func TestHello(t *testing.T) { if got, want := Hello(), "Hello, World!"; got != want { t.Errorf("got %v, want %v", got, want) } }
go.mod:测试
module github.com/linehk/hello go 1.14
该文件经过命令:go mod init github.com/linehk/hello
生成。ui
创建一个 GitHub 仓库,并将上面三个文件组成的 hello 项目推送到仓库。spa
在 Travis CI 仓库页面左上角点击 Sync account 来将 GitHub 新建立的仓库同步到 Travis CI。code
并在左边的 Legacy Services Integration 下找到 hello 项目,将其勾选上。以下图所示:blog
受益于在 Go 1.13 版本后,Go Modules 是默认开启的状况下,如下就是最简单的 .travis.yml 文件,.yml 后缀表示使用的是 YAML 格式:
# 使用 Go 语言(# 号开头的为注释) language: go script: - go test -v ./...
将上一步编写的 .travis.yml 文件加入仓库并推送至 GitHub。Travis CI 检测到该文件就会根据里面的内容开始测试。以后每次推送都会触发测试。
在 dashboard 中能够找到 hello 项目,点击进入就能够看到详细的测试信息。以下图所示:
点击绿色的 build passing 就能获得徽章的地址,如:https://travis-ci.org/linehk/hello.svg?branch=master
。
能够将它贴在你的 README 文件里,若是项目在某次推送中测试失败,徽章就会变成红色。