10.07.2015 Views

Unit Testing: doctest and unittest (PyUnit)

Unit Testing: doctest and unittest (PyUnit)

Unit Testing: doctest and unittest (PyUnit)

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Test-driven development Instead of putting off testing until the end, Write your test cases before you code!● Once you have the design/specification(e.g., pre/post-conditions of a method),● Write comprehensive test cases first● Then code <strong>and</strong> test along the way● Your code is correct if it passes all the tests <strong>Unit</strong> testing: each class/method in isolation Integration testing:do all components work correctly together?CMPT140: unit testing 30 Nov 20106


Test frameworks in Python Python has two built-in libraries to help you withrunning tests (plus more 3rd-party libraries): <strong>doctest</strong>: simple framework, easy to use● “Test script” goes in docstring● Small function at bottom of file runs tests <strong>unittest</strong> (Py<strong>Unit</strong>): more sophisticated● Test cases organized into test suites● Test cases may share common test fixturesCMPT140: unit testing 30 Nov 20107


<strong>doctest</strong>: simple test library In each docstring, include a test script:“””Calculate factorial.>>> factorial(5)24“”” Comm<strong>and</strong>/response exactly as in IDLE:● Prefix comm<strong>and</strong>s with '>>>'● Put expected output on line by itself● If you expect an exception, write theexpected “red text” for the exception! More details in Python <strong>doctest</strong> library docsSee factorialtest.pyCMPT140: unit testing 30 Nov 20108


<strong>doctest</strong>: running tests To run <strong>doctest</strong>, put this function at the end:if __name__ == “__main__”:import <strong>doctest</strong><strong>doctest</strong>.testmod() The if statement prevents the tests from beingrun if another program is importing the module <strong>doctest</strong>.testmod() scans through all docstringsin the file <strong>and</strong> runs all test cases it can find If all tests succeed, output is silent If any tests fail, a report is output to screen You can also run: python -m <strong>doctest</strong> myfile.pyCMPT140: unit testing 30 Nov 20109


<strong>doctest</strong>: test narratives <strong>doctest</strong> searches your docstrings for textresembling a test script (e.g., '>>>') You can also put your test scripts in a separatefile, interspersed amongst your documentation● As though the whole file were a docstring A test narrative is a document written forhumans (e.g., user manual) where test casesare interleaved with the narrative See factorialtest.txt for an example Run: python -m <strong>doctest</strong> factorialtest.txt Or in code: <strong>doctest</strong>.testfile(“factorialtest.txt”)CMPT140: unit testing 30 Nov 201010


GOOD NEIGHBORS USANOTES TO FINANCIAL STATEMENTS (Continued)Income TaxesThe Organization is a nonprofit organization that is exempt from federal income tax underInternal Revenue Code Section 501(c)(3).NOTE C - PROPERTY AND EQUIPMENTProperty <strong>and</strong> equipment consisted of the following at December 31, 2009:Automobile $ 22,645Computer 3,254Office equipment <strong>and</strong> fixture 9,839Website 76,650Total property <strong>and</strong> equipment 112,388Less: Accumulated depreciation (28,684)Property <strong>and</strong> equipment, net $ 83,704Depreciation expense for the year ended December 31,2009 was $ 19,757.NOTE D - ACCRUED EXPENSES AND OTHER CURRENT LIABILITIES:Accrued expenses <strong>and</strong> other current liabilities at December 31, 2009 consist of the following:Accrued liabilities $ 517Contract payable,current 3,977Payroll taxes 4,433NOTE E — Contract Payable$ 8,927Contract payable on automobile, 11.49% note, interest <strong>and</strong> principal due monthly, is $7,710 atDecember 31, 2009, <strong>and</strong> scheduled debt maturity for future years ending December 31 are asfollows:2010 $ 3,9772011 3,733• 9


<strong>unittest</strong>: running tests Put the following at the end of the file of tests:if __name__ == '__main__':<strong>unittest</strong>.main() Run from the comm<strong>and</strong> line:python my<strong>unittest</strong>s.py Outputs results <strong>and</strong> any failures Can also run from IDLE, but it will try toSystemExit after tests are finishedCMPT140: unit testing 30 Nov 201013

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!