Write and run unit tests
-The Golang testing framework provides several functionalities and conventions that help structure and execute tests efficiently. To enhance its assertion and mocking capabilities, we use the testify
library.
The Golang testing framework provides several functionalities and conventions that help structure and execute tests efficiently. To enhance its assertion and mocking capabilities, we use the testify
library.
You may find tests using pingcap/check which is a fork of go-check/check in release branches before
@@ -232,9 +232,9 @@release-6.1
, but since that framework is poorly maintained, we are migrated to testify fromrelease-6.1
. You can check the background and progress on the migration tracking issue.Parallel
@@ -273,10 +273,10 @@func TestParent(t *testing.T) { t.Parallel() // <setup code> - t.Run("Serial 0", func(t *testing.T) { ... }) - t.Run("Serial 1", func(t *testing.T) { ... }) - t.Run("Serial 2", func(t *testing.T) { ... }) + t.Run("Serial 0", func(t *testing.T) { ... }) + t.Run("Serial 1", func(t *testing.T) { ... }) + t.Run("Serial 2", func(t *testing.T) { ... }) // <tear-down code> }
R
or if it is an older test not using testify
-make failpoint-enable -(cd pkg/planner/core ; go test -v -run "^TestT$" -check.f TestBinaryOpFunction ) +(cd pkg/planner/core ; go test -v -run "^TestT$" -check.f TestBinaryOpFunction ) make failpoint-disable
If one want to compile the test into a debug binary for running in a debugger, one can also use
+go test -gcflags="all=-N -l" -o ./t
, which removes any optimisations and outputs at
binary file ready to be used, likedlv exec ./t
or combine it with the above to only debug a single testdlv exec ./t -- -test.run "^TestT$" -check.f TestBinaryOpFunction
.If one want to compile the test into a debug binary for running in a debugger, one can also use
go test -gcflags="all=-N -l" -o ./t
, which removes any optimisations and outputs at
binary file ready to be used, likedlv exec ./t
or combine it with the above to only debug a single testdlv exec ./t -- -test.run "^TestT$" -check.f TestBinaryOpFunction
.Notice there is also an
ut
utility for running tests, seeMakefile
andtools/bin/ut
.To display information on all the test flags, enter
go help testflag
.If you develop with GoLand, you can also run a test from the IDE with manually enabled and disabled fail points. See the documentation for details.
@@ -292,7 +292,7 @@
/retest
-Rerun all failed CI test cases.
+Rerun all failed CI test cases.
/test {{test1}} {{testN}}
Run given CI failed tests.
CI parameters
@@ -321,7 +321,7 @@Assertion f Test: TestTopology --- FAIL: TestTopology (0.76s) -
To find this type of failure, enter
+grep -i "FAIL"
to search the report output.To find this type of failure, enter
grep -i "FAIL"
to search the report output.Data race
Golang testing supports detecting data race by running tests with the
-race
flag. Its failure report looks like:[2021-06-21T15:36:38.766Z] ================== diff --git a/index.html b/index.html index f5ea3204..160e9295 100644 --- a/index.html +++ b/index.html @@ -178,7 +178,7 @@
TiDB Development Guide
TiDB Development Guide
- +About this guide