Skip to content
This repository was archived by the owner on Jun 11, 2020. It is now read-only.

Commit c222dd7

Browse files
committed
Add Test Table example to testing
1 parent 1736a68 commit c222dd7

File tree

2 files changed

+60
-6
lines changed

2 files changed

+60
-6
lines changed

docs/testing.html

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,11 @@ <h2>Contents</h2>
7878
<article class="entry-content">
7979
<h1>Testing</h1>
8080

81-
<p>The <a href="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 <a href="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>
8282

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>
8484

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>
8686

8787
<p>For example, if you want to test an <code>Add</code> function in your project:</p>
8888

@@ -102,6 +102,32 @@ <h1>Testing</h1>
102102

103103
<p>Run test using: <code>go test</code></p>
104104

105+
<h2>Test Table</h2>
106+
107+
<p>Use a table structure to setup and test multiple inputs and expected values.</p>
108+
109+
<pre><code class="language-go">func TestAdd(t *testing.T) {
110+
var testData = []struct {
111+
param1 int
112+
param2 int
113+
expect int
114+
}{
115+
{ 1, 2, 3 },
116+
{ 0, 1, 1 },
117+
{ 1, 0, 1 },
118+
{ -1, 1, 0 },
119+
{ -1, -1, -2 },
120+
}
121+
122+
for _, td := range testData {
123+
result := Add(td.param1, td.param2)
124+
if td.expect != result {
125+
t.Errorf(&quot;Expected %s but received %s&quot;, td.expect, result)
126+
}
127+
}
128+
}
129+
</code></pre>
130+
105131
</article>
106132

107133
</main>

pages/testing.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,11 @@ order: 24
66

77
# Testing
88

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.
1010

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)`
1212

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.
1414

1515
For example, if you want to test an `Add` function in your project:
1616

@@ -31,3 +31,31 @@ func TestAdd(t *testing.T) {
3131

3232
Run test using: `go test`
3333

34+
## Test Table
35+
36+
Use a table structure to setup and test multiple inputs and expected values.
37+
38+
```go
39+
func TestAdd(t *testing.T) {
40+
var testData = []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)
56+
}
57+
}
58+
}
59+
```
60+
61+

0 commit comments

Comments
 (0)