To get started with unit testing in Python, you need some code to unit test
Here's some code you can use that you can save as "mymath.py"
To add a unit test in Python, you normally create a new file with the same name as the file you are testing, but prepended with the word "test" -> "test_mymath.py"
Then you subclass `unittest.TestCase` and create one or more test methods
Here's an example:
To keep things simple, save the test file in the same directory as the file that you are testing.
Then open up a terminal and navigate to the folder that has your code.
Finally, you run the following command:
What does this output mean? You will see dots or periods for each test that passes and "F"s for tests that fail.
In this example, you have 3 tests and 3 periods, so they all passed!
If you'd like more information about what tests are running, you can pass the `-v` argument for verbose mode:
You can get a full listing of the arguments you can pass to your test by using `-h` for help:
You can even specify which tests you want it to run specifically rather than running all of them:
You can do a lot with `unittest`. Check out the full documentation for details:
One big gotcha when it comes to #Python decorators is that the decorator will covertly replace the function's name and docstring with its own (π§΅ππ)
Check out the following example
When you run the decorated code in the previous tweet, you will see that the decorated function's name and docstring have disappeared!
You may be wondering, why do I care if a function's name and docstring are replaced?
The reason this is important is that your introspection won't work correctly anymore. If you run Python's `help()` built-in against your decorated function, it will give you the wrong info