Test: Reverse String

Test: Reverse String

Specification for a function you don't need to write:

reverse_string(s)

Returns s with its characters in reverse order.

Your job: write a unittest.TestCase class, TestReverseString, with test methods test_reverses_word and test_empty_string that verify this specification. Call reverse_string directly.

As before, your tests are checked against both a correct implementation (all should pass) and a buggy one (at least one should fail).


Walkthrough

Thinking it through

import unittest


class TestReverseString(unittest.TestCase):
    def test_reverses_word(self):
        self.assertEqual(reverse_string("hello"), "olleh")

    def test_empty_string(self):
        self.assertEqual(reverse_string(""), "")

test_reverses_word is the test that actually exercises the interesting behavior: it only passes if the characters were genuinely reversed. A broken implementation that just returns its input unchanged (return s) would fail this immediately, since "hello" != "olleh".

test_empty_string covers a different kind of edge case: not a boundary value (like 0 was for is_positive), but an empty input — often a source of off-by-one errors or crashes in string/list-processing code. It's worth noting this particular test wouldn't catch the identity-function bug above (reversing "" correctly gives back "" either way) — which is exactly why you need test_reverses_word too. No single test case can catch every possible bug; a good test suite combines several that each catch different mistakes.

main.py
Console
Press Run to execute your code, or Submit to test and complete this problem.