Dart單元測試

單元測試涉及測試應用程式的每個單元。它可以幫助開發人員在不運行整個複雜應用程式的情況下測試小功能。

名為“test”的Dart外部庫提供了編寫和運行單元測試的標準方法。

Dart單元測試涉及以下步驟 -

第1步:安裝 test 包

要在當前專案中安裝第三方軟體包,需要pubspec.yaml檔。要安裝 text 包,首先在pubspec.yaml檔中進行以下輸入 -

dependencies:
test:

輸入後,右鍵單擊pubspec.yaml檔並獲取依賴項。它將安裝 test 包。下麵給出了WebStorm編輯器中相同的螢幕截圖。

包也可以從命令行安裝。在終端中輸入以下內容 -

$ pub get

第2步:導入 test 包

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(),此方法採用兩個整數值並返回表示總和的整數。要測試這個add()方法 -

第1步 - 導入測試包,如下所示。

第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);
   });
}

執行上面示例代碼,得到以下結果 -

00:00 +0: test to check add method
00:00 +1: All tests passed!

示例2:失敗測試

下麵定義的subtract()方法存在邏輯錯誤,下麵將測試驗證。

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: <10>
Actual: <9>
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函數編寫測試用例。因此在邏輯上將這些測試用例分組並為字串。

示例

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

上一篇: Dart併發 下一篇: Dart HTML DOM