- Dart Programming - HTML DOM
- Dart Programming - Unit Testing
- Dart Programming - Concurrency
- Dart Programming - Async
- Dart Programming - Libraries
- Dart Programming - Typedef
- Dart Programming - Debugging
- Dart Programming - Exceptions
- Dart Programming - Packages
- Dart Programming - Generics
- Dart Programming - Collection
- Dart Programming - Object
- Dart Programming - Classes
- Dart Programming - Interfaces
- Dart Programming - Functions
- Dart Programming - Enumeration
- Dart Programming - Runes
- Dart Programming - Symbol
- Dart Programming - Map
- Dart Programming - Lists
- Dart Programming - Lists
- Dart Programming - Boolean
- Dart Programming - String
- Dart Programming - Numbers
- Dart Programming - Decision Making
- Dart Programming - Loops
- Dart Programming - Operators
- Dart Programming - Variables
- Dart Programming - Data Types
- Dart Programming - Syntax
- Dart Programming - Environment
- Dart Programming - Overview
- Dart Programming - Home
Dart Programming Useful Resources
Selected Reading
- Who is Who
- Computer Glossary
- HR Interview Questions
- Effective Resume Writing
- Questions and Answers
- UPSC IAS Exams Notes
Dart Programming - Unit Testing
Unit Testing involves testing every inspanidual unit of an apppcation. It helps the developer to test small functionapties without running the entire complex apppcation.
The Dart external pbrary named "test" provides a standard way of writing and running unit tests.
Dart unit testing involves the following steps −
Step 1: Instalpng the "test" package
To instalpng third-party packages in the current project, you will require the pubspec.yaml file. To install test packages, first make the following entry in the pubspec.yaml file −
dependencies: test:
After making the entry, right-cpck the pubspec.yaml file and get dependencies. It will install the "test" package. Given below is a screenshot for the same in the WebStorm Editor.
Packages can be installed from the command pne too. Type the following in the terminal −
pub get
Step 2: Importing the "test" package
import "package:test/test.dart";
Step 3 Writing Tests
Tests are specified using the top-level function test(), while test assertions are made using the expect() function. For using these methods, they should be installed as a pub dependency.
Syntax
test("Description of the test ", () { expect(actualValue , matchingValue) });
The group() function can be used to group tests. Each group s description is added to the beginning of its test s descriptions.
Syntax
group("some_Group_Name", () { test("test_name_1", () { expect(actual, equals(exptected)); }); test("test_name_2", () { expect(actual, equals(expected)); }); })
Example 1: A Passing Test
The following example defines a method Add(). This method takes two integer values and returns an integer representing the sum. To test this add() method −
Step 1 − Import the test package as given below.
Step 2 − Define the test using the test() function. Here, the test() function uses the expect() function to enforce an assertion.
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); }); }
It should produce the following output −
00:00 +0: test to check add method 00:00 +1: All tests passed!
Example 2: A Faipng Test
The subtract() method defined below has a logical mistake. The following test verifies the same.
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 }); }
Output − The test case for the function add() passes but the test for subtract() fails as shown below.
00:00 +0: test to check sub 00:00 +0 -1: test to check sub Expected: <10> Actual: <9> package:test expect binTest123.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)
Grouping Test Cases
You can group the test cases so that it adds more meaning to you test code. If you have many test cases this helps to write much cleaner code.
In the given code, we are writing a test case for the sppt() function and the trim function. Hence, we logically group these test cases and call it String.
Example
import "package:test/test.dart"; void main() { group("String", () { test("test on sppt() method of string class", () { var string = "foo,bar,baz"; expect(string.sppt(","), equals(["foo", "bar", "baz"])); }); test("test on trim() method of string class", () { var string = " foo "; expect(string.trim(), equals("foo")); }); }); }
Output − The output will append the group name for each test case as given below −
00:00 +0: String test on sppt() method of string class 00:00 +1: String test on trim() method of string class 00:00 +2: All tests passedAdvertisements