Dart 程式設計 - 單元測試



單元測試涉及測試應用程式的每個獨立單元。它幫助開發人員測試小功能,而無需執行整個複雜的應用程式。

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

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

步驟 1:安裝“test”包

要在當前專案中安裝第三方包,您需要 **pubspec.yaml** 檔案。要安裝 **test 包**,首先在 **pubspec.yaml** 檔案中進行以下條目:

dependencies: 
test:

進行條目後,右鍵單擊 **pubspec.yaml** 檔案並獲取依賴項。它將安裝 **“test”** 包。下面是在 **WebStorm** 編輯器中進行此操作的螢幕截圖。

Unit Testing

也可以從 **命令列** 安裝包。在終端中鍵入以下內容:

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** - 如下所示匯入 **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); 
   }); 
}

它應該產生以下 **輸出**:

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** 函式編寫測試用例。因此,我們從邏輯上對這些測試用例進行分組並將其稱為 **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
廣告