Wednesday, 14 October 2015

Unit Testing - Simplegamelib Part Six

In this post, we take a step back and look at how the Simplegamelib is tested. Sorry no ninjas this time!

The awesome test package allows unit tests to be created and run. For example:

    test('Check empty', () {
      SpriteGroup sg = createSpriteGroup(4);
      expect(sg.empty, equals(false));
      sg.reset();
      expect(sg.empty, equals(true));
    });

The unit tests (20 or so) can be found on GitHub. In contrast to other command line apps, it can run code that imports dart:html which saves splitting of packages to remove browser dependencies. This is hugely useful and a real plus point for Dart Unit Testing. It has certainly helped in the development of this little package!! :-)

These can be run using the command:

pub run test .\test\simplegamelib_test.dart -p dartium

Providing the system has a binary called dartiumon the path. The Dart content shell is another possibility but as dartium worked first time I have tried anything else locally.

This is all great for running unit tests when I want to but how about getting them running every time a change goes into the GitHub repository? Turns out the continuous integration service Travis CI supports Dart. The claim to be set up in minutes turns out to be true! All you need to add is a small YML file to the GitHub repository and it will monitor the repo for changes and even email you the results. The current implementation is focussed on the old unittest package but I hear it is to be move to test very soon!

language: dart
dart:
  - stable
  - dev
sudo: false
with_content_shell: true
before_install:
  - export DISPLAY=:99.0
  - sh -e /etc/init.d/xvfb start
script: pub run test -p content-shell -p firefox

This time we use the content shell and firefox. As a bonus, we can run the tests on Dart STABLE and DEV.

You can see the test reports for the Simplegamelib package here.

Next time, we will get back to a traditional demo and look at adding some speed to our sprites.