Text file src/cmd/go/testdata/script/test_main.txt

     1  # Test TestMain
     2  go test standalone_main_normal_test.go
     3  ! stdout '^ok.*\[no tests to run\]'
     4  ! stderr '^ok.*\[no tests to run\]'
     5  stdout '^ok'
     6  
     7  # Test TestMain sees testing flags
     8  go test standalone_testmain_flag_test.go
     9  stdout '^ok.*\[no tests to run\]'
    10  
    11  # Test TestMain with wrong signature (Issue #22388)
    12  ! go test standalone_main_wrong_test.go
    13  stderr 'wrong signature for TestMain, must be: func TestMain\(m \*testing.M\)'
    14  
    15  # Test TestMain does not call os.Exit (Issue #34129)
    16  ! go test standalone_testmain_not_call_os_exit_test.go
    17  ! stdout '^ok'
    18  
    19  -- standalone_main_normal_test.go --
    20  // Copyright 2017 The Go Authors. All rights reserved.
    21  // Use of this source code is governed by a BSD-style
    22  // license that can be found in the LICENSE file.
    23  
    24  package standalone_main_normal_test
    25  
    26  import "testing"
    27  
    28  func TestMain(t *testing.T) {
    29  }
    30  -- standalone_main_wrong_test.go --
    31  // Copyright 2017 The Go Authors. All rights reserved.
    32  // Use of this source code is governed by a BSD-style
    33  // license that can be found in the LICENSE file.
    34  
    35  package standalone_main_wrong_test
    36  
    37  import "testing"
    38  
    39  func TestMain(m *testing.Main) {
    40  }
    41  -- standalone_testmain_flag_test.go --
    42  // Copyright 2019 The Go Authors. All rights reserved.
    43  // Use of this source code is governed by a BSD-style
    44  // license that can be found in the LICENSE file.
    45  
    46  package standalone_testmain_flag_test
    47  
    48  import (
    49  	"flag"
    50  	"fmt"
    51  	"os"
    52  	"testing"
    53  )
    54  
    55  func TestMain(m *testing.M) {
    56  	// A TestMain should be able to access testing flags if it calls
    57  	// flag.Parse without needing to use testing.Init.
    58  	flag.Parse()
    59  	found := false
    60  	flag.VisitAll(func(f *flag.Flag) {
    61  		if f.Name == "test.count" {
    62  			found = true
    63  		}
    64  	})
    65  	if !found {
    66  		fmt.Println("testing flags not registered")
    67  		os.Exit(1)
    68  	}
    69  	os.Exit(m.Run())
    70  }
    71  -- standalone_testmain_not_call_os_exit_test.go --
    72  // Copyright 2020 The Go Authors. All rights reserved.
    73  // Use of this source code is governed by a BSD-style
    74  // license that can be found in the LICENSE file.
    75  
    76  package standalone_testmain_not_call_os_exit_test
    77  
    78  import (
    79  	"testing"
    80  )
    81  
    82  func TestWillFail(t *testing.T) {
    83  	t.Error("this test will fail.")
    84  }
    85  
    86  func TestMain(m *testing.M) {
    87  	defer func() {
    88  		recover()
    89  	}()
    90  	exit := m.Run()
    91  	panic(exit)
    92  }
    93  

View as plain text