Skip to content
Steve-Fenton edited this page Oct 20, 2014 · 2 revisions

Quick Start

Create a test class

You create a test class by implementing the tsUnit.ITestClass interface. Your test class can also be placed inside of a module.

module CalculationsTests {
    export class SimpleMathTests extends tsUnit.TestClass {

        private target = new Calculations.SimpleMath();

        addTwoNumbersWith1And2Expect3() {
            var result = this.target.addTwoNumbers(1, 2);

            this.areIdentical(3, result);
        }

        addTwoNumbersWith3And2Expect5() {
            var result = this.target.addTwoNumbers(3, 2);

            this.areIdentical(4, result); // Deliberate error
        }
    }
}

Any functions in your test class are automatically run for you and the assertions used to pass or fail your tests. You can refer to class-level variables in your tests as the instance is preserved during testing. Run the tests

// Instantiate tsUnit and pass in modules that contain tests
var test = new tsUnit.Test(CalculationsTests);

// Use the built in results display
test.showResults(document.getElementById('results'), test.run());

You can run the tests in the browser, or via a script engine. You can include these tests in your normal build and deploy routine either way by using a Coded UI test (or Selenium, or Waitn) or by using a script engine in your unit testing project.

Customise your output (only if you want to!)

If you want to customise the output, you can use the test result class returned from the run function:

var testResult = test.run();

if (testResult.errors.lenth > 0) {
    alert('Oh dear!');
}

Grouping test results

You can optionally specify a name against your test class to group the test results:

test.addTestClass(new CalculationsTests.SimpleMathTests(), 'My Grouping');

This will then nest the results under the grouping. If you don't specify any groups, everything will be grouped under a single "Tests" group.