Skip to content

proposal: testing: allow TestMain to return int #23404

@rogpeppe

Description

@rogpeppe

Currently TestMain is expected to call os.Exit itself, but that means it's
not possible to use defer to do package-wide test cleanup there,
because the defer will never be called.

I propose that TestMain be optionally allowed to return an integer,
in which case os.Exit will be called with its result.

Then we can write:

 func TestMain(m *testing.M) int {
     defer testhelper.Cleanup()
     return m.Run()
 }

which is arguably more natural and harder to get wrong than calling os.Exit.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions