Unit Testing with EUnit

While in theory the Elements languages can be used with any existing unit testing framework (such as NUnit or XUnit on .NET or JUnit on Java), Elements comes with its own unit testing framework that is modeled on the same principles, but makes it easy to share tests across platforms: EUnit.

EUnit provides a common API for writing and running tests on all platforms supported by Elements. That means you can use it as your one stop library to write all your unit tests – those that are platform specific, and those that can be shared, to test the same shared code on all platforms.

Getting Started with EUnit

EUnit ships with a "Test Application" template for each platform. This is essentially an empty shell project that sets up the test runner harness, and is ready to run any tests you then add.

If you are building tests for a single platform, you can simply create a single test runner project for the platform you need, and then add additional test classes to it via the platform-agnostic "Test Class" template.

If you want to build tests for multiple platforms, you will create a test runner project for each platform, alongside a Shared Project that you reference from each of the test runners. You can then add shared test classes to the shared project, and platform-specific test classes to the individual runner projects.

Sample Code

A test runner project would look something like this (generated from the template):

uses RemObjects.Elements.EUnit

begin
  var Tests := Discovery.DiscoverTests();
  var Results := Runner.RunTests(Tests) withListener(new ConsoleTestListener());
end.
using RemObjects.Elements.EUnit;

public void main()
{
    var Tests = Discovery.DiscoverTests();
    var Results = Runner.RunTests(Tests) withListener(new ConsoleTestListener());
}
import RemObjects.Elements.EUnit

let Tests = Discovery.DiscoverTests()
let Results = Runner.RunTests(Tests, withListener: ConsoleTestListener());

Tests are defined by simply writing a new class that descends from RemObjects.Elements.EUnit and adding parameterless and resultless public methods to it. Each method will be run as a test:

MyTests = public class (Test)
public
  method AlwaysPass;
  method AlwaysFails;
end;

Implementation:

method MyTests.AlwaysPass;
begin
  Assert.AreEqual(1, 1);
end;

method MyTests.AlwaysFails;
begin
  Assert.AreEqual("a", "b");
end;
public class MyTests: Test
{
    public void AlwaysPass()
    {
        Assert.AreEqual(1, 1);
    }

    public void AlwaysFail()
    {
        Assert.AreEqual("a", "b");
    }
}
public class MyTests: Test {
    public func AlwaysPass() {
        Assert.AreEqual(1, 1)
    }

    public func AlwaysFail() {
        Assert.AreEqual("a", "b")
    }
}

You can use the "Test Class" template or simply write a class from scratch.

EUnit API Reference

You can find more complete coverage of all EUnit APIs in the EUnit API Reference.