testing
1 2 3 4 |
└── mylib ├── human.go ├── math.go ├── math_test.go |
testされるファイル
1 2 3 4 5 6 7 8 9 |
<span class="k">package</span> <span class="n">mylib</span> <span class="k">func</span> <span class="n">Average</span><span class="p">(</span><span class="n">s</span> <span class="p">[]</span><span class="kt">int</span><span class="p">)</span> <span class="kt">int</span> <span class="p">{</span> <span class="n">total</span> <span class="o">:=</span> <span class="m">0</span> <span class="k">for</span> <span class="n">_</span><span class="p">,</span> <span class="n">i</span> <span class="o">:=</span> <span class="k">range</span> <span class="n">s</span> <span class="p">{</span> <span class="n">total</span> <span class="o">+=</span> <span class="n">i</span> <span class="p">}</span> <span class="k">return</span> <span class="kt">int</span><span class="p">(</span><span class="n">total</span> <span class="o">/</span> <span class="nb">len</span><span class="p">(</span><span class="n">s</span><span class="p">))</span> <span class="p">}</span> |
testするファイル
3
が期待される
1 2 3 4 5 6 7 8 9 10 |
<span class="k">package</span> <span class="n">mylib</span> <span class="k">import</span> <span class="s">"testing"</span> <span class="k">func</span> <span class="n">TestAverage</span><span class="p">(</span><span class="n">t</span> <span class="o">*</span><span class="n">testing</span><span class="o">.</span><span class="n">T</span><span class="p">)</span> <span class="p">{</span> <span class="n">v</span> <span class="o">:=</span> <span class="n">Average</span><span class="p">([]</span><span class="kt">int</span><span class="p">{</span><span class="m">1</span><span class="p">,</span> <span class="m">2</span><span class="p">,</span> <span class="m">3</span><span class="p">,</span> <span class="m">4</span><span class="p">,</span> <span class="m">5</span><span class="p">})</span> <span class="k">if</span> <span class="n">v</span> <span class="o">!=</span> <span class="m">3</span> <span class="p">{</span> <span class="n">t</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span><span class="s">"Expected 3, got"</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="p">}</span> <span class="p">}</span> |
run test
- vscodeから
- コマンドラインから
1 2 3 4 |
PS C:\Users\yuta\go\src\awesomeProject> go test ./... ? awesomeProject [no test files] ok awesomeProject/mylib (cached) ? awesomeProject/mylib/under [no test files] |
- 数字変えるとFAILになる
1 2 3 4 5 6 7 8 9 10 |
<span class="k">package</span> <span class="n">mylib</span> <span class="k">import</span> <span class="s">"testing"</span> <span class="k">func</span> <span class="n">TestAverage</span><span class="p">(</span><span class="n">t</span> <span class="o">*</span><span class="n">testing</span><span class="o">.</span><span class="n">T</span><span class="p">)</span> <span class="p">{</span> <span class="n">v</span> <span class="o">:=</span> <span class="n">Average</span><span class="p">([]</span><span class="kt">int</span><span class="p">{</span><span class="m">1</span><span class="p">,</span> <span class="m">2</span><span class="p">,</span> <span class="m">3</span><span class="p">,</span> <span class="m">4</span><span class="p">,</span> <span class="m">5</span><span class="p">,</span> <span class="m">6</span><span class="p">,</span><span class="m">7</span><span class="p">})</span> <span class="k">if</span> <span class="n">v</span> <span class="o">!=</span> <span class="m">3</span> <span class="p">{</span> <span class="n">t</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span><span class="s">"Expected 3, got"</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="p">}</span> <span class="p">}</span> |
1 2 3 4 5 6 |
--- FAIL: TestAverage (0.00s) C:\Users\yuta\go\src\awesomeProject\mylib\math_test.go:8: Expected 3, got 4 FAIL FAIL awesomeProject/mylib 0.374s FAIL |
- Skip処理
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
<span class="k">package</span> <span class="n">mylib</span> <span class="k">import</span> <span class="s">"testing"</span> <span class="k">var</span> <span class="n">Debug</span> <span class="kt">bool</span> <span class="o">=</span> <span class="no">true</span> <span class="k">func</span> <span class="n">TestAverage</span><span class="p">(</span><span class="n">t</span> <span class="o">*</span><span class="n">testing</span><span class="o">.</span><span class="n">T</span><span class="p">)</span> <span class="p">{</span> <span class="c">// Debugがtrueならスキップ</span> <span class="k">if</span> <span class="n">Debug</span> <span class="p">{</span> <span class="n">t</span><span class="o">.</span><span class="n">Skip</span><span class="p">(</span><span class="s">"Skip reason"</span><span class="p">)</span> <span class="p">}</span> <span class="n">v</span> <span class="o">:=</span> <span class="n">Average</span><span class="p">([]</span><span class="kt">int</span><span class="p">{</span><span class="m">1</span><span class="p">,</span> <span class="m">2</span><span class="p">,</span> <span class="m">3</span><span class="p">,</span> <span class="m">4</span><span class="p">,</span> <span class="m">5</span><span class="p">,</span> <span class="m">6</span><span class="p">,</span> <span class="m">7</span><span class="p">})</span> <span class="k">if</span> <span class="n">v</span> <span class="o">!=</span> <span class="m">3</span> <span class="p">{</span> <span class="n">t</span><span class="o">.</span><span class="n">Error</span><span class="p">(</span><span class="s">"Expected 3, got"</span><span class="p">,</span> <span class="n">v</span><span class="p">)</span> <span class="p">}</span> <span class="p">}</span> |
1 2 3 4 5 6 7 8 |
PS C:\Users\yuta\go\src\awesomeProject> go test ./... -v ? awesomeProject [no test files] === RUN TestAverage TestAverage: math_test.go:10: Skip reason --- SKIP: TestAverage (0.00s) PASS ok awesomeProject/mylib 0.368s ? awesomeProject/mylib/under [no test files] |