Module: Toybox::Test

Overview

The Test module provides a testing framework for Monkey C.

The test module provides the tools to implement your own unit test and asserts in your source code. Unit tests take a Logger object and allow for different levels of output. Unit tests are annotated with :test and ignored if testing is not run. Asserts do not require the :test annotation and will be compiled out in release versions or you Connect IQ Content. A test RESULTS section is printed to the console with the tests run, test status, and failure rates.

Examples:

Testing Implementation

using Toybox.Test;
var x = 1;
var y = 2;

// Asserts can be called outside of test calls
function assertWithoutLogger(x, y) {
   Test.assertEqual(x, y);
}

(:test)
function aDebugTest(logger) {
   logger.debug("This is a debug message.");
   return true;
}

(:test)
function aTestOfAssert(logger) {
   logger.debug("This tests the assert() function.");
   Test.assert(true);
   logger.debug("Test.assert(true) didn't throw an Exception which is a very good thing.");
   Test.assert(false);
   logger.error("We should not be executing this statement.");
   return true;
}

Console Output for above example

// Output:
------------------------------------------------------------------------------
Executing test aDebugTest...
DEBUG (9:21): This is a debug message.
PASS
------------------------------------------------------------------------------
Executing test aTestOfAssert...
DEBUG (9:21): This tests the assert() function.
DEBUG (9:21): Test.assert(true) didn't throw an Exception which is a very good thing.
Unhandled Exception
ASSERTION FAILED
aTestOfAssert in C:\workspace\RunNoEvil\source\RunNoEvil.mc:21
runTest in UnitTests:33
ERROR

==============================================================================
RESULTS
Test:                                Status:
aDebugTest                           PASS
aTestOfAssert                        ERROR
Ran 2 tests

FAILED (passed=1, failed=0, errors=1)

See Also:

Since:

  • 2.1.0

Defined Under Namespace

Classes: AssertException, Logger

Instance Method Summary collapse

Instance Method Details

assert(test) ⇒ Object

Throws an exception if the test is false.

Examples:

using Toybox.Test;
(:test)
function anAssertTest(logger) {
   var x = false;
   Test.assert(x);
   return true;
}
// Output:
ASSERTION FAILED
Unhandled Exception

Parameters:

Raises:

Since:

  • 2.1.0


11561
11562
# File 'Monkeybrains.rb', line 11561

def assert(test)
end

assertEqual(value1, value2) ⇒ Object

Throws an exception if value1 and value2 are not equal.

The objects passed to this function must implement the Object.equals() method which compares both type and value.

Examples:

using Toybox.Test;
(:test)
function anAssertEqualTest(logger) {
   var x = 1;
   var y = 2;
   Test.assertEqual(x, y);
   return true;
}
// Output:
ASSERTION FAILED
Unhandled Exception

Parameters:

Raises:

Since:

  • 2.1.0


11608
11609
# File 'Monkeybrains.rb', line 11608

def assertEqual(value1, value2)
end

assertEqualMessage(value1, value2, message) ⇒ Object

Throws an exception if value1 and value2 are not equal followed by a String defined by the developer.

The objects passed to this function must implement the Object.equals() method which compares both type and value.

Examples:

using Toybox.Test;
(:test)
function anAssertEqualMessageTest(logger) {
   var x = 1;
   var y = 2;
   Test.assertEqual(x, y, "x and y are not equal!");
   return true;
}
// Output:
ASSERTION FAILED: x and y are not equal!
Unhandled Exception

Parameters:

Raises:

Since:

  • 2.1.0


11634
11635
# File 'Monkeybrains.rb', line 11634

def assertEqualMessage(value1, value2, message)
end

assertMessage(test, message) ⇒ Object

Throws an exception if the test is false followed by a String defined by the developer.

Examples:

using Toybox.Test;
(:test)
function anAssertMessageTest(logger) {
   var x = false;
   Test.assertMessage(x, "The assert is False for x.");
   return true;
}
// Output:
ASSERTION FAILED: The assert is False for x.
Unhandled Exception

Parameters:

Raises:

Since:

  • 2.1.0


11582
11583
# File 'Monkeybrains.rb', line 11582

def assertMessage(test, message)
end

assertNotEqual(value1, value2) ⇒ Object

Throws an exception if value1 and value2 are equal.

The objects passed to this function must implement the Object.equals() method which compares both type and value.

Examples:

using Toybox.Test;
(:test)
function anAssertNotEqualTest(logger) {
   var x = 1;
   var y = 1;
   Test.assertNotEqual(x, y);
   return true;
}
// Output:
ASSERTION FAILED
Unhandled Exception

Parameters:

Raises:

Since:

  • 2.1.0


11658
11659
# File 'Monkeybrains.rb', line 11658

def assertNotEqual(value1, value2)
end

assertNotEqualMessage(value1, value2, message) ⇒ Object

Throws an exception if value1 and value2 are equal followed by a String defined by the developer.

The objects passed to this function must implement the Object.equals() method which compares both type and value.

Examples:

using Toybox.Test;
(:test)
function anAssertNotEqualMessageTest(logger) {
   var x = 1;
   var y = 2;
   Test.assertNotEqual(x, y, "x and y are equal!");
   return true;
}
// Output:
ASSERTION FAILED: x and y are equal!
Unhandled Exception

Parameters:

Raises:

Since:

  • 2.1.0


11684
11685
# File 'Monkeybrains.rb', line 11684

def assertNotEqualMessage(value1, value2, message)
end