csca48h style and testing. 2 style the zen of python: import this do the goodger reading!

7
CSCA48H Style and Testing

Upload: clarence-fitzgerald

Post on 13-Dec-2015

218 views

Category:

Documents


1 download

TRANSCRIPT

CSCA48HStyle and Testing

2

Style

The Zen of Python:import this

Do the Goodger reading!

3

Test-first development

Kent Beck, a famous software engineer, created Test Driven Development (TDD):

http://en.wikipedia.org/wiki/Test_Driven_Development

Many professional software developers write their tests first. Most others write their tests as they develop their code.

Steps to TDD:Write a failing test case that uses a new function or exercises an

existing one.

Write just enough code to pass the test.

Refactor the new code to acceptable standards.

4

A real-world observation

“Let's not be pedantic. Write unit tests before you code a method, or after it - in my experience, it matters little, as long as you think about and write the tests at roughly the same time as you write the code.

...

In my experience, when people set out to write unit tests after the fact, they write them poorly, as an afterthought ("I've finished all the code, now I just have to write the unit tests").”

http://www.wakaleo.com/component/content/article/216

5

Testing framework?

Options:nose (separate install, but doesn’t use classes)

PyUnit/unittest (comes with Python!)

doctest (comes with Python!)

CSCA48H:we will use PyUnit: http://docs.python.org/library/unittest.html

6

PyUnit components

import unittestand modules that are being tested, of course

Create classes that inherit from unittest.TestCase

Create methods in these classesJust like nose, test method names start with “test”

Special methods:setUp, tearDown

assertTrue, assertFalse, assertEquals, fail(or just use assert statements)

7

TDD example

We want to keep objects representing clock times (with hours and minutes). The constructor only needs to handle 24-hour time, but the printable representation should be 12-hour time with “am” and “pm”, with “noon” and “midnight” as well.