Lecture 15 - Python Testing and Debugging Flashcards
What is TDD? (3)
- instead of writing formal specifications of behavior, program behavior is specified as a series of test cases which must be passed
- the minimum necessary code is then written to make the program pass the test cases
- test cases are gradually added until the program has the desired functionality
What’s an interesting characteristic of TDD?
Will often roll back the code and write the update code again instead
of spending a long time debugging a single piece of code
What are the steps of the TDD cycle? (6)
What is a code coverage analyzer?
A tool to tell you what parts of a program have actually been active
What’s the idea of code coverage?
Infrequently-used statements in a program can hide bugs for a long time, so we want to figure out how to actually use everything
What is a test double?
A generic term for any case where you replace a production object for testing purposes.
Why do we use test doubles?
Sometimes it’s not possible to unit test certain code because of the unavailability of an external dependency (e.g. a database)
For a test double, when can the substitution be made? (2)
- –link time (compile alternate version)
- –run time (only for the duration of the actual test)
What are the types of test doubles?
How are test doubles done at runtime for
- Java
- Python
What is monkey patching?
“dynamic modifications of a class or module at runtime, motivated by the intent to patch existing third-party code as a workaround to a bug or feature which does not act as desired”
Applications of monkey patching (4)
–stub out a function for testing
–modify behavior of third-party code without forking that code
–apply a run-time patch
–distribute security fixes
What are dangerous things to be aware of regarding monkey patching? (3)
- changes in the code being patched can break the patch
- multiple modules trying to monkey patch the same thing can conflict
- source code on disk no longer reflects the actual behavior of the program, making debugging very difficult
Describe how to monkey patch in the case where you want to replace a function
- Describe how to monkey patch in the case where you want to wrap a function (2)
- To add code that executes before and/or after an existing function:
- Copy the original function pointer.
- a closure is a convenient place to store the original function pointer if you don’t need to restore it
- replace that pointer with a pointer to your monkey patch function
- Copy the original function pointer.