Presentation is loading. Please wait.

Presentation is loading. Please wait.

Unit testing Unit testing with Python and Zope Søren Roug February 2008.

Similar presentations


Presentation on theme: "Unit testing Unit testing with Python and Zope Søren Roug February 2008."— Presentation transcript:

1 Unit testing Unit testing with Python and Zope Søren Roug February 2008

2 Purpose of Unit testing To validate that individual units of source code are working properly It is executable documentation of the source By showing what output is expected from given input Continual quality assurance of the code Works like a burglar alarm Doesn't guarantee bugfree code But catches most unsophisticated bugs

3 Python unit testing exercise Create function called divide that divides arg1 with arg2 division.py def divide(arg1, arg2): return arg1 / arg2 http://pyunit.sourceforge.net/

4 Unit test to verify the code import unittest from division import divide class TestDivision(unittest.TestCase): def test_int(self): self.assertEqual(2.4285714285714284, divide(17, 7) ) def test_float(self): self.assertEqual(2.4285714285714284, divide(17.0, 7) ) if __name__ == '__main__': unittest.main()

5 Safer way import unittest from division import divide class TestDivision(unittest.TestCase): def test_int(self): assert abs(2.428571 - divide(17, 7)) < 0.00001 def test_float(self): assert abs(2.428571 - divide(17.0, 7)) < 0.00001 if __name__ == '__main__': unittest.main()

6 Exercise Fix division.py to work as the unit test expects it Next, add a unit test that provides the arguments as text strings divide(“17”,”7”) Then: Fix division.py to work as the unit test expects it This cycle is called Test-Driven Development (TDD)

7 But TDD is very difficult to get used to because it is so counter- intuitive

8 Why Unit tests? It makes you a better programmer The design is cleaner It makes it safer to modify code Unit tests gives you peace of mind.

9 Unit testing in Zope Since version 2.8, Zope has had ZopeTestCase built in That's what we'll be using http://zope.org/Members/shh/ZopeTestCaseWiki

10 Setting it up Check out the CountCoup product to a Zope site Create a 'tests' directory in the product Copy framework.py, runalltests.py, runtest and testTemplate.py to the tests directory Verify:./runtest testTemplate.py./runtest runalltests.py

11 First real Unit test A CountCoup object has already been set up in afterSetup() Now we call the index_html method RESPONSE = self.app.REQUEST.RESPONSE self.app.mycounter.index_html('testfile.pp t', self.app.REQUEST, RESPONSE) self.assertEquals(RESPONSE.getHeader("loca tion"), 'testfile.ppt')

12 Unit test interlude Print the output from listcounts() to see what it contains print self.app.mycounter.listcounts()

13 Unit test clearcounts() Call the clearcounts() and then the output from listcounts() is expected to be the empty list Potentially ensure something is counted by calling index_html a couple of times before you clear the counter

14 Unit test listcounts() Call clearcounts() to ensure a known state Call index_html with two different paths Several times for each path Check that listcounts() shows the expected values

15 Test with addresssum=0 Copy the entire class to another class name in the same file Add the class to the test_suite in the bottom In afterSetUp() simulate what would happen if the user had created the object with the addresssum unchecked …manage_addCountCoup('mycounter','Title') Run the tests again Fix the code – fix the tests

16 Revisiting the tests for addresssum=1 Did we test listcounts() correctly when we created the object with addresssum=1? index_html looks at REQUEST['REMOTE_ADDR'], so we need to call index_html with that set to various things self.app.REQUEST.set('REMOTE_ADDR', '10.0.0.1') Then check that listcounts() works

17 End of exercise


Download ppt "Unit testing Unit testing with Python and Zope Søren Roug February 2008."

Similar presentations


Ads by Google