Introduction
Unit testing as an aspect of software development is essential tool to keep the code valid. This means that when we develop software or write code, we need an automated way (programmatic way) to validate the written code. QA can help with validating the results but cannot take place of automated scripts that run the unit tests. Also, we need to have something written by programmers that validate what programmers have written so far, looking from programmers' perspective.
Unit testing with Dart
Unit testing with dart is pretty straight forward. Here is what you need to do to write a simple unit test using dart language.1. Import unit test package 'package:unittest/unittest.dart'.
2. Setup main() function to launch the test
3. Arrange code to prepare for test
4. Write code for test.
5. Assert the results.
6. Establish the testing infrastructure (or simple create a web page that would call the test code).
7. Run the tests in the browsers.
Here is an example:
import 'dart:html';
import 'dart:convert' show JSON;
import 'dart:async' show Future;
import 'dart:math' show Random;
import '../lib/attempt_helper.dart';
import '../model/attempt.dart';
import 'package:unittest/unittest.dart';
import 'package:unittest/html_enhanced_config.dart';
List offlineSymbols;
main() {
useHtmlEnhancedConfiguration();
//useHtmlEnhancedConfiguration(true);
var path = 'symbols.json';
//HttpRequest.getString(path)
// .then(_getOfflineListFromJSON)
// .then(_launchOfflineTests);
HttpRequest.getString(path)
.then(_parseSymbols)
//.catchError(_getFailsafeFeaturedSymbol)
.then(_launchAttemptHelperTests);
//_launchAttemptHelperTests();
}
List _parseSymbols(String jsonString) {
offlineSymbols = JSON.decode(jsonString);
return offlineSymbols;
}
void _launchAttemptHelperTests(List symbols) {
AttemptHelper attemptHelper = new AttemptHelper();
List<Attempt> attempts = new List<Attempt>();
Attempt newAttempt = new Attempt()
..answers.add("1")
..answers.add("2")
..answers.add("3")
..answers.add("4")
..correctAnswerIndex = 1
..givenAnswerIndex = 1;
attempts.add(newAttempt);
Random random = new Random();
group('Attempt Helper Tests ->', () {
test("Test to check if list has any item", () =>
expect(attempts.length > 0,isTrue)
);
test("Test to check if returned attempt from helper is not null", () =>
expect(attemptHelper.getAttempt(attempts, symbols) != null, isTrue)
);
test("Test to check if returned attempt's answers from helper is not null", () =>
expect(attemptHelper.getAttempt(attempts, symbols).answers != null, isTrue)
);
test("Test to check if returned attempt's answers are greater than 0", () =>
expect(attemptHelper.getAttempt(attempts, symbols).answers.length > 0, isTrue)
);
test("Test to getScore", () =>
expect(attemptHelper.getScore(attempts) > 0, isTrue)
);
test("Test to check if returned randon answer is not null", () =>
expect(attemptHelper.getRandomAnswer(symbols, random, newAttempt.answers) != null, isTrue)
);
test("Test to Post a wrong given answer", () =>
expect(attemptHelper.postGivenAnswer(attempts, "10") == false, isTrue)
);
test("Test to Post a correct given answer", () =>
expect(attemptHelper.postGivenAnswer(attempts, "1") == true, isTrue)
);
test("Load test by running 10records", () =>
expect(get100Attempts(attemptHelper, 10) != null, isTrue)
);
});
}
List<Attempt> get100Attempts(AttemptHelper attemptHelper,int numberOfInterations) {
List<Attempt> hundredAttempts = new List<Attempt>();
for(int i = 0; i < numberOfInterations ; i++) {
attemptHelper.getAttempt(hundredAttempts, offlineSymbols);
}
return hundredAttempts;
}
As you can see from the above example it is pretty simple to write the unit tests in dart programming language. Also you can see how similar it is to unit tests that we write using C# in .NET.
Running the unit tests
As mentioned in point 7 and 8 in the previous section, we have to establish the unit test infrastructure which essential the host for the tests. In our example it is a simple web page. You can use command line app to host your unit tests.
Here is the code of the web page that establishes the infrastructure and runs the unit tests:
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>tests</title>
</head>
<body>
<p id="text"></p>
<script type="application/dart" src="tests.dart"></script>
<!-- for this next line to work, your pubspec.yaml file must have a dependency on 'browser' -->
<script src="packages/browser/dart.js"></script>
</body>
</html>
That is it!
Here is how the results look like: