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 - 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
  2. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT

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

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

    It is not a tool It is not a language It is not a book
  5. 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!
  6. 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.
  7. 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
  8. 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”)
  9. 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.
  10. 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
  11. 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
  12. 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
  13. 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!
  14. TDD in Python - Leonardo Giordani - @tw_lgiordani - @THEdIGicAT

    THANK you Slides at https://speakerdeck.com/lgiordani