

List reversal using slices will give you a new list, but if you use a different method for reversal it could modify in place. This is a very important one! If you call a function and you're not sure whether it will modify the list in place or give you a new one, you're bound to encounter problems. Does it modify the list in place or does it give you a new list? If you want to allow other sequences, you could use Sequence instead of List from the typing module. Maybe this is something you'd like to allow-but at the moment it is not allowed.īecause our type hinting specifies List as the argument, lists should be passed or a warning will be given to the developer if they are using type hinting tools (which they should be!). Reversing with slices will work on strings and other sequences as well as lists. This question occurred to me while writing this post! def test_reverses_empty_list(self):ĭoes it work with strings and other sequences, or only lists? Then we would come back and fix it, happy in the knowledge that our function works in the way we expect. This test is here because if in the future we decide to raise an error if an empty list is passed, this test should fail. If you'd rather notify users when they're trying to reverse an empty list, you can raise an error if the length of the original list is 0. This question is another that you may want to decide whether to support or not. Note that you don't need an explicit test for "it doesn't raise an error when elements are all the same type" because that is inferred since our first test passes. Raise ValueError("The list to be reversed should have homogeneous types.")ĭef test_reverses_varying_elements_raises(self): Return any(not isinstance(t, first) for t in list_) For the purposes of this example, let's make it so we can't have mixed types. Should our function check that elements are all the same type? It's up to you to decide what should happen in your codebase. If you're never going to do this, maybe what you want is to not do it by accident.

You may think: "I'm never going to do this, so why should I care?". The answer to this question may seem irrelevant. Let's answer our other questions with some more tests:ĭoes it reverse a list with different types of elements? you see there is your test succeeding! Answering questions with tests If using a terminal, type python -m unittest test_reverse.py.If using P圜harm, right-click and press "Run tests with unittest.".Once you've saved your file, you can run it by either: Notice that my test method is called test_reverses_nonempty_list. The naming of the test methods should also follow this naming pattern by default. For now, call your file test_reverse.py for example. Running tests with unittestīy default when working with unittest, your file should be called test_*.py. Usually in my tests I define the argument(s) to the thing I'm testing (the original variable), the expected result, and then I compare the two. It's important to write simple tests where possible. Self.assertEqual(reverse_list(original), expected) from unittest import TestCaseĭef reverse_list(original: List) -> List: Usually my first test will be a positive test, which means "does this function do what I expect it to?". Let's start off by writing our first test. We'll be writing tests for all questions we have! Writing Python tests with unittest I'll note now that it's extremely common to think of more questions as you write tests. You need to know if that happens, for a change to some of these could break your entire code! If someone comes around later on and modifies the function, the answers to some of these questions may change.
#IUNIT TESTING PYTHON CODE#
But remember, the purpose of tests is to describe, and to make code resilient to change. You may know the answer to all these questions already (or you can easily find out just by launching a REPL and trying it out).

#IUNIT TESTING PYTHON SOFTWARE#
Testing is an essential part of software development.
