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

TDD in Python with pytest

TDD in Python with pytest

Slides for my workshop "TDD in Python with pytest"

Presented at EuroPython, PyCon UK, PyLadies London, Pycon IE, and Pycon IT

Leonardo Giordani

July 03, 2018
Tweet

More Decks by Leonardo Giordani

Other Decks in Programming

Transcript

  1. TDD in Python
    with pytest
    Leonardo Giordani - @tw_giordani - thedigitalcatonline.com

    View Slide

  2. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    About Me
    Born in 1977, I started coding in April 1987 on a Sinclair ZX
    Spectrum. I then moved to MS-DOS PCs and in 1996 I started using
    Linux and became interested in operating system internals. I love
    architectures, algorithms, mathematics and cryptography.
    I currently work as an infrastructure engineer and Python developer.
    From 2013 I blog some technical thoughts at
    www.thedigitalcatonline.com
    In 2018 I published the free book “Clean Architectures in Python”
    http://bit.ly/getpycabook

    View Slide

  3. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    BE REasonable

    View Slide

  4. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    Introduction to tdd
    Hands-ON: BASIC TDD
    AGENDA

    View Slide

  5. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    What you are going
    to learn today
    What you can learn in
    the next years

    View Slide

  6. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    What is TDD?

    View Slide

  7. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    It is not a tool
    It is not a language
    It is not a book

    View Slide

  8. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    TDD is a mindset

    View Slide

  9. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    We are working too slowly
    , we
    solved too few issues last
    month!
    Can you increase the
    productivity of the developers?
    How many issues did we solve?
    Not enough!
    How many issues do you want us
    to solve?
    More!

    View Slide

  10. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    Last month We solved 10 issues,
    and we need to solve at least 30
    of them every month.
    Can you do something?
    Well, since every developer
    works on average one week on
    an issue, and we have 3
    developers we could bla bla...
    AND PERHAPS BLA BLA...
    Nice IDEA! Let’s TRY IT!
    I’ll keep an eye on the issue
    counter. Thanks.

    View Slide

  11. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    Test first
    code later

    View Slide

  12. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    Write
    Minimal
    code

    View Slide

  13. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    1. pick ONE bug/missing feature
    2. Write a test THAT FAILS
    3. write the minimal code that
    passes the test
    4. REFACTOR the code
    TDD Workflow

    View Slide

  14. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    if the test you wrote doesn’t fail
    ask yourself
    “Is this test useful?”
    (which doesn’t mean “the test is useless”, it means “ASK YOURSELF”)

    View Slide

  15. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    Project Description
    The goal is to write a class Calc that performs calculations: addition,
    subtraction, multiplication, and division.
    Addition and multiplication shall accept multiple arguments.
    Division shall return a float and division by zero shall return the string "inf".
    Multiplication by zero must raise a ValueError exception.
    The class will also provide a function to compute the average of an iterable.
    This function gets two optional upper and lower thresholds to remove
    outliers.

    View Slide

  16. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    Project Description
    Tests for the average function
    1. Average of an iterable (list, tuple)
    2. Upper threshold (if > U don’t use it)
    3. Lower threshold (if < L don’t use it)
    4. Check that thresholds are not included
    5. Average of an empty iterable
    6. Average of an empty iterable (after thresholds)
    7. Thresholds applied to an empty iterable
    8. Argument is not iterable

    View Slide

  17. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    COVERAGE
    code is there for a reason

    View Slide

  18. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    REGRESSIONS
    Past tests are always run

    View Slide

  19. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    DOCS
    There are examples, at least

    View Slide

  20. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    REFACTORING
    Never without tests

    View Slide

  21. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    pytest
    https://docs.pytest.org
    unittest.mock
    https://docs.python.org/3/library/unittest.mock.html
    tox
    https://tox.readthedocs.io/
    travis.ci
    https://travis.ci
    Tools and Libraries

    View Slide

  22. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    http://www.thedigitalcatonline.com/categories/tdd/
    I'm too young to die
    Useful pytest command line options
    A simple example of Python OOP development (with TDD) (2 posts)
    Refactoring with tests in Python: a practical example
    Hurt me plenty
    Python Mocks: a gentle introduction (2 posts)
    Nightmare!
    A game of tokens: write an interpreter in Python with TDD (4 posts and counting)
    Clean architectures in Python: a step-by-step example
    Things I wrote On TDD

    View Slide

  23. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    http://bit.ly/getpycabook
    My First Book
    9800 readers
    so far
    What is a good software architecture? Why
    should we bother structuring the code and
    spending time testing it? If you like spending
    hours debugging your programs or staying
    late at the office to recover from a buggy
    deploy in production this book is definitely
    NOT for you!

    View Slide

  24. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT
    THANK you
    Slides at https://speakerdeck.com/lgiordani

    View Slide