Tutorial by Examples

Programs throw errors when for instance wrong input is given. Because of this, one needs to make sure that an error is thrown when actual wrong input is given. Because of that we need to check for an exact exception, for this example we will use the following exception: class WrongInputException(Ex...
One way to mock a function is to use the create_autospec function, which will mock out an object according to its specs. With functions, we can use this to ensure that they are called appropriately. With a function multiply in custom_math.py: def multiply(a, b): return a * b And a function...
Sometimes we want to prepare a context for each test to be run under. The setUp method is run prior to each test in the class. tearDown is run at the end of every test. These methods are optional. Remember that TestCases are often used in cooperative multiple inheritance so you should be careful to...
You can test that a function throws an exception with the built-in unittest through two different methods. Using a context manager def division_function(dividend, divisor): return dividend / divisor class MyTestCase(unittest.TestCase): def test_using_context_manager(self): ...
While Python has an assert statement, the Python unit testing framework has better assertions specialized for tests: they are more informative on failures, and do not depend on the execution's debug mode. Perhaps the simplest assertion is assertTrue, which can be used like this: import unittest ...
installing pytest: pip install pytest getting the tests ready: mkdir tests touch tests/test_docker.py Functions to test in docker_something/helpers.py: from subprocess import Popen, PIPE # this Popen is monkeypatched with the fixture `all_popens` def copy_file_to_docker(src, dest): ...

Page 1 of 1