Skip to content

staticcheck: flag TestMain functions that don't call m.Run #1033

Open
@jacob2964

Description

@jacob2964

Without the os.Exit(m.Run()) line in the TestMain function, control is never passed to the tests and TestMain is the only thing that runs. It can lead to tests appearing to pass when they never actually ran. The TestSanity test "passes" below:

package main_test

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestMain(m *testing.M) {
}

func TestSanity(t *testing.T) {
	assert.Equal(t, 1, 0)
}

It would be nice if there was a linting warning letting the developer know that not having this line could cause unexpected behavior.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions