Download presentation
Presentation is loading. Please wait.
Published byClarence Fitzgerald Modified over 9 years ago
1
CSCA48H Style and Testing
2
2 Style The Zen of Python: import this Do the Goodger reading!
3
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
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
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.htmlhttp://docs.python.org/library/unittest.html
6
6 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)
7
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.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.