Test Driven Development in Python

11
Test Driven Development in Python Anoop Thomas Mathew Agiliq Info Solutions Pvt. Ltd.

description

 

Transcript of Test Driven Development in Python

Page 1: Test Driven Development in Python

Test Driven Development in Python

Anoop Thomas MathewAgiliq Info Solutions Pvt. Ltd.

Page 2: Test Driven Development in Python

Overview

● About TDD

● TDD and Python

● unittests

● Developing with Tests

● Concluding Remarks

● Open Discussion

Page 3: Test Driven Development in Python

“ Walking on water and developing software from a specification are easy if both are frozen. - Edward V Berard”

Page 4: Test Driven Development in Python

About Test Driven Development (TDD)

● Write tests for the use case

● Run it (make sure it fails and fails miserably)

● Write code and implement the required functionality with relevant level of detail

● Run the test

● Write test for addition features

● Run all test

● Watch it succeed. Have a cup of coffee !

Page 5: Test Driven Development in Python

Advantages of TDD

● application is determined by using it

● written minimal amount of application code

– total application + tests is probably more

– objects: simpler, stand-alone, minimal dependencies

● tends to result in extensible architectures

● instant feedback

Page 6: Test Driven Development in Python

Unittest

import unittest

class MyTest(unittest.TestCase):

def testMethod(self):

self.assertEqual(1 + 2, 3, "1 + 2 !=3")

if __name__ == '__main__':

unittest.main()

Page 7: Test Driven Development in Python

The Testimport unittest

from demo import Greater

class DemoTest(unittest.TestCase):

def test_number(self):

comparator = Greater()

result = comparator.greater(10,5)

self.assertTrue(result)

def test_char(self):

comparator = Greater()

result = comparator.greater('abcxyz', 'AB')

self.assertTrue(result)

def test_char_equal(self):

comparator = Greater()

result = comparator.greater('4', 3)

self.assertTrue(result)

if __name__ == '__main__':

unittest.main()

Page 8: Test Driven Development in Python

The Programclass Greater(object):

def greater(self, val1, val2):

if type(val1) ==str or type(val2) == str:

val1 = str(val1)

val2 = str(val2)

sum1 = sum([ord(i) for i in val1])

sum2 = sum([ord(i) for i in val2])

if sum1 > sum2:

return True

else:

return False

if val1>val2:

return True

else:

return False

Page 9: Test Driven Development in Python

Test Again

1. Add new test for features/bugs

2. Resolve the issue, make the test succeed.

3. Iterate from Step 1

Page 10: Test Driven Development in Python

Beware!!!

Murphy is everywhere.

Page 11: Test Driven Development in Python

Let's Discuss