Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/agis/spawn
Spawn your Go server from within its own test suite, for end-to-end tests
https://github.com/agis/spawn
e2e e2e-testing e2e-tests go golang golang-testing testing testing-tools
Last synced: 4 months ago
JSON representation
Spawn your Go server from within its own test suite, for end-to-end tests
- Host: GitHub
- URL: https://github.com/agis/spawn
- Owner: agis
- License: mit
- Created: 2018-09-19T12:53:13.000Z (over 6 years ago)
- Default Branch: master
- Last Pushed: 2023-04-13T08:34:35.000Z (almost 2 years ago)
- Last Synced: 2024-10-06T04:14:19.115Z (4 months ago)
- Topics: e2e, e2e-testing, e2e-tests, go, golang, golang-testing, testing, testing-tools
- Language: Go
- Homepage: https://pkg.go.dev/github.com/agis/spawn
- Size: 16.6 KB
- Stars: 32
- Watchers: 3
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
spawn
===============Spawn makes it easy to spin up your Go server right from within its own test suite, for end-to-end testing.
Usage
--------------
An example usage for [this simple HTTP server](examples/main.go) can be found below.
The complete runnable example is at [examples](examples/).```go
func TestMain(m *testing.M) {
// start the server on localhost:8080 (we assume it accepts a `--port` argument)
server := spawn.New(main, "--port", "8080")
ctx, cancel := context.WithCancel(context.Background())
server.Start(ctx)// wait a bit for it to become ready
time.Sleep(500 * time.Millisecond)// execute the test suite
result := m.Run()// cleanly shutdown server
cancel()
err := server.Wait()
if err != nil {
log.Fatal(err)
}os.Exit(result)
}func TestServerFoo(t *testing.T) {
res, _ := http.Get("http://localhost:8080/foo")
defer res.Body.Close()resBody, _ := ioutil.ReadAll(res.Body)
if string(resBody) != "Hello!" {
t.Fatalf("expected response to be 'Hello!', got '%s'", resBody)
}
}// more tests using the server
```Rationale
--------------
Writing an end-to-end test for a server typically involves:1) compiling the server code
2) spinning up the binary
3) communicating with it from the tests
4) shutting the server down
5) verify everything went OK (server was closed cleanly etc.)This package aims to simplify this process.