Skip to content

Commit e7e57f5

Browse files
testing: add example to package doc
The package doc for the testing package doesn't have a simple example demonstrating how to write a test with an expectation. The doc has simple examples for benchmarks, examples, and skipping, and it would be useful for people new to writing tests in Go. Also moved the skip example further down because it references tests and benchmarks but benchmarks haven't been discussed in detail until the next section. Skip is also a less used feature and it seems misplaced to sit so high up in the package documentation. As an example, Skip is used 570 times the Go code repository which is significantly less than Error and Fatal that are used 23,303 times.
1 parent 0ee8a55 commit e7e57f5

File tree

1 file changed

+17
-6
lines changed

1 file changed

+17
-6
lines changed

src/testing/testing.go

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,13 @@
1717
// package builds but will be included when the ``go test'' command is run.
1818
// For more detail, run ``go help test'' and ``go help testflag''.
1919
//
20-
// Tests and benchmarks may be skipped if not applicable with a call to
21-
// the Skip method of *T and *B:
22-
// func TestTimeConsuming(t *testing.T) {
23-
// if testing.Short() {
24-
// t.Skip("skipping test in short mode.")
20+
// A sample test function looks like this:k
21+
// func TestHello(t *testing.T) {
22+
// got := fmt.Sprintf("hello %s", "world")
23+
// want := "hello world"
24+
// if got != want {
25+
// t.Errorf("got %q, want %q", got, want)
2526
// }
26-
// ...
2727
// }
2828
//
2929
// Benchmarks
@@ -132,6 +132,17 @@
132132
// example function, at least one other function, type, variable, or constant
133133
// declaration, and no test or benchmark functions.
134134
//
135+
// Skipping
136+
//
137+
// Tests and benchmarks may be skipped if not applicable with a call to
138+
// the Skip method of *T and *B:
139+
// func TestTimeConsuming(t *testing.T) {
140+
// if testing.Short() {
141+
// t.Skip("skipping test in short mode.")
142+
// }
143+
// ...
144+
// }
145+
//
135146
// Subtests and Sub-benchmarks
136147
//
137148
// The Run methods of T and B allow defining subtests and sub-benchmarks,

0 commit comments

Comments
 (0)