Assertions and Unit Testing

Python

Method
Description
assertEqual(first, second)
Test that first and second are equal. If the values do not compare equal, the test will fail.
assertNotEqual(first, second)
Test that first and second are not equal. If the values do compare equal, the test will fail.
assertAlmostEqual(first, second, delta=none)
Test that first and second are approximately equal within a positive delta.
assertListEqual(first, second)
Tests that two lists are equal.
assertTupleEqual(first, second)
Tests that two tuples are equal.
assertDictEqual(first, second)
Test that two dictionaries are equal.
assertCountEqual(first, second)
Test that first and second have the same elements in the same number, regardless of their order.
assertGreater(first, second)
Test first > second
assertGreaterEqual(first, second)
Test first >= second
assertLess(first, second)
Test first < second
assertLessEqual(first, second)
Test first <= second
assertIs(first, second)
Test that first and second are the same object.
assertIn(member, container)
Test that member is in container.
assertTrue(condition)
Test that condition is true.
assertFalse(condition)
Test that condition is false.
Why use assertListEqual() instead of just assertEqual()? Well, they are designed to show the difference between the two values on failure. So students will receive more accurate feedback if you use the proper assertion.
Feedback:
You can add feedback to assertion failures to alert students what they might have done wrong. To do so, just simply add an additional argument as a string containing the message you want to display.
1
assertEqual(student_output, real_output, "Looks like your code is not outputing the proper value.")
Copied!
Useful Links:

Java

Method
Description
assertEquals(expected, actual)
Test that two values are equal.
assertEquals(expected, actual, delta) - for doubles
Test that two doubles are equal to within a positive delta.
assertArrayEquals(expecteds, actuals)
Test that two arrays are equal.
assertNotNull(object)
Test that an object isn't null.
assertNull(object)
Test that an object is null.
assertNotSame(unexpected, actual)
Test that two objects do not refer to the same object.
assertSame(unexpected, actual)
Test that two objects refer to the same object.
assertTrue(condition)
Test that a condition is true.
assertFalse(condition)
Test that a condition is false.
Feedback:
You can add feedback to assertion failures to alert students what they might have done wrong. To do so, just simply add a string containing the message you want to display as the first argument in the method call.
1
assertEquals("Looks like your code is not outputing the proper value.", realOutput, studentOutput)
Copied!
​
Last modified 2mo ago
Export as PDF
Copy link
Contents
Python
Java