1 / 7

CSCA48H Style and Testing

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

mark-olsen
Download Presentation

CSCA48H Style and Testing

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 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 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. 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.

More Related