Assertions and Unit Testing
Python
Full documentation - https://docs.python.org/3/library/unittest.html
Method | Description |
| Test that first and second are equal. If the values do not compare equal, the test will fail. |
| Test that first and second are not equal. If the values do compare equal, the test will fail. |
| Test that first and second are approximately equal within a positive delta. |
| Tests that two lists are equal. |
| Tests that two tuples are equal. |
| Test that two dictionaries are equal. |
| Test that first and second have the same elements in the same number, regardless of their order. |
| Test first > second |
| Test first >= second |
| Test first < second |
| Test first <= second |
| Test that first and second are the same object. |
| Test that member is in container. |
| Test that condition is true. |
| 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.
Useful Links:
Skipping test and expected failures: https://docs.python.org/3/library/unittest.html#skipping-tests-and-expected-failures
Java
Full documentation - https://junit.org/junit4/javadoc/4.12/org/junit/Assert.html
Method | Description |
| Test that two values are equal. |
| Test that two doubles are equal to within a positive delta. |
| Test that two arrays are equal. |
| Test that an object isn't null. |
| Test that an object is null. |
| Test that two objects do not refer to the same object. |
| Test that two objects refer to the same object. |
| Test that a condition is true. |
| 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.
Ruby
Ruby calls assertions "matchers"
Full documentation - https://rspec.info/documentation/3.9/rspec-expectations/RSpec/Matchers.html
Matcher | Description |
---|---|
Matcher | Description |
| # => [].empty?() | passes # => [].empty?() | fails |
| # => 3.kind_of?(Numeric) | passes # => 3.kind_of?(Numeric) | passes # => 3.instance_of?(Integer) | passes # => 3.instance_of?(Numeric) | fails |
| |
| Check function output to stdout |
|
Last updated