70 likes | 143 Views
CSCA48H Style and Testing. Style. The Zen of Python: import this Do the Goodger reading!. Test-first development. Kent Beck, a famous software engineer, created Test Driven Development (TDD): http://en.wikipedia.org/wiki/Test_Driven_Development
E N D
Style • The Zen of Python: • import this • Do the Goodger reading!
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.
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
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
PyUnit components • import unittest • and modules that are being tested, of course • Create classes that inherit from unittest.TestCase • Create methods in these classes • Just like nose, test method names start with “test” • Special methods: • setUp, tearDown • assertTrue, assertFalse, assertEquals, fail(or just use assert statements)
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.