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:
Want to create a copy of a #Python list? Use Python's `copy()` method!
Note: Watch out if your list contains lists of dictionaries. In those cases, you might be better off using copy.deepcopy()
But be careful! If your list contains a mutable object, like another list or a dictionary, you may encounter some unexpected behavior.
In the following example, you `copy()` the list. Then you modify the nested dictionary in the copy, but that also changes the original list!
You can fix this behavior by using Python's `copy` module. It provides a deepcopy() function that you can use which will make a deep copy of the ENTIRE list!