Skip to content

How to determine if running in unittest framework #168

Open
@stuartpa

Description

@stuartpa
Collaborator

There is a place in the code we need to determine if we are running in a unit-test framework (to ensure the code doesn't call os.Exit(), which would shut down the whole testing process (instead we call panic, which just stop the current test).

What's the right way to determine if running in a unit-test framework

// CheckErr passes the error down to cobra.CheckErr (which is likely to call
// os.Exit(1) if err != nil. Although if running in the golang unit test framework
// we do not want to have os.Exit() called, as this exits the unit test runner
// process, and call panic instead so the call stack can be added to the unit test
// output.
func (c *Cmd) CheckErr(err error) {
// If we are in a unit test driver, then panic, otherwise pass down to cobra.CheckErr
if strings.HasSuffix(os.Args[0], ".test") || // are we in go test?
(len(os.Args) > 1 && os.Args[1] == "-test.v") { // are we in goland unittest?
if err != nil {
panic(err)
}
} else {

PR feedback - "this seems like a flimsy convention considering someone else could write a new test framework etc. Can these test frameworks not set some global state instead of relying on command line parsing?" - David

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Type

No type

Projects

No projects

Relationships

None yet

    Development

    No branches or pull requests

      Participants

      @stuartpa@grrlgeek@dlevy-msft

      Issue actions

        How to determine if running in unittest framework · Issue #168 · microsoft/go-sqlcmd