Lecture 15 - Python Testing and Debugging Flashcards

1
Q

What is TDD? (3)

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

What’s an interesting characteristic of TDD?

A

Will often roll back the code and write the update code again instead
of spending a long time debugging a single piece of code

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

What are the steps of the TDD cycle? (6)

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

What is a code coverage analyzer?

A

A tool to tell you what parts of a program have actually been active

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

What’s the idea of code coverage?

A

Infrequently-used statements in a program can hide bugs for a long time, so we want to figure out how to actually use everything

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

What is a test double?

A

A generic term for any case where you replace a production object for testing purposes.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Why do we use test doubles?

A

Sometimes it’s not possible to unit test certain code because of the unavailability of an external dependency (e.g. a database)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

For a test double, when can the substitution be made? (2)

A
  • –link time (compile alternate version)
  • –run time (only for the duration of the actual test)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What are the types of test doubles?

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

How are test doubles done at runtime for

  • Java
  • Python
A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

What is monkey patching?

A
“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”
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
12
Q

Applications of monkey patching (4)

A

–stub out a function for testing
–modify behavior of third-party code without forking that code
–apply a run-time patch
–distribute security fixes

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
13
Q

What are dangerous things to be aware of regarding monkey patching? (3)

A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
14
Q

Describe how to monkey patch in the case where you want to replace a function

A
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
15
Q
  • Describe how to monkey patch in the case where you want to wrap a function (2)
A
  • 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
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
16
Q

Why would you wrap a function when monkey patching? (wrapping is a subcase of monkey patching)

A

To add code that executes before and/or after an existing function

17
Q

In the monkey patching case where you wrap a function, what happens when the patch function is invoked?

A
  • –execute the “before” action(s)
  • –call the original function and store the returned result
  • –execute the “after” action(s)
  • –return the stored result of the original function
18
Q

What happens if you forget to return the stored result of the original function, when wrapping a function for monkey patching

A

anything relying on the function’s return value breaks

19
Q

In the case of wrapping a function for monkey patching, how do you unwrap the function (aka undo the monkey patch)?

A

–store the copy of the original function pointer you made before applying the patch back into the function pointer

20
Q

What are 3 unit testing modules in python?

A
  • unittest module
  • doctest module
  • mock module
21
Q

What are high level descriptions/characteristics for

  • unittest (2)
  • doctest (1)
  • mock (1)
A

unittest:

  • allows writing full unit tests
  • tests do not need to be in single file; all tests are found and invoked by the main harness

doctest:

  • allows writing simplified tests as comments

mock:

  • allows insertion of replacement objects that monitor how they are being used and/or return specific values
22
Q

Describe how to use unittest

A
23
Q

Describe doctest (3)

A
24
Q

Describe the mock module (2)

A
  • Allows parts of the system under test to be replaced by mock objects
  • Provides classes Mock and MagicMock
25
Q

Describe the Mock and MagicMock classes (3)

What can you do with them?

A

1.

–callable, create attributes as new mocks when accessed
–accessing the same attribute will always return the same mock
–MagicMock implements Python’s internal “magic” methods

2.

–record the attribute accesses
–permit assertions to be made about what has been done with them

26
Q
  • What’s the first step of code optimization?
  • What do you use to help with this?
A
  • Find out which parts of your code need to be faster
  • A code profiler will tell you which functions account for the most CPU time
27
Q

Compare two code profilers in python (3)

A

profile and cProfile modules

  • both have same API
  • Profile is in pure Python (more overhead, allows extensions)
  • cProfile is written in C and has much less overhead
28
Q

How do you use cprofile? (actual code)

A
29
Q

List python-specific optimizations (2)

A
  • use comprehensions or map/reduce instead of for loops
    • reduces interpreter overhead
  • ensure integers stay within +/- 231 to avoid slower inf-precision math
30
Q
A
31
Q

What is part of the mock module? (1)

A

Mock and MagicMock classes

32
Q

When is setUp and tearDown run?

A
33
Q

How do you introduce a doctest?

A

In a doc string, use >>>

34
Q

How do you import doctest?

A
  • Import doctest
  • doctest.testmod()
35
Q

What are methods of mock class? (3)

A
  • M.called
  • m.call_count
  • m.called_with_args
36
Q

What can you do with the mock class?

A

Among other things:

  • Can assert that method was called
  • What args a method was called with