You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
This repository was archived by the owner on Jun 11, 2020. It is now read-only.
Copy file name to clipboardExpand all lines: docs/testing.html
+29-3Lines changed: 29 additions & 3 deletions
Original file line number
Diff line number
Diff line change
@@ -78,11 +78,11 @@ <h2>Contents</h2>
78
78
<articleclass="entry-content">
79
79
<h1>Testing</h1>
80
80
81
-
<p>The <ahref="https://golang.org/pkg/testing/">Go testing package</a> supports creating unit test in go. Your test code is written in plain go, there is not a new assert language or syntax to learn. You write go code, and then Fail or Error out to signal the test did not pass.</p>
81
+
<p>The <ahref="https://golang.org/pkg/testing/">Go testing package</a> supports creating unit test in go. Your test code is written in plain go, there is not a new assert language or syntax to learn. Write go code and use <code>t.Fail</code> or <code>t.Error</code> to signal the test did not pass.</p>
82
82
83
-
<p>To write a new test created a test file, ending in <code>_test.go</code>which includes functions with the following signature: <code>func TestXxx(*testing.T)</code></p>
83
+
<p>To write a new test create a test file, ending in <code>_test.go</code>that includes functions with the following signature: <code>func TestXxx(*testing.T)</code></p>
84
84
85
-
<p>Note: The function name must start with <code>Test</code> and include a capitalized second part which is used to identify the test routing.</p>
85
+
<p>Note: The function name must start with <code>Test</code> and include a capitalized second part to identify the test routing.</p>
86
86
87
87
<p>For example, if you want to test an <code>Add</code> function in your project:</p>
88
88
@@ -102,6 +102,32 @@ <h1>Testing</h1>
102
102
103
103
<p>Run test using: <code>go test</code></p>
104
104
105
+
<h2>Test Table</h2>
106
+
107
+
<p>Use a table structure to setup and test multiple inputs and expected values.</p>
Copy file name to clipboardExpand all lines: pages/testing.md
+31-3Lines changed: 31 additions & 3 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -6,11 +6,11 @@ order: 24
6
6
7
7
# Testing
8
8
9
-
The [Go testing package](https://golang.org/pkg/testing/) supports creating unit test in go. Your test code is written in plain go, there is not a new assert language or syntax to learn. You write go code, and then Fail or Error out to signal the test did not pass.
9
+
The [Go testing package](https://golang.org/pkg/testing/) supports creating unit test in go. Your test code is written in plain go, there is not a new assert language or syntax to learn. Write go code and use `t.Fail` or `t.Error` to signal the test did not pass.
10
10
11
-
To write a new test created a test file, ending in `_test.go`which includes functions with the following signature: `func TestXxx(*testing.T)`
11
+
To write a new test create a test file, ending in `_test.go`that includes functions with the following signature: `func TestXxx(*testing.T)`
12
12
13
-
Note: The function name must start with `Test` and include a capitalized second part which is used to identify the test routing.
13
+
Note: The function name must start with `Test` and include a capitalized second part to identify the test routing.
14
14
15
15
For example, if you want to test an `Add` function in your project:
16
16
@@ -31,3 +31,31 @@ func TestAdd(t *testing.T) {
31
31
32
32
Run test using: `go test`
33
33
34
+
## Test Table
35
+
36
+
Use a table structure to setup and test multiple inputs and expected values.
37
+
38
+
```go
39
+
funcTestAdd(t *testing.T) {
40
+
vartestData = []struct {
41
+
param1 int
42
+
param2 int
43
+
expect int
44
+
}{
45
+
{ 1, 2, 3 },
46
+
{ 0, 1, 1 },
47
+
{ 1, 0, 1 },
48
+
{ -1, 1, 0 },
49
+
{ -1, -1, -2 },
50
+
}
51
+
52
+
for_, td:=range testData {
53
+
result:=Add(td.param1, td.param2)
54
+
if td.expect != result {
55
+
t.Errorf("Expected %s but received %s", td.expect, result)
0 commit comments