Top Banner
CSCA48H Style and Testing
7

CSCA48H Style and Testing. 2 Style The Zen of Python: import this Do the Goodger reading!

Dec 13, 2015

Download

Documents

Welcome message from author
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Page 1: CSCA48H Style and Testing. 2 Style The Zen of Python: import this Do the Goodger reading!

CSCA48HStyle and Testing

Page 2: CSCA48H Style and Testing. 2 Style The Zen of Python: import this Do the Goodger reading!

2

Style

The Zen of Python:import this

Do the Goodger reading!

Page 3: CSCA48H Style 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.

Page 4: CSCA48H Style and Testing. 2 Style The Zen of Python: import this Do the Goodger reading!

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

Page 5: CSCA48H Style and Testing. 2 Style The Zen of Python: import this Do the Goodger reading!

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

Page 6: CSCA48H Style and Testing. 2 Style The Zen of Python: import this Do the Goodger reading!

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)

Page 7: CSCA48H Style and Testing. 2 Style The Zen of Python: import this Do the Goodger reading!

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.