Dart-programming-unit-testing
Dartプログラミング-単体テスト
単体テストでは、アプリケーションの個々のユニットをすべてテストします。 開発者が複雑なアプリケーション全体を実行せずに小さな機能をテストするのに役立ちます。
「test」という名前のDart external library は、単体テストを記述および実行する標準的な方法を提供します。
Dartユニットテストには、次の手順が含まれます-
ステップ1:「テスト」パッケージのインストール
現在のプロジェクトにサードパーティパッケージをインストールするには、 pubspec.yaml ファイルが必要です。 テストパッケージ*をインストールするには、まず *pubspec.yaml ファイルに次のエントリを作成します-
dependencies:
test:
エントリを作成したら、 pubspec.yaml ファイルを右クリックして依存関係を取得します。 "test" パッケージがインストールされます。 以下は、 WebStorm エディターでの同じスクリーンショットです。
パッケージは、*コマンドライン*からもインストールできます。 端末に次のように入力します-
pub get
ステップ2:「テスト」パッケージのインポート
import "package:test/test.dart";
ステップ3テストの作成
テストはトップレベル関数* test()を使用して指定され、*テストアサーション*は expect()関数を使用して作成されます。 これらのメソッドを使用するには、 *pub 依存関係としてインストールする必要があります。
構文
test("Description of the test ", () {
expect(actualValue , matchingValue)
});
- group()*関数を使用して、テストをグループ化できます。 各グループの説明は、テストの説明の先頭に追加されます。
構文
group("some_Group_Name", () {
test("test_name_1", () {
expect(actual, equals(exptected));
});
test("test_name_2", () {
expect(actual, equals(expected));
});
})
例1:テストに合格
次の例では、メソッド* Add()を定義しています。 このメソッドは2つの整数値を取り、 *sum を表す整数を返します。 この* add()*メソッドをテストするには-
ステップ1 *-以下のように *test パッケージをインポートします。
ステップ2 *- test()関数を使用してテストを定義します。 ここで、 test()関数は expect()*関数を使用してアサーションを強制します。
import 'package:test/test.dart';
//Import the test package
int Add(int x,int y)
//Function to be tested {
return x+y;
}
void main() {
//Define the test
test("test to check add method",(){
//Arrange
var expected = 30;
//Act
var actual = Add(10,20);
//Asset
expect(actual,expected);
});
}
次の output が生成されるはずです-
00:00 +0: test to check add method
00:00 +1: All tests passed!
例2:テストの失敗
以下で定義されている* subtract()メソッドには論理的な誤りがあります。 次の *test は同じことを検証します。
import 'package:test/test.dart';
int Add(int x,int y){
return x+y;
}
int Sub(int x,int y){
return x-y-1;
}
void main(){
test('test to check sub',(){
var expected = 10;
//Arrange
var actual = Sub(30,20);
//Act
expect(actual,expected);
//Assert
});
test("test to check add method",(){
var expected = 30;
//Arrange
var actual = Add(10,20);
//Act
expect(actual,expected);
//Asset
});
}
出力-関数* add()のテストケースは合格しますが、以下に示すように subtract()*のテストは失敗します。
00:00 +0: test to check sub
00:00 +0 -1: test to check sub
Expected:
Actual:
package:test expect
bin\Test123.dart 18:5 main.<fn>
00:00 +0 -1: test to check add method
00:00 +1 -1: Some tests failed.
Unhandled exception:
Dummy exception to set exit code.
#0 _rootHandleUncaughtError.<anonymous closure> (dart:async/zone.dart:938)
#1 _microtaskLoop (dart:async/schedule_microtask.dart:41)
#2 _startMicrotaskLoop (dart:async/schedule_microtask.dart:50)
#3 _Timer._runTimers (dart:isolate-patch/timer_impl.dart:394)
#4 _Timer._handleMessage (dart:isolate-patch/timer_impl.dart:414)
#5 _RawReceivePortImpl._handleMessage (dart:isolate-patch/isolate_patch.dart:148)
テストケースのグループ化
- テストケース*をグループ化して、テストコードにより多くの意味を追加できます。 多くの*テストケース*がある場合、これはよりクリーンなコードを書くのに役立ちます。
指定されたコードでは、* split()関数と *trim 関数のテストケースを記述しています。 したがって、これらのテストケースを論理的にグループ化し、 String と呼びます。
例
import "package:test/test.dart";
void main() {
group("String", () {
test("test on split() method of string class", () {
var string = "foo,bar,baz";
expect(string.split(","), equals(["foo", "bar", "baz"]));
});
test("test on trim() method of string class", () {
var string = " foo ";
expect(string.trim(), equals("foo"));
});
});
}
出力-出力は、以下に示すように各テストケースのグループ名を追加します-
00:00 +0: String test on split() method of string class
00:00 +1: String test on trim() method of string class
00:00 +2: All tests passed