39
TDD* Alex Kavanagh @ajkavanagh *Test Driven Development

TDD super mondays-june-2014

Embed Size (px)

Citation preview

Page 1: TDD super mondays-june-2014

TDD*Alex Kavanagh @ajkavanagh

*Test Driven Development

Page 2: TDD super mondays-june-2014
Page 3: TDD super mondays-june-2014

–Michael Sinz

“Programming is like sex … one mistake and you have to support it

for the rest of your life.”

Page 4: TDD super mondays-june-2014

–Steve M. McConnell

“The problem with quick and dirty is that Dirty

remains long after the quick has been

forgotten.”

Page 5: TDD super mondays-june-2014

Agile with Scrum or Kanban

Page 6: TDD super mondays-june-2014
Page 7: TDD super mondays-june-2014
Page 8: TDD super mondays-june-2014

Continuous Integration

& Deployment

Page 9: TDD super mondays-june-2014

Reducing pressure to ship

Page 10: TDD super mondays-june-2014

If you don’t automate it,

it won’t happen!

Page 11: TDD super mondays-june-2014

What is TDD?

Page 12: TDD super mondays-june-2014
Page 13: TDD super mondays-june-2014

Write Test

Page 14: TDD super mondays-june-2014

Run the test and see it fail

$ nosetests -v websandpym/tests/test_auth_utils.py:Utils_Tests.test__generate_jti_claim test__generate_jti_claim (websandpym.tests.test_auth_utils.Utils_Tests) ... ERROR

====================================================================== ERROR: test__generate_jti_claim (websandpym.tests.test_auth_utils.Utils_Tests) ---------------------------------------------------------------------- Traceback (most recent call last): File "/Users/alex/unison-root/Projects/Websand/instances/websand-pym/websand-pym/websandpym/tests/test_auth_utils.py", line 101, in test__generate_jti_claim claims = auth_utils._generate_jti_claim() nose.proxy.AttributeError: 'module' object has no attribute '_generate_jti_claim' -------------------- >> begin captured logging << -------------------- passlib.utils.compat: DEBUG: loaded lazy attr 'SafeConfigParser': <class 'configparser.ConfigParser'> passlib.utils.compat: DEBUG: loaded lazy attr 'NativeStringIO': <class '_io.StringIO'> passlib.utils.compat: DEBUG: loaded lazy attr 'BytesIO': <class '_io.BytesIO'> passlib.registry: DEBUG: registered 'bcrypt' handler: <class 'passlib.handlers.bcrypt.bcrypt'> passlib.registry: DEBUG: registered 'pbkdf2_sha256' handler: <class 'passlib.handlers.pbkdf2.pbkdf2_sha256'> --------------------- >> end captured logging << ---------------------

---------------------------------------------------------------------- Ran 1 test in 0.002s

FAILED (errors=1)

Page 15: TDD super mondays-june-2014

Write some code

Page 16: TDD super mondays-june-2014

Run test again

$ nosetests -v websandpym/tests/test_auth_utils.py:Utils_Tests.test__generate_jti_claim test__generate_jti_claim (websandpym.tests.test_auth_utils.Utils_Tests) ... ok

---------------------------------------------------------------------- Ran 1 test in 0.014s

OK

Page 17: TDD super mondays-june-2014

…until the test passes

Page 18: TDD super mondays-june-2014

then …

Page 19: TDD super mondays-june-2014

Refactor

Page 20: TDD super mondays-june-2014
Page 21: TDD super mondays-june-2014

In summary

Page 22: TDD super mondays-june-2014
Page 23: TDD super mondays-june-2014

TDD and Python

Page 24: TDD super mondays-june-2014

Python Testing

• UnitTest

• Nose

• py.test

• sure

Page 25: TDD super mondays-june-2014

unittest

• “Inspired” by JUnit

• Has fixtures, test cases, suites and a test runner.

• Uses ‘assert  <something’ or

• self.<assert_method>(params…)  

• to do assertions.

Page 26: TDD super mondays-june-2014

nose• Extends unittest - to make testing easier

• Lots of plugins (not all Python3 compatible).

• Basically:

• finds tests

• Runs them

• Gives nicer outputs that unittest.

• https://nose.readthedocs.org/en/latest/

Page 27: TDD super mondays-june-2014

py.test

• Also a test-runner!

• pytest can run nose, unittest and doctest style test suites

• Very flexible.

• http://pytest.org/latest/contents.html

Page 28: TDD super mondays-june-2014

sure• A DSL for writing tests in Python

• Inspired by should.js

• Write this:

• expect(something).to.be.equal(4)  

• something.should.be.equal.to(4)  

• Instead of this:

• self.assertEqual(something,  4)

Page 29: TDD super mondays-june-2014

Where to next?http://pythontesting.net/start-here/

Page 30: TDD super mondays-june-2014

TDD and Javascript

Well, actually CoffeeScript as it’s nicer for tests :)

Page 31: TDD super mondays-june-2014

Yes, coffee script :)describe('User',  function(){      describe('#save()',  function(){          it('should  save  without  error',  function(done){              var  user  =  new  User('Luna');              user.save(function(err){                  if  (err)  throw  err;                  done();              });          })      })  })

describe  ‘User’,  -­‐>      describe  ‘#save’,  -­‐>          it  ‘should  save  without  error’,  (done)  -­‐>              user  =  new  User(‘Luna’)              user.save  (err)  -­‐>                  if(err)  throw  err                  done()

Page 32: TDD super mondays-june-2014

JS Testing• Test your JS in the browser (phantomjs, et al.)

• Writing node.js? Test your node.js code (mocha, jasmine, et al.)

• AngularJS? EmberJS? (<other> JS framework). Test your code in various browsers & node.js using Karma

• End-2-End testing - bootstrap your app, with the browser and have e2e tests.

Page 33: TDD super mondays-june-2014

JS Testing Tools

• Karma - http://karma-runner.github.io

• A simple tool that allows you to execute JavaScript code in multiple real browsers.

• Mocha - http://visionmedia.github.io/mocha/

• Asynchronous testing fun!

• Should.js, expect.js, chai - DSL style assertion libraries.

Page 34: TDD super mondays-june-2014

Things I didn’t coverMocking

BDD (but hopefully Philip did!)

Continuous Integration

Continuous Delivery

…but you have to have an automated test system to even begin to attempt these.

Page 35: TDD super mondays-june-2014

–codebetter.com

“Unit Tests confirm we have written the code right [sic].

Acceptance Tests that we built the right software.”

Page 36: TDD super mondays-june-2014

–Kent Beck

“TDD doesn’t drive good design. TDD gives you immediate feedback about what is likely to

be bad design.”

Page 37: TDD super mondays-june-2014

–John Woods

“Always code as if the guy who ends up maintaining your code

will be a violent psychopath

who knows where you live.”

Page 38: TDD super mondays-june-2014

@ajkavanagh

Page 39: TDD super mondays-june-2014

Image Creditshttp://www.cfn308bestnetwork.com/sites/default/files/writing.gif http://2.bp.blogspot.com/_4OIF7pROmHM/SwV3hzmTaAI/AAAAAAAAAe8/wVyXyHhMrT0/s1600/54208__468x_failed-exam-answers-22.jpg http://allantyoung.com/wp-content/uploads/2008/11/matrixcode.jpg http://icons.iconarchive.com/icons/icons-land/vista-multimedia/256/Play-Mode-Repeat-All-Hot-icon.png http://coltandsage.com/wp-content/uploads/2013/12/rinse-repeat.jpg http://carlosble.com/downloads/tdd.png http://www.ben-morris.com/wp-content/library/refactoring-vs-rewriting.gif http://www.repmanblog.com/wp_app/wp-content/uploads/2014/03/why-1.jpg http://www.lynnecazaly.com.au/storage/agile%20manifesto%20screenshot.jpg?__SQUARESPACE_CACHEVERSION=1394163978554 http://www.versionone.com/images/agile101/agile_development_value_proposition.gif http://clickhowto.com/wp-content/uploads/2013/01/Agile-methodologies.jpg http://blog.crisp.se/wp-content/uploads/2013/02/continuous-delivery-deployment-sm.jpg http://gb.fotolibra.com/images/previews/120351-a-pressure-gauge-showing-high-pressure.jpeg http://www.layeredthoughts.com/wp-content/uploads/2012/09/200712_regex_no_manual_labor.jpg https://cdn.tutsplus.com/net/uploads/legacy/767_testDrivenDev/tdd.png http://www.wpclipart.com/signs_symbol/words/thank_you/Thank_You_note_big_outline_T.png