Slide 1

Slide 1 text

Leonardo Giordani - @thedigicat – www.thedigitalcat.academy Test-Driven Development in Python with pytest

Slide 2

Slide 2 text

TDD in Python - Leonardo Giordani - @tw_lgiordani - @thedigicat Introduction to TDD (10 mins) Hands-on TDD (100 mins) Links and resources (10 mins) Agenda

Slide 3

Slide 3 text

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

Slide 4

Slide 4 text

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

Slide 5

Slide 5 text

TDD in Python - Leonardo Giordani - @tw_lgiordani - @thedigicat Test first Code later

Slide 6

Slide 6 text

TDD in Python - Leonardo Giordani - @tw_lgiordani - @thedigicat Pick one bug/missing feature Write a test that fails Write enough code to pass the test Refactor the code TDD workflow

Slide 7

Slide 7 text

TDD in Python - Leonardo Giordani - @tw_lgiordani - @thedigicat Pick one bug/missing feature Write a test that fails Write enough code to pass the test Refactor the code Split the problem into small manageable units of work.

Slide 8

Slide 8 text

TDD in Python - Leonardo Giordani - @tw_lgiordani - @thedigicat Pick one bug/missing feature Write a test that fails Write enough code to pass the test Refactor the code 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 9

Slide 9 text

TDD in Python - Leonardo Giordani - @tw_lgiordani - @thedigicat Pick one bug/missing feature Write a test that fails Write enough code to pass the test Refactor the code Your goal is to pass the test. More code = more (potential) bugs + more maintenance.

Slide 10

Slide 10 text

TDD in Python - Leonardo Giordani - @tw_lgiordani - @thedigicat Pick one bug/missing feature Write a test that fails Write enough code to pass the test Refactor the code NEVER refactor without tests. How can you be sure the refactored code still works?

Slide 11

Slide 11 text

TDD in Python - Leonardo Giordani - @tw_lgiordani - @thedigicat Be reasonable

Slide 12

Slide 12 text

TDD in Python - Leonardo Giordani - @tw_lgiordani - @thedigicat The goal of the project is to write a class SimpleCalculator that performs calculations: addition, subtraction, multiplication, and division. Addition and multiplication shall accept multiple arguments. Division shall return a float value, 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 like a list. This function gets two optional upper and lower thresholds and should remove from the computation the values that fall outside these boundaries. Project requirements

Slide 13

Slide 13 text

TDD in Python - Leonardo Giordani - @tw_lgiordani - @thedigicat Tests for the average function Average of an iterable (list, tuple) Upper threshold (if > U don’t use it) Lower threshold (if < L don’t use it) Check that thresholds are not included Average of an empty iterable Average of an empty iterable (after thresholds) Thresholds applied to an empty iterable Argument is not iterable Project requirements

Slide 14

Slide 14 text

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

Slide 15

Slide 15 text

TDD in Python - Leonardo Giordani - @tw_lgiordani - @thedigicat REGRESSIONS Old tests are always run

Slide 16

Slide 16 text

TDD in Python - Leonardo Giordani - @tw_lgiordani - @thedigicat DOCUMENTATION At least there are examples

Slide 17

Slide 17 text

TDD in Python - Leonardo Giordani - @tw_lgiordani - @thedigicat REFACTORING Never do it without tests

Slide 18

Slide 18 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/ GitHub actions https://docs.github.com/en/actions Tools and libraries

Slide 19

Slide 19 text

TDD in Python - Leonardo Giordani - @tw_lgiordani - @thedigicat https://www.thedigitalcatonline.com/categories/tdd/ I'm too young to die TDD in Python with pytest (5 posts) Useful pytest command line options 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 Posts I wrote on TDD

Slide 20

Slide 20 text

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