[go] testing: explain using a _test package

155 views
Skip to first unread message

Gerrit Bot (Gerrit)

unread,
Aug 1, 2022, 3:42:30 AM8/1/22
to goph...@pubsubhelper.golang.org, Brian, golang-co...@googlegroups.com

Gerrit Bot has uploaded this change for review.

View Change

testing: explain using a _test package

The existing documentation did not explain the difference between
placing a _test.go file in the same package as what is being
tested vs. adding it to a seprate _test package. This explains the
distinction and adds an example.

Concept is explained well here: https://stackoverflow.com/a/31443271

Fixes #25223

Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
GitHub-Last-Rev: 8e4322ab3c7d927e65741ec190598a011794b5f1
GitHub-Pull-Request: golang/go#54160
---
M src/testing/testing.go
1 file changed, 52 insertions(+), 4 deletions(-)

diff --git a/src/testing/testing.go b/src/testing/testing.go
index ec2d864..f2d454e 100644
--- a/src/testing/testing.go
+++ b/src/testing/testing.go
@@ -14,12 +14,19 @@
// Within these functions, use the Error, Fail or related methods to signal failure.
//
// To write a new test suite, create a file whose name ends _test.go that
-// contains the TestXxx functions as described here. Put the file in the same
-// package as the one being tested. The file will be excluded from regular
+// contains the TestXxx functions as described here.
+// The file will be excluded from regular
// package builds but will be included when the "go test" command is run.
-// For more detail, run "go help test" and "go help testflag".
//
-// A simple test function looks like this:
+// The test file can either be in the same package as the one being tested,
+// or you can place it in a package with the _test suffix.
+//
+// If the test file is in the package you are testing, then you can can access
+// all identifiers within the package, such as in this example:
+//
+// package abs
+//
+// import "testing"
//
// func TestAbs(t *testing.T) {
// got := Abs(-1)
@@ -28,6 +35,27 @@
// }
// }
//
+// If the test files is in a _test package, then you must import the package you
+// wish to test and then reference the exported identifiers.
+// This is known as "black box" testing.
+//
+// package abs_test
+//
+// import (
+// "testing"
+//
+// "path_to_pkg/abs"
+// )
+//
+// func TestAbs(t *testing.T) {
+// got := abs.Abs(-1)
+// if got != 1 {
+// t.Errorf("Abs(-1) = %d; want 1", got)
+// }
+// }
+//
+// For more detail, run "go help test" and "go help testflag".
+//
// # Benchmarks
//
// Functions of the form

To view, visit change 420415. To unsubscribe, or for help writing mail filters, visit settings.

Gerrit-Project: go
Gerrit-Branch: master
Gerrit-Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
Gerrit-Change-Number: 420415
Gerrit-PatchSet: 1
Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
Gerrit-CC: Brian <bcyn...@gmail.com>
Gerrit-MessageType: newchange

Gopher Robot (Gerrit)

unread,
Aug 1, 2022, 8:11:58 AM8/1/22
to Gerrit Bot, Brian, goph...@pubsubhelper.golang.org, Bryan Mills, Marcel van Lohuizen, golang-co...@googlegroups.com

Attention is currently required from: Bryan Mills.

Congratulations on opening your first change. Thank you for your contribution!

Next steps:
A maintainer will review your change and provide feedback. See
https://go.dev/doc/contribute#review for more info and tips to get your
patch through code review.

Most changes in the Go project go through a few rounds of revision. This can be
surprising to people new to the project. The careful, iterative review process
is our way of helping mentor contributors and ensuring that their contributions
have a lasting impact.

During May-July and Nov-Jan the Go project is in a code freeze, during which
little code gets reviewed or merged. If a reviewer responds with a comment like
R=go1.11 or adds a tag like "wait-release", it means that this CL will be
reviewed as part of the next development cycle. See https://go.dev/s/release
for more details.

View Change

    To view, visit change 420415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
    Gerrit-Change-Number: 420415
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-CC: Brian <bcyn...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Marcel van Lohuizen <mp...@golang.org>
    Gerrit-Attention: Bryan Mills <bcm...@google.com>
    Gerrit-Comment-Date: Mon, 01 Aug 2022 12:11:55 +0000
    Gerrit-HasComments: No
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Bryan Mills (Gerrit)

    unread,
    Aug 1, 2022, 10:14:07 AM8/1/22
    to Gerrit Bot, Brian, goph...@pubsubhelper.golang.org, Bryan Mills, Marcel van Lohuizen, Gopher Robot, golang-co...@googlegroups.com

    View Change

    1 comment:

    • File src/testing/testing.go:

      • Patch Set #1, Line 22: you

        (nit) The rest of the docs for this package avoid the word “you”, instead preferring the passive voice.

        So, something like:
        ```
        // The test file can be in the same package as the one being tested,
        // or in a corresponding package with the suffix "_test".
        //
        // If the test file is in the same package, it may refer to unexported
        // identifiers within the package, as in this example:

        // If the file is in a separate "_test" package, the package being tested
        // must be imported explicitly and only its exported identifiers may be used:

        ```

    To view, visit change 420415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
    Gerrit-Change-Number: 420415
    Gerrit-PatchSet: 1
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-CC: Brian <bcyn...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Marcel van Lohuizen <mp...@golang.org>
    Gerrit-Comment-Date: Mon, 01 Aug 2022 14:14:03 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Gerrit-MessageType: comment

    Gerrit Bot (Gerrit)

    unread,
    Aug 1, 2022, 1:23:11 PM8/1/22
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Gerrit Bot uploaded patch set #2 to this change.

    View Change

    testing: explain using a _test package

    The existing documentation did not explain the difference between
    placing a _test.go file in the same package as what is being
    tested vs. adding it to a seprate _test package. This explains the
    distinction and adds an example.

    Concept is explained well here: https://stackoverflow.com/a/31443271

    Fixes #25223

    Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
    GitHub-Last-Rev: 5d6f4fa6e423b57894112a35bd684bf18285380a

    GitHub-Pull-Request: golang/go#54160
    ---
    M src/testing/testing.go
    1 file changed, 52 insertions(+), 4 deletions(-)

    To view, visit change 420415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
    Gerrit-Change-Number: 420415
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-CC: Brian <bcyn...@gmail.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Marcel van Lohuizen <mp...@golang.org>
    Gerrit-MessageType: newpatchset

    Brian (Gerrit)

    unread,
    Aug 4, 2022, 1:10:15 PM8/4/22
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Bryan Mills, Marcel van Lohuizen, Gopher Robot, golang-co...@googlegroups.com

    Attention is currently required from: Bryan Mills.

    Patch set 2:Code-Review +1

    View Change

    1 comment:

    • File src/testing/testing.go:

      • (nit) The rest of the docs for this package avoid the word “you”, instead preferring the passive voi […]

        Done. That definitely reads better, thanks!

    To view, visit change 420415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
    Gerrit-Change-Number: 420415
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Brian <bcyn...@gmail.com>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Marcel van Lohuizen <mp...@golang.org>
    Gerrit-Attention: Bryan Mills <bcm...@google.com>
    Gerrit-Comment-Date: Wed, 03 Aug 2022 21:09:07 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    Comment-In-Reply-To: Bryan Mills <bcm...@google.com>
    Gerrit-MessageType: comment

    Brian (Gerrit)

    unread,
    Aug 4, 2022, 5:36:02 PM8/4/22
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Bryan Mills, Marcel van Lohuizen, Gopher Robot, golang-co...@googlegroups.com

    Attention is currently required from: Bryan Mills.

    Patch set 2:-Code-Review

    View Change

    1 comment:

    • Patchset:

      • Patch Set #2:

        Reverting the accidental self review; apologies for the noise!

    To view, visit change 420415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
    Gerrit-Change-Number: 420415
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Marcel van Lohuizen <mp...@golang.org>
    Gerrit-Attention: Bryan Mills <bcm...@google.com>
    Gerrit-Comment-Date: Thu, 04 Aug 2022 21:35:58 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: Yes
    Gerrit-MessageType: comment

    Dan Kortschak (Gerrit)

    unread,
    Aug 4, 2022, 6:49:54 PM8/4/22
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Bryan Mills, Marcel van Lohuizen, Gopher Robot, golang-co...@googlegroups.com

    Attention is currently required from: Bryan Mills.

    View Change

    1 comment:

    To view, visit change 420415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
    Gerrit-Change-Number: 420415
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-CC: Dan Kortschak <d...@kortschak.io>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Marcel van Lohuizen <mp...@golang.org>
    Gerrit-Attention: Bryan Mills <bcm...@google.com>
    Gerrit-Comment-Date: Thu, 04 Aug 2022 22:49:48 +0000

    Brian (Gerrit)

    unread,
    Aug 4, 2022, 10:54:41 PM8/4/22
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Dan Kortschak, Bryan Mills, Marcel van Lohuizen, Gopher Robot, golang-co...@googlegroups.com

    Attention is currently required from: Bryan Mills, Dan Kortschak.

    View Change

    1 comment:

    • Commit Message:

      • Done. English spelling is the worst.

    To view, visit change 420415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
    Gerrit-Change-Number: 420415
    Gerrit-PatchSet: 2
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-CC: Brian <bcyn...@gmail.com>
    Gerrit-CC: Dan Kortschak <d...@kortschak.io>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Marcel van Lohuizen <mp...@golang.org>
    Gerrit-Attention: Bryan Mills <bcm...@google.com>
    Gerrit-Attention: Dan Kortschak <d...@kortschak.io>
    Gerrit-Comment-Date: Fri, 05 Aug 2022 02:54:30 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No
    Comment-In-Reply-To: Dan Kortschak <d...@kortschak.io>
    Gerrit-MessageType: comment

    Gerrit Bot (Gerrit)

    unread,
    Aug 4, 2022, 10:55:51 PM8/4/22
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Attention is currently required from: Bryan Mills, Dan Kortschak.

    Gerrit Bot uploaded patch set #3 to this change.

    View Change

    testing: explain using a _test package

    The existing documentation did not explain the difference between
    placing a _test.go file in the same package as what is being
    tested vs. adding it to a separate _test package. This explains the

    distinction and adds an example.

    Concept is explained well here: https://stackoverflow.com/a/31443271

    Fixes #25223

    Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
    GitHub-Last-Rev: 43cef699ed47cd5cad72253a2f88795e314c7496

    GitHub-Pull-Request: golang/go#54160
    ---
    M src/testing/testing.go
    1 file changed, 52 insertions(+), 4 deletions(-)

    To view, visit change 420415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
    Gerrit-Change-Number: 420415
    Gerrit-PatchSet: 3
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-CC: Brian <bcyn...@gmail.com>
    Gerrit-CC: Dan Kortschak <d...@kortschak.io>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Marcel van Lohuizen <mp...@golang.org>
    Gerrit-Attention: Bryan Mills <bcm...@google.com>
    Gerrit-Attention: Dan Kortschak <d...@kortschak.io>
    Gerrit-MessageType: newpatchset

    Bryan Mills (Gerrit)

    unread,
    Aug 24, 2022, 5:16:30 PM8/24/22
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Bryan Mills, Brian, Dan Kortschak, Marcel van Lohuizen, Gopher Robot, golang-co...@googlegroups.com

    Attention is currently required from: Dan Kortschak.

    Patch set 3:Run-TryBot +1Code-Review +2

    View Change

    2 comments:

    • File src/testing/testing.go:

      • Patch Set #3, Line 28:

        //
        // import "testing"
        //

        (not sure) Do the blank lines in these code blocks need trailing tabs to keep each snippet in a single block?

      • Patch Set #3, Line 40: T

        s/This/this/ (since the previous line ends in a colon)

    To view, visit change 420415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
    Gerrit-Change-Number: 420415
    Gerrit-PatchSet: 3
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-CC: Brian <bcyn...@gmail.com>
    Gerrit-CC: Dan Kortschak <d...@kortschak.io>
    Gerrit-CC: Gopher Robot <go...@golang.org>
    Gerrit-CC: Marcel van Lohuizen <mp...@golang.org>
    Gerrit-Attention: Dan Kortschak <d...@kortschak.io>
    Gerrit-Comment-Date: Wed, 24 Aug 2022 21:16:26 +0000

    Gerrit Bot (Gerrit)

    unread,
    Aug 27, 2022, 7:02:37 PM8/27/22
    to goph...@pubsubhelper.golang.org, golang-co...@googlegroups.com

    Attention is currently required from: Bryan Mills, Dan Kortschak.

    Gerrit Bot uploaded patch set #4 to this change.

    View Change

    The following approvals got outdated and were removed: Run-TryBot+1 by Bryan Mills, TryBot-Result+1 by Gopher Robot

    testing: explain using a _test package

    The existing documentation did not explain the difference between
    placing a _test.go file in the same package as what is being
    tested vs. adding it to a separate _test package. This explains the
    distinction and adds an example.

    Concept is explained well here: https://stackoverflow.com/a/31443271

    Fixes #25223

    Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
    GitHub-Last-Rev: 7f49c5f4624b358af8052272da8ac3240751ada0

    GitHub-Pull-Request: golang/go#54160
    ---
    M src/testing/testing.go
    1 file changed, 52 insertions(+), 4 deletions(-)

    To view, visit change 420415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
    Gerrit-Change-Number: 420415
    Gerrit-PatchSet: 4
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-CC: Brian <bcyn...@gmail.com>
    Gerrit-CC: Dan Kortschak <d...@kortschak.io>
    Gerrit-CC: Marcel van Lohuizen <mp...@golang.org>

    Brian (Gerrit)

    unread,
    Aug 27, 2022, 7:05:12 PM8/27/22
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Gopher Robot, Bryan Mills, Dan Kortschak, Marcel van Lohuizen, golang-co...@googlegroups.com

    Attention is currently required from: Bryan Mills, Dan Kortschak.

    View Change

    2 comments:

    • File src/testing/testing.go:

      • (not sure) Do the blank lines in these code blocks need trailing tabs to keep each snippet in a sing […]

        The codeblock under # Example lower down doesn't have a trailing tab, so I don't think they are needed.
        I also built it with `godoc` and it was a single block.

      • Replaced the colon with a period.

    To view, visit change 420415. To unsubscribe, or for help writing mail filters, visit settings.

    Gerrit-Project: go
    Gerrit-Branch: master
    Gerrit-Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
    Gerrit-Change-Number: 420415
    Gerrit-PatchSet: 4
    Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
    Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
    Gerrit-Reviewer: Gopher Robot <go...@golang.org>
    Gerrit-CC: Brian <bcyn...@gmail.com>
    Gerrit-CC: Dan Kortschak <d...@kortschak.io>
    Gerrit-CC: Marcel van Lohuizen <mp...@golang.org>
    Gerrit-Attention: Bryan Mills <bcm...@google.com>
    Gerrit-Attention: Dan Kortschak <d...@kortschak.io>
    Gerrit-Comment-Date: Sat, 27 Aug 2022 23:05:08 +0000
    Gerrit-HasComments: Yes
    Gerrit-Has-Labels: No

    Bryan Mills (Gerrit)

    unread,
    Aug 29, 2022, 10:03:26 AM8/29/22
    to Gerrit Bot, goph...@pubsubhelper.golang.org, Bryan Mills, Gopher Robot, Brian, Dan Kortschak, Marcel van Lohuizen, golang-co...@googlegroups.com

    Attention is currently required from: Dan Kortschak.

    Patch set 4:Run-TryBot +1Auto-Submit +1Code-Review +2

    View Change

      To view, visit change 420415. To unsubscribe, or for help writing mail filters, visit settings.

      Gerrit-Project: go
      Gerrit-Branch: master
      Gerrit-Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
      Gerrit-Change-Number: 420415
      Gerrit-PatchSet: 4
      Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
      Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
      Gerrit-Reviewer: Gopher Robot <go...@golang.org>
      Gerrit-CC: Brian <bcyn...@gmail.com>
      Gerrit-CC: Dan Kortschak <d...@kortschak.io>
      Gerrit-CC: Marcel van Lohuizen <mp...@golang.org>
      Gerrit-Attention: Dan Kortschak <d...@kortschak.io>
      Gerrit-Comment-Date: Mon, 29 Aug 2022 14:03:22 +0000
      Gerrit-HasComments: No
      Gerrit-Has-Labels: Yes
      Gerrit-MessageType: comment

      Heschi Kreinick (Gerrit)

      unread,
      Aug 29, 2022, 4:08:09 PM8/29/22
      to Gerrit Bot, goph...@pubsubhelper.golang.org, Gopher Robot, Bryan Mills, Brian, Dan Kortschak, Marcel van Lohuizen, golang-co...@googlegroups.com

      Attention is currently required from: Dan Kortschak.

      Patch set 4:Code-Review +1

      View Change

        To view, visit change 420415. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
        Gerrit-Change-Number: 420415
        Gerrit-PatchSet: 4
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
        Gerrit-Reviewer: Gopher Robot <go...@golang.org>
        Gerrit-Reviewer: Heschi Kreinick <hes...@google.com>
        Gerrit-CC: Brian <bcyn...@gmail.com>
        Gerrit-CC: Dan Kortschak <d...@kortschak.io>
        Gerrit-CC: Marcel van Lohuizen <mp...@golang.org>
        Gerrit-Attention: Dan Kortschak <d...@kortschak.io>
        Gerrit-Comment-Date: Mon, 29 Aug 2022 20:08:04 +0000

        Gopher Robot (Gerrit)

        unread,
        Aug 29, 2022, 4:08:15 PM8/29/22
        to Gerrit Bot, goph...@pubsubhelper.golang.org, golang-...@googlegroups.com, Heschi Kreinick, Bryan Mills, Brian, Dan Kortschak, Marcel van Lohuizen, golang-co...@googlegroups.com

        Gopher Robot submitted this change.

        View Change


        Approvals: Gopher Robot: TryBots succeeded Bryan Mills: Looks good to me, approved; Run TryBots; Automatically submit change Heschi Kreinick: Looks good to me, but someone else must approve
        testing: explain using a _test package

        The existing documentation did not explain the difference between
        placing a _test.go file in the same package as what is being
        tested vs. adding it to a separate _test package. This explains the
        distinction and adds an example.

        Concept is explained well here: https://stackoverflow.com/a/31443271

        Fixes #25223

        Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
        GitHub-Last-Rev: 7f49c5f4624b358af8052272da8ac3240751ada0
        GitHub-Pull-Request: golang/go#54160
        Reviewed-on: https://go-review.googlesource.com/c/go/+/420415
        Reviewed-by: Bryan Mills <bcm...@google.com>
        TryBot-Result: Gopher Robot <go...@golang.org>
        Reviewed-by: Heschi Kreinick <hes...@google.com>
        Auto-Submit: Bryan Mills <bcm...@google.com>
        Run-TryBot: Bryan Mills <bcm...@google.com>
        ---
        M src/testing/testing.go
        1 file changed, 58 insertions(+), 4 deletions(-)

        diff --git a/src/testing/testing.go b/src/testing/testing.go
        index a38b40e..5fd1539 100644

        --- a/src/testing/testing.go
        +++ b/src/testing/testing.go
        @@ -14,12 +14,19 @@
        // Within these functions, use the Error, Fail or related methods to signal failure.
        //
        // To write a new test suite, create a file whose name ends _test.go that
        -// contains the TestXxx functions as described here. Put the file in the same
        -// package as the one being tested. The file will be excluded from regular
        +// contains the TestXxx functions as described here.
        +// The file will be excluded from regular
        // package builds but will be included when the "go test" command is run.
        -// For more detail, run "go help test" and "go help testflag".
        //
        -// A simple test function looks like this:
        +// The test file can be in the same package as the one being tested,
        +// or in a corresponding package with the suffix "_test".
        +//
        +// If the test file is in the same package, it may refer to unexported
        +// identifiers within the package, as in this example:

        +//
        +// package abs
        +//
        +// import "testing"
        //
        // func TestAbs(t *testing.T) {
        // got := Abs(-1)
        @@ -28,6 +35,27 @@
        // }
        // }
        //
        +// If the file is in a separate "_test" package, the package being tested
        +// must be imported explicitly and only its exported identifiers may be used.

        +// This is known as "black box" testing.
        +//
        +// package abs_test
        +//
        +// import (
        +// "testing"
        +//
        +// "path_to_pkg/abs"
        +// )
        +//
        +// func TestAbs(t *testing.T) {
        +// got := abs.Abs(-1)
        +// if got != 1 {
        +// t.Errorf("Abs(-1) = %d; want 1", got)
        +// }
        +// }
        +//
        +// For more detail, run "go help test" and "go help testflag".
        +//
        // # Benchmarks
        //
        // Functions of the form

        To view, visit change 420415. To unsubscribe, or for help writing mail filters, visit settings.

        Gerrit-Project: go
        Gerrit-Branch: master
        Gerrit-Change-Id: Iebaba15207d8aa24f0b370d8dd4062eadb504b5c
        Gerrit-Change-Number: 420415
        Gerrit-PatchSet: 5
        Gerrit-Owner: Gerrit Bot <letsus...@gmail.com>
        Gerrit-Reviewer: Bryan Mills <bcm...@google.com>
        Gerrit-Reviewer: Gopher Robot <go...@golang.org>
        Gerrit-Reviewer: Heschi Kreinick <hes...@google.com>
        Gerrit-CC: Brian <bcyn...@gmail.com>
        Gerrit-CC: Dan Kortschak <d...@kortschak.io>
        Gerrit-CC: Marcel van Lohuizen <mp...@golang.org>
        Gerrit-MessageType: merged
        Reply all
        Reply to author
        Forward
        0 new messages