SlideShare une entreprise Scribd logo
1  sur  42
Télécharger pour lire hors ligne
Test Driven Development in Python




      Siddharta Govindaraj
  siddharta@silverstripesoftware.com
What is Test Driven Development (TDD)?




http://www.flickr.com/photos/johnsyweb/3051647719/
Red, Green, Refactor

●   First write a test
●   Write code to pass the
    test
●   Clean up the code
●   Repeat
TDD Example




Write a function to check whether a given input
             string is a palindrome
code.py
def is_palindrome(input_str):
    pass
tests.py
from code import is_palindrome


def test_function_should_accept_palindromic_words():
    input = "noon"
    assert is_palindrome(input) == True
Result
code.py
def is_palindrome(input_str):
    return input_str == input_str[::-1]
Result
tests.py
def test_function_should_ignore_case():
    input = "Noon"
    assert is_palindrome(input) == True
Result
code.py
def is_palindrome(input_str):
    input_clean = input_str.lower()
    return input_clean == input_clean[::-1]
Result
tests.py
def test_function_should_ignore_trailing_space():
    input = "Noon   "
    assert is_palindrome(input) == True
code.py
def is_palindrome(input_str):
    input_clean = input_str.strip().lower()
    return input_clean == input_clean[::-1]
tests.py
def test_function_should_ignore_spaces_in_text():
    input = "ab raca carba"
    assert is_palindrome(input) == True
code.py
def is_palindrome(input_str):
    input_stripped = input_str.replace(" ", "")
    input_clean = input_stripped.lower()
    return input_clean == input_clean[::-1]
tests.py
def test_function_should_handle_combined_characters():
    input = u"u0bb4u0bbfuu0b95u0bb4u0bbf"
    assert is_palindrome(input) == True



                 (Input is ழ கழ )
Reversing unicode strings

The String: ழ கழ
Characters: ழ + ி + க + ழ + ி
Wrong: ி + ழ + க + ி + ழ
Right: ழ + ி + க + ழ + ி
# naïve implementation to pass the test



def is_palindrome(input_str):

   def reverse_string(input_str):

       def is_combining_char(char):

           chars = [u"u0bcd"]

           return char in chars

       reversed_chars = []

       for char in input_str:

           if is_combining_char(char): reversed_chars.insert(1, char)

           else: reversed_chars.insert(0, char)

       return "".join(reversed_chars)

   input_stripped = input_str.replace(" ", "")

   input_clean = input_stripped.lower()

   reversed_string = reverse_string(input_clean)

   return input_clean == reversed_string
And so it continues...

●   Turns out reversing a string is quite complex
    when unicode scripts come into the picture
●   Many different cases to consider
●   Unit tests can validate the complex code logic
    and check for regression errors
Why is unit testing important?

●   Quality
●   Regression
●   Safety Net
●   Integration with build
    and CI tools
●   Documentation
Attributes of good tests

●   Fast
●   Clear
●   Isolated
●   Reliable
Unit Testing in Python

●   We will look at three test frameworks
    ●   unittest
    ●   py.test
    ●   nose
What are we looking for?

●   Ease of writing tests    ●   xUnit output support
●   Ease of running tests    ●   Test →Doc
●   Test autodiscovery       ●   Code coverage
●   Running specific tests   ●   Code profiling
●   Running failed tests     ●   Parallel testing
●   Setup & teardown         ●   Interactive debug
unittest
import unittest


class TestPalindrome(unittest.TestCase):
    def test_function_should_accept_palindromes(self):
        input = “noon”
        self.assertTrue(is_palindrome(input))
unittest features

+ Similar to standard unit testing frameworks in
other languages (jUnit, Nunit...)
+ Included in base python standard library
+ Best IDE support
+ Maximum adoption
unittest features

– Inflexible, cumbersome, unpythonic
– Requires lots of boilerplate code to write code
– No test autodiscovery
– No support for running specific tests
– Limited support for setup and teardown
– No support for advanced test features
py.test
def test_function_should_accept_palindromic_words():
    input = "noon"
    assert is_palindrome(input) == True
py.test features

+ Test autodiscovery
+ Easy to write and run tests
+ Supports most of the advanced features –
parallel testing, parametrized tests, compatibility
with unittest, coverage, interactive debug
+ Good support for extensions
py.test features

– Not standard
– Lack of IDE support
nose
def test_function_should_accept_palindromic_words():
    input = "noon"
    assert is_palindrome(input) == True
nose features

+ Compatible with unittest
+ Supports all advanced features
+ Works well with Django, Pylons, Turbogears
+ Excellent plugin support
+ Supported by some IDEs
+ Most popular among alternative test frameworks
nose features

– Not standard
Some interesting plugins

●   Code coverage – Shows you how well your unit
    tests covers the code
●   Profiling – Measures the time taken by
    functions when running the tests
●   Parallel testing – Runs tests in parallel to speed
    things up
Other Interesting Features

●   Generative tests – Runs the same test sequence
    with different combinations of input data
●   Interactive debug – Drops into the python
    debugger on test failure
How we use nose
..Scriptspaver.exe test_django
---> test_django
.............
Ran 1302 tests in 262.391s


OK
Destroying test database...
How we use nose
..Scriptspaver.exe test_django --database=sqlite3
--exclude=south
---> test_django
.............
Ran 1274 tests in 128.359s


OK
Destroying test database...
How we use nose
..Scriptspaver.exe test_django metrics --with-coverage
--cover-package=metrics
Name                            Stmts   Exec   Cover
----------------------------------------------------------
metrics                             0      0    100%
metrics.cumulative_calculator      34     34    100%
metrics.models                     39     37     94%   48-49
metrics.throughput                 13     13    100%
metrics.views                     100     91     91%   20-
22, 33-35, 46-48
TOTAL                             186    175     94%
Nose Plugins - Spec
Test → Doc


class TestIsPalindrome(self)
    def test_should_accept_palindromic_words
    def test_function_should_ignore_case
    def test_function_should_ignore_trailing_space


IsPalindrome
 - Should accept palindromic words
 - Should ignore case
 - Should ignore trailing space
Nose Plugins - Xunit

●   Provides test result output in the standard xUnit
    xml format
●   This format can be read and integrated into
    standard continuous integration systems
Summary

Not much to choose between py.test and nose
nose is currently more popular
Use unittest if standardisation is important

Contenu connexe

Tendances

JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkOnkar Deshpande
 
Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)Lars Thorup
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPTsuhasreddy1
 
automation testing benefits
automation testing benefitsautomation testing benefits
automation testing benefitsnazeer pasha
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)nedirtv
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated TestingLee Englestone
 
Test Automation Framework Designs
Test Automation Framework DesignsTest Automation Framework Designs
Test Automation Framework DesignsSauce Labs
 
Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Mohamed Taman
 
Unit testing framework
Unit testing frameworkUnit testing framework
Unit testing frameworkIgor Vavrish
 
UI Testing Automation
UI Testing AutomationUI Testing Automation
UI Testing AutomationAgileEngine
 
Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkArulalan T
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration TestingDavid Berliner
 

Tendances (20)

JUnit- A Unit Testing Framework
JUnit- A Unit Testing FrameworkJUnit- A Unit Testing Framework
JUnit- A Unit Testing Framework
 
Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)Test and Behaviour Driven Development (TDD/BDD)
Test and Behaviour Driven Development (TDD/BDD)
 
UNIT TESTING PPT
UNIT TESTING PPTUNIT TESTING PPT
UNIT TESTING PPT
 
automation testing benefits
automation testing benefitsautomation testing benefits
automation testing benefits
 
TDD (Test Driven Design)
TDD (Test Driven Design)TDD (Test Driven Design)
TDD (Test Driven Design)
 
Unit Tests And Automated Testing
Unit Tests And Automated TestingUnit Tests And Automated Testing
Unit Tests And Automated Testing
 
Test Automation Framework with BDD and Cucumber
Test Automation Framework with BDD and CucumberTest Automation Framework with BDD and Cucumber
Test Automation Framework with BDD and Cucumber
 
Unit test
Unit testUnit test
Unit test
 
Unit testing
Unit testingUnit testing
Unit testing
 
Test Automation Framework Designs
Test Automation Framework DesignsTest Automation Framework Designs
Test Automation Framework Designs
 
Junit
JunitJunit
Junit
 
Agile Testing by Example
Agile Testing by ExampleAgile Testing by Example
Agile Testing by Example
 
Unit Testing
Unit TestingUnit Testing
Unit Testing
 
Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.Unit testing & TDD concepts with best practice guidelines.
Unit testing & TDD concepts with best practice guidelines.
 
Unit testing framework
Unit testing frameworkUnit testing framework
Unit testing framework
 
Testing fundamentals
Testing fundamentalsTesting fundamentals
Testing fundamentals
 
UI Testing Automation
UI Testing AutomationUI Testing Automation
UI Testing Automation
 
Testing in-python-and-pytest-framework
Testing in-python-and-pytest-frameworkTesting in-python-and-pytest-framework
Testing in-python-and-pytest-framework
 
Unit and integration Testing
Unit and integration TestingUnit and integration Testing
Unit and integration Testing
 
TDD and BDD and ATDD
TDD and BDD and ATDDTDD and BDD and ATDD
TDD and BDD and ATDD
 

En vedette

Softwarequalitätssicherung mit Continuous Integration Tools
Softwarequalitätssicherung mit Continuous Integration ToolsSoftwarequalitätssicherung mit Continuous Integration Tools
Softwarequalitätssicherung mit Continuous Integration ToolsGFU Cyrus AG
 
Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...Timo Stollenwerk
 
Python Unit Test
Python Unit TestPython Unit Test
Python Unit TestDavid Xie
 
unittest in 5 minutes
unittest in 5 minutesunittest in 5 minutes
unittest in 5 minutesRay Toal
 
Cómo aplicar TDD. Almería 13/05/2014
Cómo aplicar TDD. Almería 13/05/2014Cómo aplicar TDD. Almería 13/05/2014
Cómo aplicar TDD. Almería 13/05/2014Javier_J
 
Las Claves del Desarrollo Dirigido por Pruebas (o TDD)
Las Claves del Desarrollo Dirigido por Pruebas (o TDD)Las Claves del Desarrollo Dirigido por Pruebas (o TDD)
Las Claves del Desarrollo Dirigido por Pruebas (o TDD)Javier_J
 
TDD y Python
TDD y PythonTDD y Python
TDD y PythonJavier_J
 
Kata Tenis. Enunciados y soluciones alternativas. Python Sevilla 30/11/2012
Kata Tenis. Enunciados y soluciones alternativas. Python Sevilla 30/11/2012Kata Tenis. Enunciados y soluciones alternativas. Python Sevilla 30/11/2012
Kata Tenis. Enunciados y soluciones alternativas. Python Sevilla 30/11/2012Javier_J
 
Kata Tenis Completa Paso a Paso. Python Sevilla 30/11/2012
Kata Tenis Completa Paso a Paso. Python Sevilla 30/11/2012Kata Tenis Completa Paso a Paso. Python Sevilla 30/11/2012
Kata Tenis Completa Paso a Paso. Python Sevilla 30/11/2012Javier_J
 
Clean Code Developer
Clean Code DeveloperClean Code Developer
Clean Code DeveloperGFU Cyrus AG
 
Unit Testing with Python
Unit Testing with PythonUnit Testing with Python
Unit Testing with PythonMicroPyramid .
 
Easy selenium test automation on python
Easy selenium test automation on pythonEasy selenium test automation on python
Easy selenium test automation on pythonMykhailo Poliarush
 
bubble sorting of an array in 8086 assembly language
bubble sorting of an array in 8086 assembly languagebubble sorting of an array in 8086 assembly language
bubble sorting of an array in 8086 assembly languageBilal Amjad
 

En vedette (20)

Mock testing mit Python
Mock testing mit PythonMock testing mit Python
Mock testing mit Python
 
Softwarequalitätssicherung mit Continuous Integration Tools
Softwarequalitätssicherung mit Continuous Integration ToolsSoftwarequalitätssicherung mit Continuous Integration Tools
Softwarequalitätssicherung mit Continuous Integration Tools
 
Python in Test automation
Python in Test automationPython in Test automation
Python in Test automation
 
Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...Python-nose: A unittest-based testing framework for Python that makes writing...
Python-nose: A unittest-based testing framework for Python that makes writing...
 
Python Unit Test
Python Unit TestPython Unit Test
Python Unit Test
 
unittest in 5 minutes
unittest in 5 minutesunittest in 5 minutes
unittest in 5 minutes
 
Presentation TDD in Python
Presentation TDD in PythonPresentation TDD in Python
Presentation TDD in Python
 
Cómo aplicar TDD. Almería 13/05/2014
Cómo aplicar TDD. Almería 13/05/2014Cómo aplicar TDD. Almería 13/05/2014
Cómo aplicar TDD. Almería 13/05/2014
 
Las Claves del Desarrollo Dirigido por Pruebas (o TDD)
Las Claves del Desarrollo Dirigido por Pruebas (o TDD)Las Claves del Desarrollo Dirigido por Pruebas (o TDD)
Las Claves del Desarrollo Dirigido por Pruebas (o TDD)
 
TDD y Python
TDD y PythonTDD y Python
TDD y Python
 
Java EE 5
Java EE 5Java EE 5
Java EE 5
 
Kata Tenis. Enunciados y soluciones alternativas. Python Sevilla 30/11/2012
Kata Tenis. Enunciados y soluciones alternativas. Python Sevilla 30/11/2012Kata Tenis. Enunciados y soluciones alternativas. Python Sevilla 30/11/2012
Kata Tenis. Enunciados y soluciones alternativas. Python Sevilla 30/11/2012
 
Kata Tenis Completa Paso a Paso. Python Sevilla 30/11/2012
Kata Tenis Completa Paso a Paso. Python Sevilla 30/11/2012Kata Tenis Completa Paso a Paso. Python Sevilla 30/11/2012
Kata Tenis Completa Paso a Paso. Python Sevilla 30/11/2012
 
Python unittest
Python unittestPython unittest
Python unittest
 
Clean Code Developer
Clean Code DeveloperClean Code Developer
Clean Code Developer
 
TDD com Python (Completo)
TDD com Python (Completo)TDD com Python (Completo)
TDD com Python (Completo)
 
Integration Testing in Python
Integration Testing in PythonIntegration Testing in Python
Integration Testing in Python
 
Unit Testing with Python
Unit Testing with PythonUnit Testing with Python
Unit Testing with Python
 
Easy selenium test automation on python
Easy selenium test automation on pythonEasy selenium test automation on python
Easy selenium test automation on python
 
bubble sorting of an array in 8086 assembly language
bubble sorting of an array in 8086 assembly languagebubble sorting of an array in 8086 assembly language
bubble sorting of an array in 8086 assembly language
 

Similaire à Test Driven Development With Python

Plone testingdzug tagung2010
Plone testingdzug tagung2010Plone testingdzug tagung2010
Plone testingdzug tagung2010Timo Stollenwerk
 
Tdd with python unittest for embedded c
Tdd with python unittest for embedded cTdd with python unittest for embedded c
Tdd with python unittest for embedded cBenux Wei
 
MT_01_unittest_python.pdf
MT_01_unittest_python.pdfMT_01_unittest_python.pdf
MT_01_unittest_python.pdfHans Jones
 
Django’s nasal passage
Django’s nasal passageDjango’s nasal passage
Django’s nasal passageErik Rose
 
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...GlobalLogic Ukraine
 
Token Testing Slides
Token  Testing SlidesToken  Testing Slides
Token Testing Slidesericholscher
 
Leveling Up With Unit Testing - LonghornPHP 2022
Leveling Up With Unit Testing - LonghornPHP 2022Leveling Up With Unit Testing - LonghornPHP 2022
Leveling Up With Unit Testing - LonghornPHP 2022Mark Niebergall
 
Testing in Python: doctest and unittest
Testing in Python: doctest and unittestTesting in Python: doctest and unittest
Testing in Python: doctest and unittestFariz Darari
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in pythonKarin Lagesen
 
Testing Django Applications
Testing Django ApplicationsTesting Django Applications
Testing Django ApplicationsHonza Král
 
How to write maintainable code without tests
How to write maintainable code without testsHow to write maintainable code without tests
How to write maintainable code without testsJuti Noppornpitak
 
Making the most of your Test Suite
Making the most of your Test SuiteMaking the most of your Test Suite
Making the most of your Test Suiteericholscher
 
Effective testing with pytest
Effective testing with pytestEffective testing with pytest
Effective testing with pytestHector Canto
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdflailoesakhan
 
Testing in Python: doctest and unittest (Updated)
Testing in Python: doctest and unittest (Updated)Testing in Python: doctest and unittest (Updated)
Testing in Python: doctest and unittest (Updated)Fariz Darari
 
Automation Testing theory notes.pptx
Automation Testing theory notes.pptxAutomation Testing theory notes.pptx
Automation Testing theory notes.pptxNileshBorkar12
 

Similaire à Test Driven Development With Python (20)

Plone testingdzug tagung2010
Plone testingdzug tagung2010Plone testingdzug tagung2010
Plone testingdzug tagung2010
 
Tdd with python unittest for embedded c
Tdd with python unittest for embedded cTdd with python unittest for embedded c
Tdd with python unittest for embedded c
 
MT_01_unittest_python.pdf
MT_01_unittest_python.pdfMT_01_unittest_python.pdf
MT_01_unittest_python.pdf
 
Django’s nasal passage
Django’s nasal passageDjango’s nasal passage
Django’s nasal passage
 
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
GlobalLogic Test Automation Online TechTalk “Test Driven Development as a Per...
 
Token Testing Slides
Token  Testing SlidesToken  Testing Slides
Token Testing Slides
 
Golang dot-testing-lite
Golang dot-testing-liteGolang dot-testing-lite
Golang dot-testing-lite
 
Leveling Up With Unit Testing - LonghornPHP 2022
Leveling Up With Unit Testing - LonghornPHP 2022Leveling Up With Unit Testing - LonghornPHP 2022
Leveling Up With Unit Testing - LonghornPHP 2022
 
report
reportreport
report
 
Testing in Python: doctest and unittest
Testing in Python: doctest and unittestTesting in Python: doctest and unittest
Testing in Python: doctest and unittest
 
Functions and modules in python
Functions and modules in pythonFunctions and modules in python
Functions and modules in python
 
Testing Django Applications
Testing Django ApplicationsTesting Django Applications
Testing Django Applications
 
How to write maintainable code without tests
How to write maintainable code without testsHow to write maintainable code without tests
How to write maintainable code without tests
 
Python ppt
Python pptPython ppt
Python ppt
 
Python basic
Python basicPython basic
Python basic
 
Making the most of your Test Suite
Making the most of your Test SuiteMaking the most of your Test Suite
Making the most of your Test Suite
 
Effective testing with pytest
Effective testing with pytestEffective testing with pytest
Effective testing with pytest
 
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdfProgFund_Lecture_4_Functions_and_Modules-1.pdf
ProgFund_Lecture_4_Functions_and_Modules-1.pdf
 
Testing in Python: doctest and unittest (Updated)
Testing in Python: doctest and unittest (Updated)Testing in Python: doctest and unittest (Updated)
Testing in Python: doctest and unittest (Updated)
 
Automation Testing theory notes.pptx
Automation Testing theory notes.pptxAutomation Testing theory notes.pptx
Automation Testing theory notes.pptx
 

Plus de Siddhi

Not all features are equal
Not all features are equalNot all features are equal
Not all features are equalSiddhi
 
The end of the backlog?
The end of the backlog?The end of the backlog?
The end of the backlog?Siddhi
 
Growth hacks
Growth hacksGrowth hacks
Growth hacksSiddhi
 
Kanban for Startups
Kanban for StartupsKanban for Startups
Kanban for StartupsSiddhi
 
Venture lab tech entrepreneurship market survey
Venture lab tech entrepreneurship market surveyVenture lab tech entrepreneurship market survey
Venture lab tech entrepreneurship market surveySiddhi
 
Technology Entrepreneurship: Assignment 2
Technology Entrepreneurship: Assignment 2Technology Entrepreneurship: Assignment 2
Technology Entrepreneurship: Assignment 2Siddhi
 
5 steps to better user engagement
5 steps to better user engagement5 steps to better user engagement
5 steps to better user engagementSiddhi
 
Bridging the gap between your Agile project organisation and the traditional ...
Bridging the gap between your Agile project organisation and the traditional ...Bridging the gap between your Agile project organisation and the traditional ...
Bridging the gap between your Agile project organisation and the traditional ...Siddhi
 
So you wanna build something? Now what?
So you wanna build something? Now what?So you wanna build something? Now what?
So you wanna build something? Now what?Siddhi
 
Agile in short projects
Agile in short projectsAgile in short projects
Agile in short projectsSiddhi
 
Continuous feedback
Continuous feedbackContinuous feedback
Continuous feedbackSiddhi
 
Organizational Dysfunctions - Agile to the Rescue
Organizational Dysfunctions - Agile to the RescueOrganizational Dysfunctions - Agile to the Rescue
Organizational Dysfunctions - Agile to the RescueSiddhi
 
Agile is not the easy way out
Agile is not the easy way outAgile is not the easy way out
Agile is not the easy way outSiddhi
 
The Three Amigos
The Three AmigosThe Three Amigos
The Three AmigosSiddhi
 
Visualisation & Self Organisation
Visualisation & Self OrganisationVisualisation & Self Organisation
Visualisation & Self OrganisationSiddhi
 
Portfolio Management - Figuring Out How to Say When and Why
Portfolio Management - Figuring Out How to Say When and WhyPortfolio Management - Figuring Out How to Say When and Why
Portfolio Management - Figuring Out How to Say When and WhySiddhi
 
Attention Middle Management Chickens
Attention Middle Management ChickensAttention Middle Management Chickens
Attention Middle Management ChickensSiddhi
 
Agile Project Outsourcing - Dealing with RFP and RFI
Agile Project Outsourcing - Dealing with RFP and RFIAgile Project Outsourcing - Dealing with RFP and RFI
Agile Project Outsourcing - Dealing with RFP and RFISiddhi
 
Migrating Legacy Code
Migrating Legacy CodeMigrating Legacy Code
Migrating Legacy CodeSiddhi
 
Big Bang Agile Roll-out
Big Bang Agile Roll-outBig Bang Agile Roll-out
Big Bang Agile Roll-outSiddhi
 

Plus de Siddhi (20)

Not all features are equal
Not all features are equalNot all features are equal
Not all features are equal
 
The end of the backlog?
The end of the backlog?The end of the backlog?
The end of the backlog?
 
Growth hacks
Growth hacksGrowth hacks
Growth hacks
 
Kanban for Startups
Kanban for StartupsKanban for Startups
Kanban for Startups
 
Venture lab tech entrepreneurship market survey
Venture lab tech entrepreneurship market surveyVenture lab tech entrepreneurship market survey
Venture lab tech entrepreneurship market survey
 
Technology Entrepreneurship: Assignment 2
Technology Entrepreneurship: Assignment 2Technology Entrepreneurship: Assignment 2
Technology Entrepreneurship: Assignment 2
 
5 steps to better user engagement
5 steps to better user engagement5 steps to better user engagement
5 steps to better user engagement
 
Bridging the gap between your Agile project organisation and the traditional ...
Bridging the gap between your Agile project organisation and the traditional ...Bridging the gap between your Agile project organisation and the traditional ...
Bridging the gap between your Agile project organisation and the traditional ...
 
So you wanna build something? Now what?
So you wanna build something? Now what?So you wanna build something? Now what?
So you wanna build something? Now what?
 
Agile in short projects
Agile in short projectsAgile in short projects
Agile in short projects
 
Continuous feedback
Continuous feedbackContinuous feedback
Continuous feedback
 
Organizational Dysfunctions - Agile to the Rescue
Organizational Dysfunctions - Agile to the RescueOrganizational Dysfunctions - Agile to the Rescue
Organizational Dysfunctions - Agile to the Rescue
 
Agile is not the easy way out
Agile is not the easy way outAgile is not the easy way out
Agile is not the easy way out
 
The Three Amigos
The Three AmigosThe Three Amigos
The Three Amigos
 
Visualisation & Self Organisation
Visualisation & Self OrganisationVisualisation & Self Organisation
Visualisation & Self Organisation
 
Portfolio Management - Figuring Out How to Say When and Why
Portfolio Management - Figuring Out How to Say When and WhyPortfolio Management - Figuring Out How to Say When and Why
Portfolio Management - Figuring Out How to Say When and Why
 
Attention Middle Management Chickens
Attention Middle Management ChickensAttention Middle Management Chickens
Attention Middle Management Chickens
 
Agile Project Outsourcing - Dealing with RFP and RFI
Agile Project Outsourcing - Dealing with RFP and RFIAgile Project Outsourcing - Dealing with RFP and RFI
Agile Project Outsourcing - Dealing with RFP and RFI
 
Migrating Legacy Code
Migrating Legacy CodeMigrating Legacy Code
Migrating Legacy Code
 
Big Bang Agile Roll-out
Big Bang Agile Roll-outBig Bang Agile Roll-out
Big Bang Agile Roll-out
 

Dernier

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Mark Simos
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionDilum Bandara
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxLoriGlavin3
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxLoriGlavin3
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr BaganFwdays
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESmohitsingh558521
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyAlfredo García Lavilla
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .Alan Dix
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxLoriGlavin3
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxLoriGlavin3
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024BookNet Canada
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfAlex Barbosa Coqueiro
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfLoriGlavin3
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxLoriGlavin3
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupFlorian Wilhelm
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLScyllaDB
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 

Dernier (20)

Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
Tampa BSides - Chef's Tour of Microsoft Security Adoption Framework (SAF)
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Advanced Computer Architecture – An Introduction
Advanced Computer Architecture – An IntroductionAdvanced Computer Architecture – An Introduction
Advanced Computer Architecture – An Introduction
 
The State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptxThe State of Passkeys with FIDO Alliance.pptx
The State of Passkeys with FIDO Alliance.pptx
 
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
Transcript: New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptxThe Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
The Role of FIDO in a Cyber Secure Netherlands: FIDO Paris Seminar.pptx
 
"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan"ML in Production",Oleksandr Bagan
"ML in Production",Oleksandr Bagan
 
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICESSALESFORCE EDUCATION CLOUD | FEXLE SERVICES
SALESFORCE EDUCATION CLOUD | FEXLE SERVICES
 
Commit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easyCommit 2024 - Secret Management made easy
Commit 2024 - Secret Management made easy
 
From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .From Family Reminiscence to Scholarly Archive .
From Family Reminiscence to Scholarly Archive .
 
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptxUse of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
Use of FIDO in the Payments and Identity Landscape: FIDO Paris Seminar.pptx
 
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptxMerck Moving Beyond Passwords: FIDO Paris Seminar.pptx
Merck Moving Beyond Passwords: FIDO Paris Seminar.pptx
 
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
New from BookNet Canada for 2024: BNC CataList - Tech Forum 2024
 
Unraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdfUnraveling Multimodality with Large Language Models.pdf
Unraveling Multimodality with Large Language Models.pdf
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Moving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdfMoving Beyond Passwords: FIDO Paris Seminar.pdf
Moving Beyond Passwords: FIDO Paris Seminar.pdf
 
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptxA Deep Dive on Passkeys: FIDO Paris Seminar.pptx
A Deep Dive on Passkeys: FIDO Paris Seminar.pptx
 
Streamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project SetupStreamlining Python Development: A Guide to a Modern Project Setup
Streamlining Python Development: A Guide to a Modern Project Setup
 
Developer Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQLDeveloper Data Modeling Mistakes: From Postgres to NoSQL
Developer Data Modeling Mistakes: From Postgres to NoSQL
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 

Test Driven Development With Python

  • 1. Test Driven Development in Python Siddharta Govindaraj siddharta@silverstripesoftware.com
  • 2. What is Test Driven Development (TDD)? http://www.flickr.com/photos/johnsyweb/3051647719/
  • 3. Red, Green, Refactor ● First write a test ● Write code to pass the test ● Clean up the code ● Repeat
  • 4. TDD Example Write a function to check whether a given input string is a palindrome
  • 6. tests.py from code import is_palindrome def test_function_should_accept_palindromic_words(): input = "noon" assert is_palindrome(input) == True
  • 8. code.py def is_palindrome(input_str): return input_str == input_str[::-1]
  • 10. tests.py def test_function_should_ignore_case(): input = "Noon" assert is_palindrome(input) == True
  • 12. code.py def is_palindrome(input_str): input_clean = input_str.lower() return input_clean == input_clean[::-1]
  • 14. tests.py def test_function_should_ignore_trailing_space(): input = "Noon " assert is_palindrome(input) == True
  • 15. code.py def is_palindrome(input_str): input_clean = input_str.strip().lower() return input_clean == input_clean[::-1]
  • 16. tests.py def test_function_should_ignore_spaces_in_text(): input = "ab raca carba" assert is_palindrome(input) == True
  • 17. code.py def is_palindrome(input_str): input_stripped = input_str.replace(" ", "") input_clean = input_stripped.lower() return input_clean == input_clean[::-1]
  • 18. tests.py def test_function_should_handle_combined_characters(): input = u"u0bb4u0bbfuu0b95u0bb4u0bbf" assert is_palindrome(input) == True (Input is ழ கழ )
  • 19. Reversing unicode strings The String: ழ கழ Characters: ழ + ி + க + ழ + ி Wrong: ி + ழ + க + ி + ழ Right: ழ + ி + க + ழ + ி
  • 20. # naïve implementation to pass the test def is_palindrome(input_str): def reverse_string(input_str): def is_combining_char(char): chars = [u"u0bcd"] return char in chars reversed_chars = [] for char in input_str: if is_combining_char(char): reversed_chars.insert(1, char) else: reversed_chars.insert(0, char) return "".join(reversed_chars) input_stripped = input_str.replace(" ", "") input_clean = input_stripped.lower() reversed_string = reverse_string(input_clean) return input_clean == reversed_string
  • 21. And so it continues... ● Turns out reversing a string is quite complex when unicode scripts come into the picture ● Many different cases to consider ● Unit tests can validate the complex code logic and check for regression errors
  • 22. Why is unit testing important? ● Quality ● Regression ● Safety Net ● Integration with build and CI tools ● Documentation
  • 23. Attributes of good tests ● Fast ● Clear ● Isolated ● Reliable
  • 24. Unit Testing in Python ● We will look at three test frameworks ● unittest ● py.test ● nose
  • 25. What are we looking for? ● Ease of writing tests ● xUnit output support ● Ease of running tests ● Test →Doc ● Test autodiscovery ● Code coverage ● Running specific tests ● Code profiling ● Running failed tests ● Parallel testing ● Setup & teardown ● Interactive debug
  • 26. unittest import unittest class TestPalindrome(unittest.TestCase): def test_function_should_accept_palindromes(self): input = “noon” self.assertTrue(is_palindrome(input))
  • 27. unittest features + Similar to standard unit testing frameworks in other languages (jUnit, Nunit...) + Included in base python standard library + Best IDE support + Maximum adoption
  • 28. unittest features – Inflexible, cumbersome, unpythonic – Requires lots of boilerplate code to write code – No test autodiscovery – No support for running specific tests – Limited support for setup and teardown – No support for advanced test features
  • 29. py.test def test_function_should_accept_palindromic_words(): input = "noon" assert is_palindrome(input) == True
  • 30. py.test features + Test autodiscovery + Easy to write and run tests + Supports most of the advanced features – parallel testing, parametrized tests, compatibility with unittest, coverage, interactive debug + Good support for extensions
  • 31. py.test features – Not standard – Lack of IDE support
  • 32. nose def test_function_should_accept_palindromic_words(): input = "noon" assert is_palindrome(input) == True
  • 33. nose features + Compatible with unittest + Supports all advanced features + Works well with Django, Pylons, Turbogears + Excellent plugin support + Supported by some IDEs + Most popular among alternative test frameworks
  • 35. Some interesting plugins ● Code coverage – Shows you how well your unit tests covers the code ● Profiling – Measures the time taken by functions when running the tests ● Parallel testing – Runs tests in parallel to speed things up
  • 36. Other Interesting Features ● Generative tests – Runs the same test sequence with different combinations of input data ● Interactive debug – Drops into the python debugger on test failure
  • 37. How we use nose ..Scriptspaver.exe test_django ---> test_django ............. Ran 1302 tests in 262.391s OK Destroying test database...
  • 38. How we use nose ..Scriptspaver.exe test_django --database=sqlite3 --exclude=south ---> test_django ............. Ran 1274 tests in 128.359s OK Destroying test database...
  • 39. How we use nose ..Scriptspaver.exe test_django metrics --with-coverage --cover-package=metrics Name Stmts Exec Cover ---------------------------------------------------------- metrics 0 0 100% metrics.cumulative_calculator 34 34 100% metrics.models 39 37 94% 48-49 metrics.throughput 13 13 100% metrics.views 100 91 91% 20- 22, 33-35, 46-48 TOTAL 186 175 94%
  • 40. Nose Plugins - Spec Test → Doc class TestIsPalindrome(self) def test_should_accept_palindromic_words def test_function_should_ignore_case def test_function_should_ignore_trailing_space IsPalindrome - Should accept palindromic words - Should ignore case - Should ignore trailing space
  • 41. Nose Plugins - Xunit ● Provides test result output in the standard xUnit xml format ● This format can be read and integrated into standard continuous integration systems
  • 42. Summary Not much to choose between py.test and nose nose is currently more popular Use unittest if standardisation is important