Skip to content

Commit

Permalink
Add generic test runner option
Browse files Browse the repository at this point in the history
  • Loading branch information
heppu committed Feb 6, 2024
1 parent 756ca28 commit e6e3ae4
Showing 1 changed file with 37 additions and 7 deletions.
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 @@ func OptCoverDir(coverDir string) Opt {
//
// 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 @@ func OptTestFunc(t *testing.T, fn func(*testing.T)) Opt {
}
}

// 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

0 comments on commit e6e3ae4

Please sign in to comment.