Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitaly.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPatrick Steinhardt <psteinhardt@gitlab.com>2021-10-06 09:53:36 +0300
committerPatrick Steinhardt <psteinhardt@gitlab.com>2021-10-07 08:37:22 +0300
commitd8b43e951b747acc49a8d1fff1d59be9867a5eca (patch)
treeac836f0ff0cd7db109900e2f0bb7b10a0dfff835 /STYLE.md
parent27de9ce6c8a34f387ea2f3a00ca08fdbb4f74099 (diff)
testhelper: Unify setup of test suites
Setup of a test package's main function is quite repetitive: we call a separate `testMain()` function such that we can use `defer` calls, this function calls `MustHaveNoChildProcess()` and `Configure()`, and then we return the result of `m.Run()`. This is almost always exactly the same, with some exceptions where we need to have additional setup code. Unify this code via a single `testhelper.Run()` function which does all of this. It allows us greater flexibility in the setup code such that we can easily perform additional validation after the tests without having to modify all callsites. Note that this change removes calls to the goleak package in some places. These will resurface in a later commit in this patch series, where we instead move this call into `testhelper.Run()` such that it is executed by default.
Diffstat (limited to 'STYLE.md')
-rw-r--r--STYLE.md20
1 files changed, 4 insertions, 16 deletions
diff --git a/STYLE.md b/STYLE.md
index a67515d6a..e663c397a 100644
--- a/STYLE.md
+++ b/STYLE.md
@@ -238,22 +238,10 @@ importantly, this includes any calls to `log.Fatal()` and related functions.
### Common setup
-When all tests require a common setup, we use the `TestMain()` function for
-this. `TestMain()` must call `os.Exit()` to indicate whether any tests failed.
-As this will cause deferred function calls to not be processed, we use the
-following pattern:
-
-```
-func TestMain(m *testing.M) {
- os.Exit(testMain(m))
-}
-
-func testMain(m *testing.M) int {
- cleanup := testhelper.Configure()
- defer cleanup()
- return m.Run()
-}
-```
+The `TestMain()` function shouldn't do any package-specific setup. Instead, all
+tests are supposed to set up required state as part of the tests themselves. All
+`TestMain()` functions must call `testhelper.Run()` though, which performs the
+setup of global state required for tests.
## Black box and white box testing