Slide 1

Slide 1 text

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

Slide 2

Slide 2 text

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

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

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

Slide 6

Slide 6 text

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

Slide 7

Slide 7 text

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

Slide 8

Slide 8 text

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

Slide 9

Slide 9 text

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!

Slide 10

Slide 10 text

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.

Slide 11

Slide 11 text

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

Slide 12

Slide 12 text

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

Slide 13

Slide 13 text

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

Slide 14

Slide 14 text

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”)

Slide 15

Slide 15 text

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.

Slide 16

Slide 16 text

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

Slide 17

Slide 17 text

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

Slide 18

Slide 18 text

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

Slide 19

Slide 19 text

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

Slide 20

Slide 20 text

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

Slide 21

Slide 21 text

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

Slide 22

Slide 22 text

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

Slide 23

Slide 23 text

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!

Slide 24

Slide 24 text

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