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

TDD in Python with pytest

TDD in Python with pytest

Leonardo Giordani

February 16, 2024
Tweet

More Decks by Leonardo Giordani

Other Decks in Technology

Transcript

  1. TDD in Python - Leonardo Giordani - @tw_lgiordani - @thedigicat

    Introduction to TDD (10 mins) Hands-on TDD (100 mins) Links and resources (10 mins) Agenda
  2. 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
  3. 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.
  4. 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”)
  5. 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.
  6. 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?
  7. 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
  8. 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
  9. TDD in Python - Leonardo Giordani - @tw_lgiordani - @thedigicat

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

    DOCUMENTATION At least there are examples
  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/ GitHub actions https://docs.github.com/en/actions Tools and libraries
  12. 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
  13. TDD in Python - Leonardo Giordani - @tw_lgiordani - @thedigicat

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