The Testing Package#
This package provides basic testing functionality.
Each of your tests should be in its own function, named test[FunctionName][testName]
.
These test functions should use the assertion functions testEqual
, testNotEqual
, testTrue
, and testFalse
in this package to assert things about the results of the test.
For example:
/** tests some simple examples of sums */
void testSumOfNumbersSimple() {
testEqual(10, sumOfNumbers(3,7));
testEqual(-5, sumOfNumbers(-10,5));
}
You then run these testing functions from your main functions, using the runAsTest
function from this package as a wrapper, as in
void test() {
runAsTest(this::testSumOfNumbersSimple);
...
}
To run your tests, you can either call the test function from main
, or you can run
java --enable-preview comp1110.testing.Test yourfilename.java
This will just run the test
function.
To import this package, use the following import statements:
import static comp1110.testing.Comp1110Unit.*;
Testing#
Functions#
runAsTest ; testEqual ; testNotEqual ; testTrue ; testFalse
runAsTest#
/**
* Runs a given function as a test. The test succeeds if all
* assertions within the function succeed, and no errors occur.
*
* @param testFun a void function with no arguments that runs a test
*/
void runAsTest(Runnable testFun);
testEqual#
/**
* Tests whether two given objects are equal
* (i.e. either both are null or both are not
* null and Equals(o1,o2)==true). Prints given
* message if test fails.
*
* Examples:
* given: "Hello", "World"
* expect: Test fails, default message displayed
* given: 5, 5
* expect: Test succeeds
*
* @param o1 An object to compare. By convention,
* this is the expected value.
* @param o2 An object to compare. By convention,
* this is the actual value.
* @param message A message to display if the test fails.
* You can use $0; and $1; to include the
* values of o1 and o2, respectively.
* OPTIONAL, default = "$0; and $1; are not equal!"
*/
void testEqual(Object o1, Object o2, String message);
testNotEqual#
/**
* Tests whether two given objects are not equal
* (i.e. either one is null and the other is not,
* or both are not null and Equals(o1,o2)==false).
* Prints given message if test fails.
*
* Examples:
* given: "Hello", "World"
* expect: Test succeeds
* given: 5, 5
* expect: Test fails, default message displayed
*
* @param o1 An object to compare.
* @param o2 An object to compare.
* @param message A message to display if the test fails.
* You can use $0; and $1; to include the
* values of o1 and o2, respectively.
* OPTIONAL, default = "$0; and $1; are equal!"
*/
void testNotEqual(Object o1, Object o2, String message);
testTrue#
/**
* Tests whether a given boolean is true.
* Prints given message if boolean is false.
*
* Examples:
* given: true
* expect: Test succeeds
* given: false
* expect: Test fails, displays error message
* given: 5 == 2
* expect: Test fails, displays error message
*
* @param b The boolean value that should be true.
* @param message A message to display if the test fails.
* OPTIONAL, default = "Expected true, but got false!"
*/
void testTrue(boolean b, String message);
testFalse#
/**
* Tests whether a given boolean is false.
* Prints given message if boolean is true.
*
* Examples:
* given: false
* expect: Test succeeds
* given: true
* expect: Test fails, displays error message
* given: 5 == 2
* expect: Test succeeds
*
* @param b The boolean value that should be false.
* @param message A message to display if the test fails.
* OPTIONAL, default = "Expected false, but got true!"
*/
void testFalse(boolean b, String message);