Dart语言教程:编写和运行单元测试的标准方法

2019-06-2217:46:10编程语言入门到精通Comments3,161 views字数 2782阅读模式

单元测试涉及测试应用程序的每个单元。它可以帮助开发人员在不运行整个复杂应用程序的情况下测试小功能。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

名为“test”的Dart外部库提供了编写和运行单元测试的标准方法。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

Dart单元测试涉及以下步骤 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

第1步:安装 test 包文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

要在当前项目中安装第三方软件包,需要pubspec.yaml文件。要安装 text 包,首先在pubspec.yaml文件中进行以下输入 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

dependencies: 
test:
YAML

输入后,右键单击pubspec.yaml文件并获取依赖项。它将安装 test 包。下面给出了WebStorm编辑器中相同的屏幕截图。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

Dart语言教程:编写和运行单元测试的标准方法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

包也可以从命令行安装。在终端中输入以下内容 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

$ pub get
Shell

第2步:导入 test 包文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

import "package:test/test.dart";
Shell

第3步,编写测试文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

使用顶级函数test()指定测试,而使用expect()函数进行测试断言。要使用这些方法,应将它们安装为pub依赖项。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

语法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

test("Description of the test ", () {  
   expect(actualValue , matchingValue) 
});
Dart

group()函数可用于对测试进行分组。每个组的描述都会添加到测试描述的开头。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

语法文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

group("some_Group_Name", () { 
   test("test_name_1", () { 
      expect(actual, equals(exptected)); 
   });  
   test("test_name_2", () { 
      expect(actual, equals(expected)); 
   }); 
})
Dart

示例1:传递测试

以下示例定义方法Add(),此方法采用两个整数值并返回表示总和的整数。要测试这个add()方法 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

第1步 - 导入测试包,如下所示。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

第2步 - 使用test()函数定义测试。这里test()函数使用expect()函数来强制执行断言。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

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

执行上面示例代码,得到以下结果 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

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

示例2:失败测试

下面定义的subtract()方法存在逻辑错误,下面将测试验证。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

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

输出 - 函数add()的测试用例通过,但subtract()的测试失败,如下所示。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

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)
Shell

分组测试用例

可以对测试用例进行分组,以便为测试代码添加更多含义。如果有许多测试用例,这有助于编写更清晰的代码。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

在给定的代码中,为split()函数和trim函数编写测试用例。因此在逻辑上将这些测试用例分组并为字符串。文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

示例文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

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")); 
      }); 
   }); 
}
Dart

输出将附加每个测试用例的组名称,如下所示 -文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

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

原文出自【易百教程】,商业转载请联系作者获得授权,非商业转载请保留原文链接:https://www.yiibai.com/dart/dart_programming_unit_testing.html#article-start文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html

文章源自菜鸟学院-https://www.cainiaoxueyuan.com/ymba/13774.html
  • 本站内容整理自互联网,仅提供信息存储空间服务,以方便学习之用。如对文章、图片、字体等版权有疑问,请在下方留言,管理员看到后,将第一时间进行处理。
  • 转载请务必保留本文链接:https://www.cainiaoxueyuan.com/ymba/13774.html

Comment

匿名网友 填写信息

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定