Upgrade to Pro — share decks privately, control downloads, hide ads and more …

Guide to TDD & Flight to BDD with Python/Django

Guide to TDD & Flight to BDD with Python/Django

Talk presented at PyCon India 2012

Avatar for Saket Bhushan

Saket Bhushan

August 16, 2012
Tweet

More Decks by Saket Bhushan

Other Decks in Programming

Transcript

  1. What we will do? • Overview • Getting Started is

    Painful • Unit testing django models, views, forms • Integration testing with selenium • The shift to BDD • BDD Demo • Q & A
  2. A basic unittest import unittest2 class TestDemo(unittest2.TestCase): " A demo

    of simple unit test" def setup(self): self.char = 'a' def tearDown(self): del self.char def test_first_pass(self): "Asserting that the character is not b" self.assertNotEqual(self.char, 'b') def test_second_fail(self): "Making a false assertion" assert self.char != 'c'
  3. A good TDD sequence for a project 1. Write test

    for models a. the objects are being created and they have the desired relations as per specs b. the functions are returning correct values & manipulating db as per specs 2. Write test for forms - red, green a. states of forms b. perms. of good & bad forms, data 3. Write test for views a. response variable(200, 404) b. template & context variable c. behavior via decorator(@login_required etc.) 4. Write test for custom middlewares & template tags.
  4. A sample fixture test from django_dynamic_fixture import G class ChoiceModelTest(TestCase):

    def test_choice_creation(self): # set up the values poll0 = G(Poll) poll1 = G(Poll) ch0 = G(Choice, poll=poll0) ch1 = G(Choice, poll=poll0) # test basic values polls_in_db = Poll.objects.all() poll_zero = polls_in_db[0] poll_one = polls_in_db[1] choices = Choice.objects.filter(poll = poll_zero) self.assertEquals(choices.count(), 4) ch_zero = choices[0] self.assertEquals(ch_zero.choice, ch0.choice) self.assertEquals(ch_zero.votes, ch0.votes)
  5. Settings for nose test runner TEST_RUNNER = 'django_nose. NoseTestSuiteRunner' NOSE_ARGS

    = [ '--with-coverage', '--cover-package=polls', '--with-progressive', '--verbosity=0', '--with-fixture-bundling', ]
  6. Integ. test with Selenium 1. Get the browser 2. Simulate

    Behavior a. use selectors i. locate by name, id, xpath, hyperlink test ii. browser extensions b. trigger keypress/action 3. Assert
  7. BDD - GWT/Gherkin Feature: Run or Riot In order to

    test my bot's strength, As a bot developer, I want to program my bot's decision making. Scenario: Boring Opponent Given the bots network has been trained When attacked by RA-One Then the bot should kill the opponent Scenario: Legendary opponent Given the bot's network has been trained When attacked by Robot Then the bot should bow down in respect! http://dannorth.net/whats-in-a-story/
  8. BDD libraries • Lettuce - /gabrielfalcao/lettuce • Freshen - /rlisagor/freshen

    • Behave - /jeamland/behave • ...& counting.... (all above are format github.com/w+/w+)
  9. Few more libraries • Another full featured testing tool -

    ◦ pytest.org • WSGI testing ◦ /pypi/WebTest • Test Discovery: ◦ /jezdez/django-discover-runner • Better Assertion Messages: ◦ /gabrielfalcao/sure
  10. Practicality beats Purity In practical, the difference between theory &

    practical, is greater than, the difference between theory & practical, in theory.