# Assertions and Unit Testing

## Python&#x20;

Full documentation - <https://docs.python.org/3/library/unittest.html>

| **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.

```python
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>

## Java

Full documentation - <https://junit.org/junit4/javadoc/4.12/org/junit/Assert.html>

| **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.

```python
assertEquals("Looks like your code is not outputing the proper value.", realOutput, studentOutput)
```

## Ruby

Ruby calls assertions "matchers"

Full documentation - <https://rspec.info/documentation/3.9/rspec-expectations/RSpec/Matchers.html>

<table><thead><tr><th width="230.91502237068875">Matcher</th><th>Description</th></tr></thead><tbody><tr><td><strong>Matcher</strong></td><td><strong>Description</strong></td></tr><tr><td><p><code>expect([]).to be_empty</code></p><p><code>expect([]).not_to be_empty</code></p></td><td><p># => [].empty?() | passes</p><p># => [].empty?() | fails</p></td></tr><tr><td><p><code>expect("a string").to be_an_instance_of(String)</code> </p><p><code>expect(3).to be_a_kind_of(Integer)</code> </p><p><code>expect(3).to be_a_kind_of(Numeric)</code> </p><p><code>expect(3).to be_an_instance_of(Integer)</code> <code>expect(3).not_to be_an_instance_of(Numeric)</code></p></td><td><p># => 3.kind_of?(Numeric) | passes </p><p># => 3.kind_of?(Numeric) | passes </p><p># => 3.instance_of?(Integer) | passes </p><p># => 3.instance_of?(Numeric) | fails</p></td></tr><tr><td><p><code>expect(5).to eq(5)</code></p><p><code>expect(5).not_to eq(3)</code></p></td><td></td></tr><tr><td><code>expect { print 'foo' }.to output('foo').to_stdout</code></td><td>Check function output to stdout</td></tr><tr><td><p><code>expect(5).to satisfy { |n| n > 3 }</code></p><p><code>expect(5).to satisfy("be greater than 3") { |n| n > 3 }</code></p></td><td></td></tr></tbody></table>


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://auto-grade.joemazzone.net/assertions-and-unit-testing.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
