Slide 1

Slide 1 text

Guide to TDD & Flight to BDD with python/django

Slide 2

Slide 2 text

About Us Me: A humble student of the trade! You: ?

Slide 3

Slide 3 text

What I won't do? ● Comparisons ● No Mocking/DocTests ● Opinions ● Conclusions

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

TDD - Initial Pain

Slide 6

Slide 6 text

About the topic

Slide 7

Slide 7 text

When & What & How

Slide 8

Slide 8 text

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'

Slide 9

Slide 9 text

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.

Slide 10

Slide 10 text

No content

Slide 11

Slide 11 text

Fixture Management (& Namespaces) are one honking great idea -- let's do more of those!

Slide 12

Slide 12 text

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)

Slide 13

Slide 13 text

Test Runners

Slide 14

Slide 14 text

Settings for nose test runner TEST_RUNNER = 'django_nose. NoseTestSuiteRunner' NOSE_ARGS = [ '--with-coverage', '--cover-package=polls', '--with-progressive', '--verbosity=0', '--with-fixture-bundling', ]

Slide 15

Slide 15 text

Integration/Performance/System/Fun ctional/Web Tests

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

Code

Slide 18

Slide 18 text

Behavior Driven Development (Sapir Whorf Hypothesis )

Slide 19

Slide 19 text

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/

Slide 20

Slide 20 text

.....although that may not be obvious at first unless you're Dutch.

Slide 21

Slide 21 text

BDD libraries ● Lettuce - /gabrielfalcao/lettuce ● Freshen - /rlisagor/freshen ● Behave - /jeamland/behave ● ...& counting.... (all above are format github.com/w+/w+)

Slide 22

Slide 22 text

BDD - Demo

Slide 23

Slide 23 text

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

Slide 24

Slide 24 text

Practicality beats Purity In practical, the difference between theory & practical, is greater than, the difference between theory & practical, in theory.

Slide 25

Slide 25 text

Thanks & Questions?? ● Contact ○ at gamebit07 (twitter) ○ gamebit07 at gmail dot com