The test()
function in Dart allows developers to define test cases, make assertions, and validate the expected behavior of their code. It is a powerful tool for ensuring code correctness.
This function plays a crucial role in software development by verifying the smallest testable software units, such as functions, methods, or classes. In the image below, different type of testing is displayed.
We can follow the instructions given below to implement the test()
function.
Install the test package by defining it into pubspec.yaml file.
Import the package into the dart file.
Define the test cases according to the syntax guidelines.
To begin using the test() function in Dart, we need to install the necessary package that provides the testing framework. For this purpose, we will open the pubspec.yaml in our dart project and will add the following line under the "dependencies" section.
dependencies
test:
After making the changes, we can install the package in two ways.
Right-click the pubspec.yaml
file and click on "get packages."
Write the following command in the terminal.
dart pub get
We can import a package into the Dart file by adding the following line of code in the Dart file where we want to implement the test()
function:
import "package:test/test.dart";
We can specify tests in Dart using test()
, while the expect()
functions.
test("Here we can write description of the test", () {
// Here you can specify any variable or logic
expect(actualValue , expectedOutcome)
});
We can also group multiple tests using a group()
function and specify a group description for it.
group("We can specify group name here", () {
test("Here we can write description of the test 1", () {
expect(actual, exptected);
});
test("Here we can write description of the test 2", () {
expect(actual, expected);
});
})
Here we will discuss two examples by putting together the code explained above, one where test cases will be passed and the other where some will fail.
In the code below, we have two files: index.dart
, where one test case is defined, and the other is pubspec.yaml
file where the test
package is declared so we can import it into the dart file.
// Import the test package import "package:test/test.dart"; // Function to be tested int Add(int x,int y) { var z = x+y; return z; } void main() { print(" "); // Define the test test("test to check add method",(){ // Arrange var expected = 30; // Act var actual = Add(10,20); // Asset expect(actual,expected); }); }
Lines 5–8: This code defines a Add()
function, which is being tested, takes two integers as input parameters, adds them together, and returns the sum.
Lines 13–22: A test case is defined using the test()
function. The test case has a description ("test to check add method")
. Inside the test case, a variable expected
is initialized with the expected sum value. The Add()
function is invoked with input values (10 and 20), and the result is stored in the actual
variable. The expect()
function is used to compare the actual
and expected
values. If they match, the test case passes; otherwise, it fails.
As the index.dart
contains only one test case, and it passed, so the output shows +1
in the final output.
00:00 +0: test to check add method
00:00 +1: All tests passed!
In the code below, we again have the same two files index.dart
, where two test cases are defined but they are grouped, and the other is pubspec.yaml
file where the test
package is declared so we can import it into the dart file.
// Import the test package import 'package:test/test.dart'; // Function to be tested int Add(int x,int y){ return x+y; } int Sub(int x,int y){ return x-y-1; } void main(){ group("This is a group",(){ 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 }); }); }
Lines 5–10: Two functions are defined for the test: Add()
and Sub()
. Add()
takes two parameters x
and y
, and returns their sum while Sub()
subtracts y
from x
and then subtracts 1
from the result.
Line 14: A test block is defined using the group()
function. It groups related tests and provide a description.
Lines 15–34: We have two individual test cases defined using the test()
function in the test group. The first test, 'test to check sub
,' checks the result of the Sub()
function and expects the value 10
as the outcome. The second test, 'test to check add method
,' checks the result of the Add()
function and expects the value 30
as the outcome.
This time index.dart
file contains two test cases, but as a group, one fails, and the other passes, so the count becomes +1 -1
.
00:00 +0: This is a group test to check sub
00:00 +0 -1: This is a group test to check sub [E]
Expected: <10>
Actual: <9>
package:matcher/src/expect/expect.dart 149 fail
package:matcher/src/expect/expect.dart 144 _expect
package:matcher/src/expect/expect.dart 56 expect
index.dart 22 main.<fn>.<fn>
package:test_api/src/backend/declarer.dart 215 Declarer.test.<fn>.<fn>
===== asynchronous gap ===========================
package:test_api/src/backend/declarer.dart 213 Declarer.test.<fn>
===== asynchronous gap ===========================
package:test_api/src/backend/invoker.dart 258 Invoker._waitForOutstandingCallbacks.<fn>
00:00 +0 -1: This is a group test to check add method
00:00 +1 -1: Some tests failed.
The Dart test() function is essential for ensuring code quality. Developers can effectively implement tests verifying their software units' correctness by installing the test package, importing it into their Dart files, and following the syntax guidelines. This practice helps catch potential bugs and ensures the code behaves as expected. Adopting a test-driven development approach with test() in Dart can lead to more reliable applications.