Assertions and Unit Testing
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.
assertEqual(student_output, real_output, "Looks like your code is not outputing the proper value.")
Useful Links:
- Skipping test and expected failures: https://docs.python.org/3/library/unittest.html#skipping-tests-and-expected-failures
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.
assertEquals("Looks like your code is not outputing the proper value.", realOutput, studentOutput)
Ruby calls assertions "matchers"
Matcher | Description |
---|---|
Matcher | Description |
expect([]).to be_empty expect([]).not_to be_empty | # => [].empty?() | passes # => [].empty?() | fails |
expect("a string").to be_an_instance_of(String) expect(3).to be_a_kind_of(Integer) expect(3).to be_a_kind_of(Numeric) expect(3).to be_an_instance_of(Integer) expect(3).not_to be_an_instance_of(Numeric) | # => 3.kind_of?(Numeric) | passes # => 3.kind_of?(Numeric) | passes # => 3.instance_of?(Integer) | passes # => 3.instance_of?(Numeric) | fails |
expect(5).to eq(5) expect(5).not_to eq(3) | |
expect { print 'foo' }.to output('foo').to_stdout | Check function output to stdout |
expect(5).to satisfy { |n| n > 3 } expect(5).to satisfy("be greater than 3") { |n| n > 3 } | |
Last modified 1yr ago