UnitTest:
Filter:
Classes | Testing

UnitTest : Object

a class for programmatic testing of classes
Source: UnitTest.sc

Description

In order to make sure a method works correctly, a test can be implemented that assures the correct behavior.

It is a common practice to write tests to clarify how an object should respond, and it may avoid inconsistencies on the long run. A test is always a subclass of UnitTest, implementing at least one method starting with test_.

NOTE: UnitTests for the Common library classes are kept in the testsuite/classlibrary directory of the SuperCollider sources.

To install, download the sources and add the directory to your sclang_conf.yaml, either by editing it or via the ScIDE preferences.

Class Methods

UnitTest.gui

A graphical interface to run and browse all tests

UnitTest.reportPasses

UnitTest.reportPasses = value

Accessor controlling whether passing tests should be reported. Defaults to true. See also *passVerbosity which controls how much detail is reported from passing tests when this is set to true.

Arguments:

(value)

Should be true or false.

UnitTest.passVerbosity

UnitTest.passVerbosity = value

Accessor controlling whether extra details should be reported for passing tests.

Defaults to UnitTest.full so that all details are reported; however this behaviour may be too verbose for some, for whom it is sufficient to see that a test is passing without needing to see the detail. If set to UnitTest.brief, and *reportPassesis true, passes will be reported, but without any extra details which may be provided by assertions.

Arguments:

(value)

Should be UnitTest.full or UnitTest.brief.

UnitTest.run(reset: true, report: true)

Run all methods whose names begin with test_.

Arguments:

reset

If true, first runs *reset on the class.

report

If true, outputs the test results.

UnitTest.runTest(methodName)

Run a single test method.

Arguments:

methodName

A String referring to the class and test method within it, e.g. "TestPolyPlayerPool:test_prepareChildrenToBundle".

UnitTest.runAll

runs all subclasses of UnitTest

Inherited class methods

Undocumented class methods

UnitTest.brief

UnitTest.full

UnitTest.runTestClassForClass(class, reset: true, report: true)

Instance Methods

.runTestMethod(method)

Run a single test method of this class

Arguments:

method

the method to run

Returns:

(describe returnvalue here)

.assert(boolean, message, report: true, onFailure, details)

Asserts that an expression must be true.

Arguments:

boolean

A boolean, where true means that the test is passed.

message

A message describing the purpose of the assertion, e.g. "foo should be less than bar". Posted if report is true.

report

Reports the result of the test if true.

onFailure

If not nil, a failure stops the tests and evaluates this function.

details

Some optional extra details which will be passed to the reporting framework for display unless brief reporting is requested (see *passVerbosity).

.assertEquals(a, b, message: "", report: true, onFailure)

Asserts that an expression a is equal to the value b, where equality is determined according to a's implementation of ==.

Automatically passes details of the equality check to the reporting framework, which will be displayed if the assertion fails, and also if it passes and *reportPasses is true and *passVerbosity is not set to UnitTest.brief.

Arguments:

a

the experimentally produced value

b

the expected value

message

A message describing the purpose of the assertion, e.g. "Adding two numbers should return their sum.". Posted if report is true.

report

Reports the result of the test if true (default is true)

onFailure

If not nil, a failure stops the tests and evaluates this function.

.assertFloatEquals(a, b, message: "", within: 0.0001, report: true, onFailure)

Asserts that an expression a returning a float is within a given range (within) of being equal to b.

Automatically passes details of the equality check to the reporting framework, which will be displayed if the assertion fails, and also if it passes and *reportPasses is true and *passVerbosity is not set to UnitTest.brief.

Arguments:

a

the experimentally produced float value

b

the expected float value

message

A message describing the purpose of the assertion, e.g. "Adding two numbers should return their sum.". Posted if report is true.

within

The range within which a must be of b in order for the test to pass.

report

Reports the result of the test if true (default is true)

onFailure

If not nil, a failure stops the tests and evaluates this function.

.assertArrayFloatEquals(a, b, message: "", within: 0.0001, report: true, onFailure)

Make sure that the two arrays of floats a and b equal within a given range (within).

Arguments:

a

the experimentally produced value, which is an Array of floats

b

the Array of float values expected

message

A message describing the purpose of the assertion, e.g. "Arrays foo and bar should be equal.". Posted if report is true.

within

The range within which each item of a must be of the corresponding item in b in order for the test to pass.

report

Reports the result of the test if true (default is true)

onFailure

If not nil, a failure stops the tests and evaluates this function.

.ifAsserts(boolean, message, ifPassedFunc, ifFailedFunc, report: true)

Make a further assertion only if it passed, or only if it failed.

.wait(condition, failureMessage, maxTime: 10.0)

Wait for a condition, consider failed after maxTime. Only valid within a test (or a routine).

.bootServer(server)

Wait for server boot until continued. Only valid within a test (or a routine).

If already booted, then freeAll and create new allocators.

.passed(method, message, report: true, details)

Register a passed test.

Arguments:

method

Name of the method in which the test is passing.

message

A message describing the purpose of the assertion, e.g. "foo should be less than bar". Posted if neither report nor *reportPasses are false.

report

Reports the result of the test if true and *reportPasses is true.

details

Some optional extra details which will be displayed if *reportPasses is true and *passVerbosity is not set to UnitTest.brief.

Discussion:

.failed(method, message, report: true, details)

Register a test failure.

Arguments:

method

Name of the method in which the test is failing.

message

A message describing the purpose of the assertion, e.g. "foo should be less than bar". Posted if report is true.

report

Reports the result of the test if true.

details

Some optional extra details which will be displayed if reportis true.

Discussion:

Inherited instance methods

Undocumented instance methods

.assertException(func, errorClass, message, report: true, onFailure, details)

.assertNoException(func, message, report: true, onFailure, details)

.asynchAssert(waitConditionBlock, testBlock, timeoutMessage: "", timeout: 10)

Examples

Write tests by subclassing UnitTest, and defining methods whose names begins test_. Each test method will be called from a fresh instance of the subclass.

If you implement the methods setUp and tearDown in your test class, they will be called before and after every test. This can help to eliminate repetitive code, and makes it easier to maintain your tests by ensuring that they all run under the same set of conditions.