Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add generic test runner option #194

Merged
merged 1 commit into from
Feb 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
44 changes: 37 additions & 7 deletions integrationtest/opts.go
Original file line number Diff line number Diff line change
Expand Up @@ -104,15 +104,12 @@
//
// Before using this pattern be sure to read how TestMain should be used!
func OptTestMain(m *testing.M) Opt {
return func(itr *IntegrationTestRunner) error {
itr.testRunner = func() error {
if code := m.Run(); code != 0 {
return errors.New("tests have failed")
}
return nil
return OptFuncRunner(func() error {
if code := m.Run(); code != 0 {
return errors.New("tests have failed")

Check warning on line 109 in integrationtest/opts.go

View check run for this annotation

Elisa-Codecov / codecov/patch

integrationtest/opts.go#L108-L109

Added lines #L108 - L109 were not covered by tests
}
return nil
}
})
}

// OptTestFunc allows wrapping testing.T into IntegrationTestRunner.
Expand Down Expand Up @@ -146,6 +143,39 @@
}
}

// OptFuncRunner allows wrapping any function as testRunner function.
// This can be useful injecting code between ready and test code.
// Example TestMain:
//
// func TestMain(m *testing.M) {
// itr := it.NewIntegrationTestRunner(
// it.OptBase("../"),
// it.OptTarget("./cmd/app"),
// it.OptCompose("docker-compose.yaml"),
// it.OptWaitHTTPReady("http://127.0.0.1:8080", time.Second*10),
// it.OptFuncRunner(func() error {
// if err := initStuff(); err != nil {
// return err
// }
// if code := m.Run(); code != 0 {
// return errors.New("tests have failed")
// }
// return nil
// }),
// )
// if err := itr.InitAndRun(); err != nil {
// log.Fatal(err)
// }
// }
//
// Before using this pattern be sure to read how TestMain should be used!
func OptFuncRunner(fn func() error) Opt {
return func(itr *IntegrationTestRunner) error {
itr.testRunner = fn
return nil
}
}

// OptPreHandler adds handler as pre condition for tests to run.
func OptPreHandler(h PreHandler) Opt {
return func(itr *IntegrationTestRunner) error {
Expand Down
Loading