英文:
How to run a Go server in Travis-CI?
问题
我正在处理一个暴露 RESTful http API 的 Go 项目。
我想运行这个 Go 项目,并使用 Node.js(Mocha)来测试端点。似乎 nohup 命令不能在后台持续运行。
在本地一切正常,但似乎无法在 Travis-ci 中运行。
language: go
go:
- 1.8
env:
- "PATH=/home/travis/gopath/bin:$PATH"
before_install:
- go get ./...
script:
- npm install mocha -g
- npm install
- nohup go run ./cmd/server/main.go --scheme=http --port=8080 --host=127.0.0.1 &
- mocha
英文:
I'm working on a Go project that exposes a RESTful http API.
I want to run the Go project and use Nodejs (Mocha) to test the endpoints. It seems as if the nohup command doesn't keep running in the background.
Locally everything works but I don't seem to be able to get it running in Travis-ci.
language: go
go:
- 1.8
env:
- "PATH=/home/travis/gopath/bin:$PATH"
before_install:
- go get ./...
script:
- npm install mocha -g
- npm install
- nohup go run ./cmd/server/main.go --scheme=http --port=8080 --host=127.0.0.1 &
- mocha
答案1
得分: 3
首先,你应该认真考虑一下是否值得用 mocha 编写测试。诚然,可能有些情况下确实有意义(比如,如果你正在将一个 Node.js 应用程序移植到 Go,并且已经编写了针对 Node.js 的测试)。但即使是这样,你也应该将其视为一种权宜之计,并尽快编写所有新的测试,并迁移旧的测试到 Go 中。
但是,除此之外,没有理由你不能正常地在后台启动进程。也许可以通过一个从 Travis 配置中调用的 shell 脚本来实现(这样通常比直接将所有命令放在配置中更清晰、更容易理解):
#!/bin/sh
go run &
mocha
如果你真的必须在外部进程中运行测试,从 Go 测试中启动它也有一些优势。特别是,你可以获得测试覆盖率统计信息,并且启动可以更容易地同步(这样就不需要 sleep
)。要做到这一点,你可以按照我在这个答案中的建议进行操作。具体来说,在 mocha 的情况下:
编写一个测试文件,在其中的一个 go 协程中执行你的 main() 函数:
func TestMainApp(t *testing.T) {
go main() // 或者你需要执行的其他启动服务器进程的操作
cmd := exec.Command("mocha", ...)
cmd.Start()
}
但是,说真的。你应该用 Go 编写你的测试。
英文:
First, you should seriously question whether writing tests in mocha makes sense. Admittedly, there might be cases when it does make sense (i.e. if you're porting a nodejs app to Go, and already have tests written for node). But even then, you should consider this a stop-gap measure, and write all new tests, and even migrate old tests, to Go as soon as possible.
But that aside, there's no reason you shouldn't be able to just start the process in the background normally. Perhaps with a shell script called from your Travis config (can often be cleaner and easier to follow than putting all commands in the config directly):
#!/bin/sh
go run &
mocha
If you really must run your tests in an external process, there are certain advantages from starting that from a Go test anyway. Namely, you can get test coverage stats, and starting can be more easily synchronized (so you don't need a sleep
). To do this, you can follow my advice in this answer. Specifically, for the mocha case:
Write a test file that executes your main() function in a go routine:
func TestMainApp(t *testing.T) {
go main() // Or whatever you need to do to start the server process
cmd := exec.Command("mocha", ...)
cmd.Start()
}
But seriously. You should write your tests in Go.
答案2
得分: 0
Mocha在- nohup go run ./cmd/server/main.go --scheme=http --port=8080 --host=127.0.0.1 &
之后直接运行,所以服务器还没有启动。
添加一个延时就足够了。
script:
- npm install mocha -g
- npm install
- nohup go run ./cmd/server/main.go --scheme=http --port=8080 --host=127.0.0.1 &
- sleep 4
- mocha
但是,我明白你的意思,我会转向使用Go测试
英文:
Mocha runs directly after - nohup go run ./cmd/server/main.go --scheme=http --port=8080 --host=127.0.0.1 &
so the server didn't start up yet.
Adding a sleep was enough.
script:
- npm install mocha -g
- npm install
- nohup go run ./cmd/server/main.go --scheme=http --port=8080 --host=127.0.0.1 &
- sleep 4
- mocha
But, the point is taken, I will move to Go tests
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论