Slide 1

Slide 1 text

LOGGING AND TESTING AND DEBUGGING, OH MY! @AlSweigart [email protected] (last name rhymes with “why dirt”) bit.ly/ohmypy

Slide 2

Slide 2 text

Why?

Slide 3

Slide 3 text

LOGGING

Slide 4

Slide 4 text

LIVE DEMO!

Slide 5

Slide 5 text

This is terrible.

Slide 6

Slide 6 text

TERRIBLE Print Debugging

Slide 7

Slide 7 text

Why do logging instead of print? • Debug output vs. normal output • Easier to remove debug messages. • Easier to put debug messages back. • Fine grain control with logging levels.

Slide 8

Slide 8 text

Logging import logging logging.basicConfig( level=logging.DEBUG) logging.debug('The log message.') logging.disable(logging.CRITICAL) Back to the live demo… This is looks complicated.

Slide 9

Slide 9 text

No content

Slide 10

Slide 10 text

Logging Levels • DEBUG • INFO • WARN • ERROR • CRITICAL • logging.basicConfig( level=logging.DEBUG) • logging.disable(logging.CRITICAL)

Slide 11

Slide 11 text

DEBUG • Low-level debugging information. • Variables values, etc. • You only look at when something has gone wrong. • Probably going to ignore this.

Slide 12

Slide 12 text

INFO • Report that a perfectly ordinary event happened.

Slide 13

Slide 13 text

WARN

Slide 14

Slide 14 text

ERROR CC BY-SA https://www.flickr.com/photos/74157931@N00/

Slide 15

Slide 15 text

CRITICAL

Slide 16

Slide 16 text

Logging Levels • logging.debug('message') • logging.info('message') • logging.warn('message') • logging.error('message') • logging.critical('message') • logging.basicConfig( level=logging.DEBUG) • logging.disable(logging.CRITICAL)

Slide 17

Slide 17 text

Logging to Screen and File logging.basicConfig( level=logging.DEBUG) logging.basicConfig( filename='logfile.txt', level=logging.DEBUG)

Slide 18

Slide 18 text

Following log files in real time tail –f (On Windows, install Cygwin.) Back to the live demo…

Slide 19

Slide 19 text

No content

Slide 20

Slide 20 text

DEBUGGING

Slide 21

Slide 21 text

import pdb; pdb.set_trace() Back to the live demo…

Slide 22

Slide 22 text

Where am I? l . (List) w (Where)

Slide 23

Slide 23 text

Print Values p

Slide 24

Slide 24 text

Stepping s (Step Into) n (Next / step over) r (Return / step out) c (continue running until next breakpoint)

Slide 25

Slide 25 text

Breakpoints • b (list all breakpoints & their numbers) • b • b • cl (clear bp) • import pdb; pdb.set_trace()

Slide 26

Slide 26 text

Quit the debugger q

Slide 27

Slide 27 text

import pdb; pdb.set_trace()

Slide 28

Slide 28 text

TESTING

Slide 29

Slide 29 text

No content

Slide 30

Slide 30 text

import doctest doctest.testmod() Back to the live demo…

Slide 31

Slide 31 text

No content

Slide 32

Slide 32 text

• Doctests are not a replacement for unit tests. • Doctests are unit tests for documentation. • Back to the live demo…

Slide 33

Slide 33 text

if __name__ == '__main__': import doctest doctest.testmod()

Slide 34

Slide 34 text

No content

Slide 35

Slide 35 text

Logging import logging logging.basicConfig( level=logging.DEBUG) logging.debug('The log message.') logging.disable(logging.CRITICAL)

Slide 36

Slide 36 text

Debugging import pdb; pdb.set_trace()

Slide 37

Slide 37 text

Testing import doctest doctest.testmod()

Slide 38

Slide 38 text

No content

Slide 39

Slide 39 text

LOGGING AND TESTING AND DEBUGGING, OH MY! @AlSweigart [email protected] (last name rhymes with “why dirt”) bit.ly/ohmypy