Skip to content

testing: external test code not compiled or run if contains no tests #8219

@rogpeppe

Description

@rogpeppe
To reproduce, create the following three files:

testit.go

    package testit

extern_test.go:

    package testit_test

    import (
        "code.google.com/p/rog-go/testit"
    )

    func init() {
        testit.Register()
        unused
    }

intern_test.go:

    package testit
    
    import "testing"
    
    var registered bool
    
    func Register() {
        registered = true
    }
    
    func TestPackage(t *testing.T) {
        if !registered {
            t.Fatal("registry never called")
        }
    }

Running the tests gives the error "registry never called",
indicating that the code in extern_test.go is never run.
Moreover, it's not even compiled - there's no error from
the unused variable.

While it may look contrived, the above configuration mirrors the way
that tests are registered with the popular gocheck testing
package - tests that were previously run correctly may now
be silently skipped.

I have verified that this is a regression since Go 1.2.2.

FWIW, there is an easy workaround (define TestPackage in the external
test file) but it might be better to avoid the regression.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions