github.com/coreos/mantle@v0.13.0/harness/doc.go (about)

     1  // Copyright 2017 CoreOS, Inc.
     2  // Copyright 2009 The Go Authors.
     3  //
     4  // Licensed under the Apache License, Version 2.0 (the "License");
     5  // you may not use this file except in compliance with the License.
     6  // You may obtain a copy of the License at
     7  //
     8  //     http://www.apache.org/licenses/LICENSE-2.0
     9  //
    10  // Unless required by applicable law or agreed to in writing, software
    11  // distributed under the License is distributed on an "AS IS" BASIS,
    12  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  // See the License for the specific language governing permissions and
    14  // limitations under the License.
    15  
    16  // Package harness provides a reusable test framework akin to the standard
    17  // "testing" Go package. For now there is no automated code generation
    18  // component like the "go test" command but that may be a future extension.
    19  // Test functions must be of type `func(*harness.H)` and registered directly
    20  // with a test Suite struct which can then be launched via the Run method.
    21  //
    22  // Within these functions, use the Error, Fail or related methods to signal failure.
    23  //
    24  // Tests may be skipped if not applicable with a call to
    25  // the Skip method of *H:
    26  //     func NeedsSomeData(h *harness.H) {
    27  //         if os.Getenv("SOME_DATA") == "" {
    28  //             h.Skip("skipping test due to missing SOME_DATA")
    29  //         }
    30  //         ...
    31  //     }
    32  //
    33  // Subtests
    34  //
    35  // The Run method of H allow defining subtests,
    36  // without having to define separate functions for each. This enables uses
    37  // like table-driven and hierarchical tests.
    38  // It also provides a way to share common setup and tear-down code:
    39  //
    40  //     func Foo(h *harness.H) {
    41  //         // <setup code>
    42  //         h.Run("A=1", func(h *harness.H) { ... })
    43  //         h.Run("A=2", func(h *harness.H) { ... })
    44  //         h.Run("B=1", func(h *harness.H) { ... })
    45  //         // <tear-down code>
    46  //     }
    47  //
    48  // Each subtest has a unique name: the combination of the name
    49  // of the top-level test and the sequence of names passed to Run, separated by
    50  // slashes, with an optional trailing sequence number for disambiguation.
    51  //
    52  // The argument to the -harness.run command-line flag is an unanchored regular
    53  // expression that matches the test's name. For tests with multiple slash-separated
    54  // elements, such as subtests, the argument is itself slash-separated, with
    55  // expressions matching each name element in turn. Because it is unanchored, an
    56  // empty expression matches any string.
    57  // For example, using "matching" to mean "whose name contains":
    58  //
    59  //     go run foo.go -harness.run ''      # Run all tests.
    60  //     go run foo.go -harness.run Foo     # Run top-level tests matching "Foo", such as "TestFooBar".
    61  //     go run foo.go -harness.run Foo/A=  # For top-level tests matching "Foo", run subtests matching "A=".
    62  //     go run foo.go -harness.run /A=1    # For all top-level tests, run subtests matching "A=1".
    63  //
    64  // Subtests can also be used to control parallelism. A parent test will only
    65  // complete once all of its subtests complete. In this example, all tests are
    66  // run in parallel with each other, and only with each other, regardless of
    67  // other top-level tests that may be defined:
    68  //
    69  //     func GroupedParallel(h *harness.H) {
    70  //         for _, tc := range tests {
    71  //             tc := tc // capture range variable
    72  //             h.Run(tc.Name, func(h *harness.H) {
    73  //                 h.Parallel()
    74  //                 ...
    75  //             })
    76  //         }
    77  //     }
    78  //
    79  // Run does not return until parallel subtests have completed, providing a way
    80  // to clean up after a group of parallel tests:
    81  //
    82  //     func TeardownParallel(h *harness.H) {
    83  //         // This Run will not return until the parallel tests finish.
    84  //         h.Run("group", func(h *harness.H) {
    85  //             h.Run("Test1", parallelTest1)
    86  //             h.Run("Test2", parallelTest2)
    87  //             h.Run("Test3", parallelTest3)
    88  //         })
    89  //         // <tear-down code>
    90  //     }
    91  //
    92  // Suite
    93  //
    94  // Individual tests are grouped into a test suite in order to execute them.
    95  // TODO: this part of the API deviates from the "testing" package and is TBD.
    96  //
    97  // A simple implementation of a test suite:
    98  //
    99  //	func SomeTest(h *harness.H) {
   100  //		h.Skip("TODO")
   101  //	}
   102  //
   103  //	func main() {
   104  //		suite := harness.NewSuite(Options{}, Tests{
   105  //			"SomeTest": SomeTest,
   106  //		})
   107  //		if err := suite.Run(); err != nil {
   108  //			fmt.Fprintln(os.Stderr, err)
   109  //			fmt.Println("FAIL")
   110  //			os.Exit(1)
   111  //		}
   112  //		fmt.Println("PASS")
   113  //	}
   114  //
   115  package harness