了解如何在 Exercism 上測試你的 Dart 練習
使用 Exercism CLI 下載你想做的練習吧。
exercism download --track=dart --exercise=hello-world
接著切換到練習所在的目錄。
cd /path/to/exercism/dart/hello-world
執行測試之前,先下載練習的相依套件。
dart pub get
dart test
dart test預設使用精簡報告器,在一行中顯示通過、略過與失敗測試的即時數量。
$ dart test
00:00 +1 -1: test/binary_search_test.dart: finds a value in the middle of an array [E]
Expected: <3>
Actual: <0>
package:matcher expect
test/binary_search_test.dart 13:5 main.<fn>
To run this test again: dart test test/binary_search_test.dart -p vm --plain-name 'finds a value in the middle of an array'
00:00 +1 ~9 -1: Some tests failed.
+1代表 1 個測試通過~9代表 9 個測試略過-1代表 1 個測試失敗若想看到每個測試更詳細的輸出,請使用展開報告器,它會在每個測試執行時顯示結果。
$ dart test -r expanded
00:00 +0: loading test/binary_search_test.dart
00:00 +0: test/binary_search_test.dart: finds a value in an array with one element
00:00 +1: test/binary_search_test.dart: finds a value in the middle of an array
00:00 +1 -1: test/binary_search_test.dart: finds a value in the middle of an array [E]
Expected: <3>
Actual: <0>
package:matcher expect
test/binary_search_test.dart 13:5 main.<fn>
00:00 +1 -1: test/binary_search_test.dart: finds a value at the beginning of an array
00:00 +1 ~1 -1: test/binary_search_test.dart: finds a value at the end of an array
00:00 +1 ~2 -1: test/binary_search_test.dart: finds a value in an array of odd length
00:00 +1 ~3 -1: test/binary_search_test.dart: finds a value in an array of even length
00:00 +1 ~4 -1: test/binary_search_test.dart: identifies that a value is not included in the array
00:00 +1 ~5 -1: test/binary_search_test.dart: a value smaller than the array's smallest value is not found
00:00 +1 ~6 -1: test/binary_search_test.dart: a value larger than the array's largest value is not found
00:00 +1 ~7 -1: test/binary_search_test.dart: nothing is found in an empty array
00:00 +1 ~8 -1: test/binary_search_test.dart: nothing is found when the left and right bounds cross
00:00 +1 ~9 -1: Some tests failed.
大多數練習都包含多個測試,預設情況下除了第一個之外,其餘都會被略過。
void main() {
final twoFer = TwoFer();
group('TwoFer', () {
test('no name given', () {
final result = twoFer.statement();
expect(result, equals('One for you, one for me.'));
}, skip: false);
test('a name given', () {
final result = twoFer.statement('Alice');
expect(result, equals('One for Alice, one for me.'));
}, skip: true);
test('another name given', () {
final result = twoFer.statement('Bob');
expect(result, equals('One for Bob, one for me.'));
}, skip: true);
});
}
如果想一次完成一個測試,就隨著進度把每個測試的 skip: true改成 skip: false。
或者,你也可以不修改檔案,直接一次執行所有測試。
dart test --run-skipped
提交時,線上測試執行器會執行所有測試,包括被略過的測試。
假設你有一個已經解完並提交的練習,隔了一段時間後又回來看它。 當你測試它時,可能會遇到這個錯誤:
$ dart test
...
Could not find a command named "/path/to/frontend_server.dart.snapshot".
解決方法是刪除 pubspec.lock 檔案,然後再次執行 dart 指令。