Skip to content

Commit aba370f

Browse files
committed
Add how to test code
1 parent 2a5b2ca commit aba370f

File tree

2 files changed

+72
-1
lines changed

2 files changed

+72
-1
lines changed

Diff for: src/SUMMARY.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@
2020
- [コンテスト開始後の流れ (TODO)](./participate/during-contest/index.md)
2121
- [テストケースの作成 (TODO)](./participate/during-contest/write-tests.md)
2222
- [プログラムの作成 (TODO)](./participate/during-contest/write-program.md)
23-
- [プログラムのテスト (TODO)](./todo.md)
23+
- [プログラムのテスト (TODO)](./participate/during-contest/test.md)
2424
- [プログラムの提出 (TODO)](./todo.md)
2525
- [Tips(小技集) (TODO)](./todo.md)
2626
- [クレートの使用例 (TODO)](./todo.md)

Diff for: src/participate/during-contest/test.md

+71
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,71 @@
1+
# プログラムのテスト
2+
3+
**TODO** このページは書きかけです。
4+
5+
一通り書き終わったと思ったら、提出前にまずは一度サンプルケースで答えが合うかどうかを確認してみましょう。コマンド プロンプトまたは端末を開いてプロジェクトフォルダに移動して、次のようなコマンドを実行します。
6+
7+
```console
8+
$ cargo test
9+
Compiling abc000 v0.1.0 (C:\Users\dicen\workspace\daily\2020\0406\abc000)
10+
Finished dev [unoptimized + debuginfo] target(s) in 0.52s
11+
Running target\debug\deps\main-1ecbd097f851d76e.exe
12+
13+
running 0 tests
14+
15+
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
16+
17+
Running target\debug\deps\sample_inputs-05a0de7d755e5cc1.exe
18+
19+
running 3 tests
20+
No
21+
test sample3 ... ok
22+
Yes
23+
test sample1 ... ok
24+
No
25+
test sample2 ... ok
26+
27+
test result: ok. 3 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
28+
```
29+
30+
すると、このようにテスト結果が表示されます。このように最後に `test result: ok.` となっていれば、すべてのテストに成功しているということになります。一方で、もし成功しないテストがあれば、次のようになります。
31+
32+
```console
33+
$ cargo test
34+
Compiling abc000 v0.1.0 (C:\Users\dicen\workspace\daily\2020\0406\abc000)
35+
Finished dev [unoptimized + debuginfo] target(s) in 0.68s
36+
Running target\debug\deps\main-1ecbd097f851d76e.exe
37+
38+
running 0 tests
39+
40+
test result: ok. 0 passed; 0 failed; 0 ignored; 0 measured; 0 filtered out
41+
42+
Running target\debug\deps\sample_inputs-05a0de7d755e5cc1.exe
43+
44+
running 3 tests
45+
No
46+
test sample2 ... ok
47+
Yes
48+
No
49+
test sample1 ... FAILED
50+
test sample3 ... ok
51+
52+
failures:
53+
54+
---- sample1 stdout ----
55+
thread 'sample1' panicked at 'assertion failed: `(left == right)`
56+
left: `"Yes\n"`,
57+
right: `"No\n"`', tests\sample_inputs.rs:16:5
58+
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace.
59+
60+
61+
failures:
62+
sample1
63+
64+
test result: FAILED. 2 passed; 1 failed; 0 ignored; 0 measured; 0 filtered out
65+
66+
error: test failed, to rerun pass '--test sample_inputs'
67+
```
68+
69+
最後の `test result: FAILED.` により失敗したことが示されています。その横には成功と失敗の個数が示されています。そして上には失敗したときのコマンドの出力も示されています。 `assertion failed` 以降から、 `No` と出力すべきところを `Yes` と出力してしまったことがわかります。
70+
71+
> Note: メッセージに示される `left:``right:` はマクロ `assert_eq!(left, right);` に渡される引数の左右です。用意したテストコードでは実際の出力を左、期待される出力を右として `assert_eq!(output.stdout_str(), "No\n");` としていたことから、本来は `No` と出力すべきところを `Yes` と表示した、ということがわかります。

0 commit comments

Comments
 (0)