TDD in Python with pytest and mock Leonardo Giordani - @lgiordani - - - PowerPoint PPT Presentation

tdd in python
SMART_READER_LITE
LIVE PREVIEW

TDD in Python with pytest and mock Leonardo Giordani - @lgiordani - - - PowerPoint PPT Presentation

TDD in Python with pytest and mock Leonardo Giordani - @lgiordani - thedigitalcatonline.com About Me Born in 1977 with Star Wars, bash, Apple ][, BSD, finger, Zork, Galaxy Express 999, Little Pollon, Dire Straits, The Police, Rumours, The


slide-1
SLIDE 1

TDD in Python

with pytest and mock

Leonardo Giordani - @lgiordani - thedigitalcatonline.com

slide-2
SLIDE 2

TDD in Python - Leonardo Giordani - @lgiordani

About Me

Born in 1977 with Star Wars, bash, Apple ][, BSD, finger, Zork, Galaxy Express 999, Little Pollon, Dire Straits, The Police, Rumours, The Silmarillion, Squad Leader. Interested in operating systems and computer languages, photography, fantasy and science fiction, video- and boardgames, guitar playing, climbing, horseback riding, Aikido, fencing, rollerskating, drawing, painting, bookbinding. I programmed in Z80 and x86 Assembly, GW-Basic, Logo, Borland Turbo Pascal, Prolog, C, C++, PHP, Lisp, Ada, Objective-C, bash, Python, Erlang, Clojure, Scala, JavaScript. I love mathematics and cryptography.

tl; dr

me = nerd + coder

slide-3
SLIDE 3

TDD in Python - Leonardo Giordani - @lgiordani

AMQP - architectures - C - Clojure - compilers - concurrent programming - C++ - decorators - Django - Erlang - functional programming - generators - Git - metaclasses - metaprogramming - Notebook - OOP - operating systems - Python - Python2 - Python3 - RabbitMQ - Scala - TDD - testing - versioning

thedigitalcatonline.com

@thedigicat

slide-4
SLIDE 4

TDD in Python - Leonardo Giordani - @lgiordani

Introduction Part 1 - Write a library in TDD Part 2 - Mocks and complex tests Part 3 - Refactoring and legacy code

AGENDA

slide-5
SLIDE 5

TDD in Python - Leonardo Giordani - @lgiordani

What you are going to learn today What you can learn in the next years

slide-6
SLIDE 6

TDD in Python - Leonardo Giordani - @lgiordani

What is TDD?

slide-7
SLIDE 7

TDD in Python - Leonardo Giordani - @lgiordani

It is not a tool It is not a language It is not a book It is a mindset

slide-8
SLIDE 8

TDD in Python - Leonardo Giordani - @lgiordani

We a r e wor k i n g t

  • sl
  • wl

y , we sol v e d t

  • f

e w i ssu e s l a st mon t h ! C a n y

  • u

i n c r e a se t h e p r

  • d

u c t i v i t y

  • f

t h e d e v e l

  • p

e r s? H

  • w

ma n y i ssu e s d i d we sol v e ? N

  • t

e n

  • u

gh ! H

  • w

ma n y i ssu e s d

  • y
  • u

w a n t u s t

  • sol

v e ? Mor e !

slide-9
SLIDE 9

TDD in Python - Leonardo Giordani - @lgiordani

L a st mon t h We sol v e d 1 i ssu e s, a n d we n e e d t

  • sol

v e a t l e a st 3

  • f

t h e m e v e r y mon t h . C a n y

  • u

d

  • some

t h i n g? We l l , si n c e e v e r y d e v e l

  • p

e r wor k s

  • n

a v e r a ge

  • n

e we e k

  • n

a n i ssu e , a n d we h a v e 3 d e v e l

  • p

e r s we c

  • u

l d b l a b l a . . . A N D P E R H A P S B L A B L A . . . N i c e I D E A ! L e t ’ s T R Y I T ! I ’ l l k e e p a n e y e

  • n

t h e i ssu e c

  • u

n t e r . T h a n k s.

slide-10
SLIDE 10

TDD in Python - Leonardo Giordani - @lgiordani

Test first code later

slide-11
SLIDE 11

TDD in Python - Leonardo Giordani - @lgiordani

COVERAGE

every line of code has a reason

slide-12
SLIDE 12

TDD in Python - Leonardo Giordani - @lgiordani

REGRESSIONS

Past tests are always run

slide-13
SLIDE 13

TDD in Python - Leonardo Giordani - @lgiordani

DOCS

There are examples, at least

slide-14
SLIDE 14

TDD in Python - Leonardo Giordani - @lgiordani

REFACTORING

Never without tests

slide-15
SLIDE 15

TDD in Python - Leonardo Giordani - @lgiordani

pick a bug/missing feature Write a test write code that passes the test

TDD Workflow

slide-16
SLIDE 16

TDD in Python - Leonardo Giordani - @lgiordani

if the test you wrote doesn’t fail ask yourself “Is this test useful?”

(which doesn’t mean “the test is useless”, it means “double check it”)

slide-17
SLIDE 17

TDD in Python - Leonardo Giordani - @lgiordani

WRITE CODE THAT WORKS

slide-18
SLIDE 18

TDD in Python - Leonardo Giordani - @lgiordani

BE REasonable

slide-19
SLIDE 19

TDD in Python - Leonardo Giordani - @lgiordani

STRUCTURE OF THE repository

master mocks refactoring my-tdd my-mocks my-refactoring

git checkout master git checkout -b my-tdd git checkout -t origin/mocks

slide-20
SLIDE 20

TDD in Python - Leonardo Giordani - @lgiordani

pytest

https://docs.pytest.org

Where to go now

unittest.mock

https://docs.python.org/3/library/unittest.mock.html

tox

https://tox.readthedocs.io/

travis.ci

https://travis.ci

slide-21
SLIDE 21

TDD in Python - Leonardo Giordani - @lgiordani

AMQP - architectures - C - Clojure - compilers - concurrent programming - C++ - decorators - Django - Erlang - functional programming - generators - Git - metaclasses - metaprogramming - Notebook - OOP - operating systems - Python - Python2 - Python3 - RabbitMQ - Scala - TDD - testing - versioning

thedigitalcatonline.com

@thedigicat

THANK you