Keyboard shortcuts

Press or to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

Python Pair Programming and TDD

Pair Programming and TDD

Slides

Parts of the session

  • Introduction to Testing in Python. (10 min)
  • Pair programming a project using TDD.(80 min)
  • Switching pairs to explain the solution to each other. (30 min)
  • Retrospective. (30 min)

Goals

  • Improve our way of software development.
  • Experiment with TDD.
  • Experiment with Pair Programming.

Module to test


def div(x, y):
    return x / y

def add(x, y):
    return x * y

Test the module

import mymath

def test_div():
    assert mymath.div(6, 3)  == 2
    assert mymath.div(42, 2) == 21
 
def test_add():
    assert mymath.add(2, 2)  == 4
    assert mymath.add(0, 0) == 0

Running Pytest

pytest test_mymath.py
========= test session starts ==========
platform darwin -- Python 3.6.3, pytest-3.3.0, py-1.5.2, pluggy-0.6.0
rootdir: ../python-pair-programming-and-tdd-workshop/test, inifile:
plugins: cov-2.5.1
collected 2 items

test_mymath.py ..                                    [100%]

======= 2 passed in 0.02 seconds =======

Test the module even more

import mymath

def test_div():
    assert mymath.div(6, 3)  == 2
    assert mymath.div(42, 2) == 21
 
def test_add():
    assert mymath.add(2, 2)  == 4
    assert mymath.add(0, 0) == 0
    assert mymath.add(-1, 1) == 0
    assert mymath.add(19, 23) == 42

Running Pytest

pytest test_mymath_more.py
========= test session starts ==========
platform darwin -- Python 3.6.3, pytest-3.3.0, py-1.5.2, pluggy-0.6.0
rootdir: ../python-pair-programming-and-tdd-workshop/test, inifile:
plugins: cov-2.5.1
collected 2 items

test_mymath_more.py .F                               [100%]

=============== FAILURES ===============
_______________ test_add _______________

    def test_add():
        assert mymath.add(2, 2)  == 4
        assert mymath.add(0, 0) == 0
>       assert mymath.add(-1, 1) == 0
E       assert -1 == 0
E        +  where -1 = <function add at 0x102386400>(-1, 1)
E        +    where <function add at 0x102386400> = mymath.add

test_mymath_more.py:10: AssertionError
====== 1 failed, 1 passed in 0.11 seconds ======

Pytest Coverage

pip install pytest-cov
pytest --cov=my --cov-report html --cov-branch
Open htmlcov/index.html

Learning Pytest

TDD - Test Driven Development

  • Write test first. It fails.
  • Implement code. Test passes.
  • Run test with coverage and check if there are cases that you have not covered yet.
  • Git commit

Why in pairs?

  • Learn from each other.
  • Learn how to express our ideas.
  • Continuous peer review.
  • Better solutions.
  • Less bugs.
  • You already know how to program alone. Let's try something else now.

Pair Programming

  • There is no single right way to do it. Experiment!
  • Verbalize everything
  • Driver - Navigator
  • Driver - Navigator - Observer

Llewellyn's Strong Style Pairing

Llewellyn's Strong Style Pairing: Driver

  • "Trust your navigator"
  • "Become comfortable working with incomplete understanding"
  • "What if I have an idea I want to implement?" - switch!

Llewellyn's Strong Style Pairing: Navigator

  • "Give the next instruction to the driver the instant they are ready to implement it."
  • "Talk in the highest level of abstraction the driver can understand."

Tic-tac-toe

tic-tac-toe.png

Tic-tac-toe - hints

  • Decide on a representation of the board.
  • Allow the computer to recognize when someone won.
  • Report invalid boards?
  • Allow two humans to play.
  • Make the computer play.
  • Make the system play against itself.
  • Watch War Games.

Retrospective