
Test Driven Development SN Biggs York Plasma Institute TDDj7th July 2017j1/40 Introduction Image ref: https://usercontent2.hubstatic.com/8530569 f520.jpg TDDj7th July 2017j2/40 Outline Unit Testing Unit Testing is a Safety Net Unit Test Structure: Setup, Exercise, Assert xUnit { The Testing Framework Exercise { Write Some Unit Tests Test Driven Development (TDD) A Different Mindset Test First!? Fail, Pass, Refactor: A Simple Example Exercise { Take TDD for a Test Drive A Word of Warning: Pick the Right Tool for the Right Job Summary and Further Reading TDDj7th July 2017j3/40 Unit Testing is a Safety Net Image ref: http://www.fitnessbin.com/wp-content/uploads/2015/12/Rock-Climbing-4.jpg TDDj7th July 2017j4/40 Unit Testing is a Safety Net Image ref: http://www.jamkey.fr/wp-content/uploads/2015/05/tests-pyramid.png TDDj7th July 2017j5/40 Unit Testing is a Safety Net TDDj7th July 2017j6/40 Unit Testing is a Safety Net TDDj7th July 2017j6/40 Unit Testing is a Safety Net TDDj7th July 2017j6/40 Unit Testing is a Safety Net TDDj7th July 2017j6/40 Unit Testing is a Safety Net TDDj7th July 2017j6/40 Unit Testing is a Safety Net TDDj7th July 2017j6/40 Unit Testing is a Safety Net TDDj7th July 2017j6/40 Unit Testing is a Safety Net TDDj7th July 2017j6/40 Test Structure: Setup, Exercise, Assert class Calculator(): def add(self, augend, addend): 'Adds the addend to the augend' return augend+ addend TDDj7th July 2017j7/40 Test Structure: Setup, Exercise, Assert def testAddBothPositive(self): # Setup a=3 b=2 calc= Calculator() # Specify expected result expected=5 # Exercise system under test actual= calc.add(a, b) # Verify result self.assertEqual(actual, expected) TDDj7th July 2017j8/40 xUnit - The Testing Framework Began as sUnit for the Smalltalk language by Kent Beck in 1998 Since ported to many languages (cUnit, cppUnit, fUnit, pyUnit, etc.) Object-oriented - test classes inherit assertions Provides test suites, test runners, formatted test results, etc. Image ref: https://d21ii91i3y6o6h.cloudfront.net/gallery images/from proof/4404/large/1425068890/xunit-dot-net-small-logo.png TDDj7th July 2017j9/40 xUnit - The Testing Framework import unittest from Calculator import Calculator class testCalculator(unittest.TestCase): def setUp(self): self.calc= Calculator() def testAddBothPositive(self): 'Tests that two positive numbers are added correctly' # Exercise system under test and verify result self.assertEqual(self.calc.add(3,2),5) TDDj7th July 2017j10/40 xUnit - The Testing Framework To run the unit tests (from bash): $ python -m unittest module[.class[.test]] [-v] e.g. $ python -m unittest testCalculator.testCalculator.testAddBothPositive or discover tests automatically: $ python -m unittest discover TDDj7th July 2017j11/40 Exercise - Write Some Unit Tests ∗ 1 Download my basic calculator class from here: http://www- users.york.ac.uk/∼snb519/coding-club-tdd-examples/Calculator.py 2 Write a test class, testCalculator.py, that extends unittest.TestCase (see slide 10) 3 Write tests (see slide 8) for adding different combinations of numbers, e.g. both positive, both negative, one of each, floats, etc. 4 Run the tests (see slide 11) to see what the test results look like { try with and without the -v flag, and try running individual tests and test discovery 5 Try changing Calculator.py to see what failing tests look like 6 Extension - can you write any tests that should pass but actually fail, e.g. test that passing in strings is handled correctly ∗You can also get the slides here: http://www-users.york.ac.uk/∼snb519/coding-club-tdd-examples/slides.pdf TDDj7th July 2017j12/40 A Different Mindset Image ref: http://www.fitnessbin.com/wp-content/uploads/2015/12/Rock-Climbing-4.jpg TDDj7th July 2017j13/40 Test First!? Image ref: https://leantesting-wp.s3.amazonaws.com/resources/wp-content/uploads/2015/02/tdd-circle-of-life.png TDDj7th July 2017j14/40 Test First!? 1 Identify desired functionality 2 Write failing test 3 Make it compile as quickly as possible 4 Make it pass a quickly as possible 5 Remove duplication while maintaining 100% pass rate 6 Repeat as required TDDj7th July 2017j15/40 Test First!? Image ref: https://leantesting-wp.s3.amazonaws.com/resources/wp-content/uploads/2015/02/tdd-circle-of-life.png TDDj7th July 2017j16/40 Fail, Pass, Refactor: A Simple Example 1 Identify desired functionality 2 Write failing test 3 Make it compile as quickly as possible 4 Make it pass a quickly as possible 5 Remove duplication while maintaining 100% pass rate 6 Repeat as required TDDj7th July 2017j17/40 Fail, Pass, Refactor: A Simple Example 1 Identify desired functionality 2 Write failing test 3 Make it compile as quickly as possible 4 Make it pass a quickly as possible 5 Remove duplication while maintaining 100% pass rate 6 Repeat as required TDDj7th July 2017j18/40 Fail, Pass, Refactor: A Simple Example def testSubtractBothPositive(self): 'Tests that two positive numbers are subtracted correctly' # Exercise system under test and verify result self.assertEqual(self.calc.subtract(3,2),1) TDDj7th July 2017j19/40 Fail, Pass, Refactor: A Simple Example E =============================================================== ERROR: testSubtractBothPositive (testCalculator.testCalculator) Tests that two positive numbers are subtracted correctly --------------------------------------------------------------- Traceback (most recent call last): File "testCalculator.py", line 40, in testSubtractBothPositiv self.assertEqual(self.calc.subtract(3, 2), 1) AttributeError: Calculator instance has no attribute 'subtract' --------------------------------------------------------------- Ran 1 test in 0.000s FAILED (errors=1) TDDj7th July 2017j20/40 Fail, Pass, Refactor: A Simple Example 1 Identify desired functionality 2 Write failing test 3 Make it compile as quickly as possible 4 Make it pass a quickly as possible 5 Remove duplication while maintaining 100% pass rate 6 Repeat as required TDDj7th July 2017j21/40 Fail, Pass, Refactor: A Simple Example def subtract(self): pass TDDj7th July 2017j22/40 Fail, Pass, Refactor: A Simple Example E =============================================================== ERROR: testSubtractBothPositive (testCalculator.testCalculator) Tests that two positive numbers are subtracted correctly --------------------------------------------------------------- Traceback (most recent call last): File "testCalculator.py", line 40, in testSubtractBothPositiv self.assertEqual(self.calc.subtract(3, 2), 1) TypeError: subtract() takes exactly 1 argument (3 given) --------------------------------------------------------------- Ran 1 test in 0.000s FAILED (errors=1) TDDj7th July 2017j23/40 Fail, Pass, Refactor: A Simple Example def subtract(self, minuend, subtrahend): pass TDDj7th July 2017j24/40 Fail, Pass, Refactor: A Simple Example F =============================================================== FAIL: testSubtractBothPositive (testCalculator.testCalculator) Tests that two positive numbers are subtracted correctly --------------------------------------------------------------- Traceback (most recent call last): File "testCalculator.py", line 40, in testSubtractBothPositiv self.assertEqual(self.calc.subtract(3, 2), 1) AssertionError: None != 1 --------------------------------------------------------------- Ran 1 test in 0.000s FAILED (failures=1) TDDj7th July 2017j25/40 Fail, Pass, Refactor: A Simple Example 1 Identify desired functionality 2 Write failing test 3 Make it compile as quickly as possible 4 Make it pass a quickly as possible 5 Remove duplication while maintaining 100% pass rate 6 Repeat as required TDDj7th July 2017j26/40 Fail, Pass, Refactor: A Simple Example def subtract(self, minuend, subtrahend): return1 TDDj7th July 2017j27/40 Fail, Pass, Refactor: A Simple Example . --------------------------------------------------------------- Ran 1 test in 0.000s OK TDDj7th July 2017j28/40 Fail, Pass, Refactor: A Simple Example 1 Identify desired functionality 2 Write failing test 3 Make it compile as quickly as possible 4 Make it pass a quickly as possible 5 Remove duplication while maintaining 100% pass rate 6 Repeat as required TDDj7th July 2017j29/40 Fail, Pass, Refactor: A Simple Example def subtract(self, minuend, subtrahend): return3-2 TDDj7th July 2017j30/40 Fail, Pass, Refactor: A Simple Example . --------------------------------------------------------------- Ran 1 test in 0.000s OK TDDj7th July 2017j31/40 Fail, Pass, Refactor: A Simple Example def subtract(self, minuend, subtrahend): return minuend-2 TDDj7th July 2017j32/40 Fail, Pass, Refactor: A Simple Example . --------------------------------------------------------------- Ran 1 test in 0.000s OK TDDj7th July 2017j33/40 Fail, Pass, Refactor: A Simple Example def subtract(self, minuend, subtrahend): return minuend- subtrahend TDDj7th July 2017j34/40 Fail, Pass, Refactor: A Simple Example . --------------------------------------------------------------- Ran 1 test in 0.000s OK TDDj7th July 2017j35/40 Fail, Pass, Refactor: A Simple Example 1 Identify desired functionality 2 Write failing test 3 Make it compile as quickly as possible 4 Make it pass a quickly as possible 5 Remove duplication while maintaining 100% pass rate 6 Repeat as required TDDj7th July 2017j36/40 Exercise - Write Some Unit Tests 1 Using Calculator.py and testCalculator.py that you have already developed, test and implement calc.multiply(multiplicand, multiplier) and calc.divide(divend, divisor) using TDD 2 Implement some extra features using TDD, e.g. handling two numbers passed in as strings element-wise array operations (see
Details
-
File Typepdf
-
Upload Time-
-
Content LanguagesEnglish
-
Upload UserAnonymous/Not logged-in
-
File Pages47 Page
-
File Size-