Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/zombiezen/postgrestest
A Go test harness that starts an ephemeral PostgreSQL server
https://github.com/zombiezen/postgrestest
golang postgres postgresql testing
Last synced: 10 days ago
JSON representation
A Go test harness that starts an ephemeral PostgreSQL server
- Host: GitHub
- URL: https://github.com/zombiezen/postgrestest
- Owner: zombiezen
- License: apache-2.0
- Created: 2020-08-08T16:58:27.000Z (over 4 years ago)
- Default Branch: main
- Last Pushed: 2024-04-08T20:11:36.000Z (7 months ago)
- Last Synced: 2024-06-19T05:58:49.795Z (5 months ago)
- Topics: golang, postgres, postgresql, testing
- Language: Go
- Homepage:
- Size: 35.2 KB
- Stars: 41
- Watchers: 3
- Forks: 3
- Open Issues: 3
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
- Funding: .github/FUNDING.yml
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# `zombiezen.com/go/postgrestest`
[![Reference](https://pkg.go.dev/badge/zombiezen.com/go/postgrestest?tab=doc)](https://pkg.go.dev/zombiezen.com/go/postgrestest?tab=doc)
[![Contributor Covenant](https://img.shields.io/badge/Contributor%20Covenant-v2.0%20adopted-ff69b4.svg)](CODE_OF_CONDUCT.md)Package `postgrestest` provides a test harness that starts an ephemeral
[PostgreSQL][] server. It is tested on macOS, Linux, and Windows. It can cut
down the overhead of PostgreSQL in tests up to 90% compared to spinning up a
`postgres` Docker container: starting a server with this package takes
roughly 650 milliseconds and creating a database takes roughly 20 milliseconds.[PostgreSQL]: https://www.postgresql.org/
## Example
```go
func TestApp(t *testing.T) {
// Start up the PostgreSQL server. This can take a few seconds, so better to
// do it once per test run.
ctx := context.Background()
srv, err := postgrestest.Start(ctx)
if err != nil {
t.Fatal(err)
}
t.Cleanup(srv.Cleanup)// Each of your subtests can have their own database:
t.Run("Test1", func(t *testing.T) {
db, err := srv.NewDatabase(ctx)
if err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`CREATE TABLE foo (id SERIAL PRIMARY KEY);`); err != nil {
t.Fatal(err)
}
// ...
})t.Run("Test2", func(t *testing.T) {
db, err := srv.NewDatabase(ctx)
if err != nil {
t.Fatal(err)
}
if _, err := db.Exec(`CREATE TABLE foo (id SERIAL PRIMARY KEY);`); err != nil {
t.Fatal(err)
}
// ...
})
}
```## Installation
PostgreSQL must be installed locally for this package to work. See the
[PostgreSQL Downloads page][] for instructions on how to obtain PostgreSQL for
your operating system.To install the package:
```
go get zombiezen.com/go/postgrestest
```[PostgreSQL Downloads page]: https://www.postgresql.org/download/
## License
[Apache 2.0](LICENSE)