{"id":21275684,"url":"https://github.com/halimath/fixture","last_synced_at":"2025-10-06T02:10:04.715Z","repository":{"id":64887591,"uuid":"578559341","full_name":"halimath/fixture","owner":"halimath","description":"A golang micro-framework for writing reusable test fixtures on top of package \"testing\"","archived":false,"fork":false,"pushed_at":"2023-11-12T11:58:41.000Z","size":19,"stargazers_count":0,"open_issues_count":0,"forks_count":0,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-01-22T03:27:27.588Z","etag":null,"topics":["golang","testing"],"latest_commit_sha":null,"homepage":"","language":"Go","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"apache-2.0","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/halimath.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-12-15T10:47:13.000Z","updated_at":"2023-02-22T08:08:12.000Z","dependencies_parsed_at":"2023-11-12T12:36:05.544Z","dependency_job_id":"88e218da-f17b-440b-aff7-1994531ad003","html_url":"https://github.com/halimath/fixture","commit_stats":null,"previous_names":[],"tags_count":1,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halimath%2Ffixture","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halimath%2Ffixture/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halimath%2Ffixture/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/halimath%2Ffixture/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/halimath","download_url":"https://codeload.github.com/halimath/fixture/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":243732303,"owners_count":20338839,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["golang","testing"],"created_at":"2024-11-21T09:36:09.666Z","updated_at":"2025-10-06T02:09:59.674Z","avatar_url":"https://github.com/halimath.png","language":"Go","funding_links":[],"categories":[],"sub_categories":[],"readme":"# fixture\n\nRe-usable test setups und teardowns for go(lang) `testing` tests.\n\n![CI Status][ci-img-url] \n[![Go Report Card][go-report-card-img-url]][go-report-card-url] \n[![Package Doc][package-doc-img-url]][package-doc-url] \n[![Releases][release-img-url]][release-url]\n\n`fixture` implements a micro-framework ontop of the standard library's \n`testing` package that allow writing of reusable test setup and teardown code.\n\n# Installation\n\nThis module uses golang modules and can be installed with\n\n```shell\ngo get github.com/halimath/fixture@main\n```\n\n# Usage\n\n`fixture` defines a very simple (and assumingly _well-known_) lifecycle for\ncode to execute before, inbetween and after tests. A _fixture_ may hook into\nthis lifecycle to setup or teardown resources needed by the tests. As multiple\ntests may share some amount of these resources `fixture` provides a simple\n_test suite_ functionality that plays well with the resource initialization.\n\nThe lifecycle is shown in the following picture:\n\n![lifecycle](https://www.plantuml.com/plantuml/png/JKyn3i8m3Dpz2giJ8FKBq2AnCR9L7I9mb8ZKgM9twEznWr3nOj-TVROxKLTqcHA0l2FFhhW9xv59rvam5mqPu70wOjkUyKe-5-fJWXtTt3DK-23HMlHUgLGwUcmcwq4rJJ0ooXALBWrg80Qqs0Q6bMJyjwCajAkSnw_djlZ7sab0_8eUeBDi3tm0 \"lifecycle\")\n\nA _fixture_ (in terms of this package) is any go value. A fixture may satisfy\na couple of additional interfaces to execute code at the given lifecycle\nphases. The interfaces are named after the lifecycle phases. Each interface\ncontains a single method (named after the interface) that receives the\n`*testing.T` and returns an `error` which will abort the test (calling\n`t.Fatal`).\n\n## Using a fixture\n\nUsing a fixture is done using the `With` function, which starts a new test \nsuite. Calling `Run` registers a test to run using this fixture.\n\n```go\nWith(t, new(myFixture)).\n\tRun(\"test 1\", func(t *testing.T, f *myFixture) {\n\t\t// Test code\n\t}).\n\tRun(\"test 2\", func(t *testing.T, f *myFixture) {\n\t\t// Test code\n\t})\n```\n\n## Implementing a fixture\n\nTo implement a fixture simply create a type to hold all the values your fixture\nwill provide. You can also add receiver functions to ease interaction with the\nfixture. Then, implement the desired hook interfaces. \n\nTypically, a fixture implements the hook methods via a pointer receiver. This\nallows using just `new` to create a fixture. Use either `BeforeAll` or\n`BeforeEach` to initialize the code.\n\nThe following example uses a fixture to spawn a `httptest.Server` with a simple\nhandler (in a real world the handle would have been some real production code).\nIt provides a `sendRequest` method to send a simple request, handle errors by\nfailing the test and returns the `http.Response`. \n\nThe `TestExample` executes two tests both using the same running server.\n\n```go\n// A simple test fixture holding a httptest.Server.\ntype httpServerFixture struct {\n\tsrv *httptest.Server\n}\n\n// BeforeAll hooks into the fixture lifecycle and creates and starts the\n// httptest.Server before the first test is executed.\nfunc (f *httpServerFixture) BeforeAll(t *testing.T) error {\n\tf.srv = httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {\n\t\tw.Header().Set(\"X-Tracing-Id\", \"1\")\n\t\tw.WriteHeader(http.StatusNoContent)\n\t}))\n\n\treturn nil\n}\n\n// AfterAll hooks into the fixture lifecycle and disposes the httptest.Server\n// after the last test has been executed.\nfunc (f *httpServerFixture) AfterAll(t *testing.T) error {\n\tf.srv.Close()\n\treturn nil\n}\n\n// sendRequest is a convenience function making it easier to read the test code.\nfunc (f *httpServerFixture) sendRequest(t *testing.T) *http.Response {\n\tr, err := http.Get(f.srv.URL)\n\tif err != nil {\n\t\tt.Fatal(err)\n\t}\n\treturn r\n}\n\nfunc TestExample(t *testing.T) {\n\tfixture.With(t, new(httpServerFixture)).\n\t\tRun(\"http status code\", func(t *testing.T, f *httpServerFixture) {\n\t\t\tgot := f.sendRequest(t).StatusCode\n\t\t\tif got != http.StatusNoContent {\n\t\t\t\tt.Errorf(\"expected %d but got %d\", http.StatusNoContent, got)\n\t\t\t}\n\t\t}).\n\t\tRun(\"tracing header\", func(t *testing.T, f *httpServerFixture) {\n\t\t\tresp := f.sendRequest(t)\n\t\t\tgot := resp.Header.Get(\"X-Tracing-Id\")\n\t\t\tif got != \"1\" {\n\t\t\t\tt.Errorf(\"expected %q but got %q\", \"1\", got)\n\t\t\t}\n\t\t})\n}\n```\n\n## Fixtures already provided by `fixture`\n\n`fixture` contains some ready to use generic fixtures. All these fixtures\nhave dependencies only to the standard library and cause no external module to\nbe required.\n\n### TempDir\n\nCreating and removing a temporary directory for filesystem related tests is \neasy with the `TempDirFixture` and the `TempDir` function.\n\n```go\nWith(t, TempDir(\"someprefix\")).\n\tRun(\"create file\", func(t *testing.T, d *TempDirFixture) {\n\t\tf, err := os.Create(d.Join(\"test\"))\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\t\tdefer f.Close()\n\t}).\n\tRun(\"expect file\", func(t *testing.T, d *TempDirFixture) {\n\t\t_, err := os.Stat(d.Join(\"test\"))\n\t\tif err != nil {\n\t\t\tt.Error(err)\n\t\t}\n\t})\n```\n\n### HTTPServerFixture\n\nThe `HTTPServerFixture` creates a HTTP server using `httptest.NewServer` which\nwill be started on `BeforeAll` and closed on `AfterAll`. The server uses a\n`http.ServerMux` as its handler and handler functions can be registered at any\nstage. The server uses HTTP/2 but no TLS; both can be changed easily.\n\n```go\nf := new(HTTPServerFixture)\n\nf.HandleFunc(\"/\", func(w http.ResponseWriter, r *http.Request) {\n\tw.WriteHeader(http.StatusOK)\n})\n\nWith(t, f).\n\tRun(\"/\", func(t *testing.T, f *HTTPServerFixture) {\n\t\tres, err := http.Get(f.URL())\n\t\tif err != nil {\n\t\t\tt.Fatal(err)\n\t\t}\n\n\t\tif res.StatusCode != http.StatusOK {\n\t\t\tt.Errorf(\"expected 200 but got %d\", res.StatusCode)\n\t\t}\n\t})\n```\n\n# License\n\nCopyright 2022 Alexander Metzner.\n\nLicensed under the Apache License, Version 2.0 (the \"License\");\nyou may not use this file except in compliance with the License.\nYou may obtain a copy of the License at\n\n[http://www.apache.org/licenses/LICENSE-2.0](http://www.apache.org/licenses/LICENSE-2.0)\n\nWITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\nSee the License for the specific language governing permissions and\nlimitations under the License.\n\n[ci-img-url]: https://github.com/halimath/fixture/workflows/CI/badge.svg\n[go-report-card-img-url]: https://goreportcard.com/badge/github.com/halimath/fixture\n[go-report-card-url]: https://goreportcard.com/report/github.com/halimath/fixture\n[package-doc-img-url]: https://img.shields.io/badge/GoDoc-Reference-blue.svg\n[package-doc-url]: https://pkg.go.dev/github.com/halimath/fixture\n[release-img-url]: https://img.shields.io/github/v/release/halimath/fixture.svg\n[release-url]: https://github.com/halimath/fixture/releases","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhalimath%2Ffixture","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhalimath%2Ffixture","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhalimath%2Ffixture/lists"}