test logo test: learn

1 2 3 4 5 6 7 8 9

The refactored test in our video looks like this:

import pytest

@pytest.mark.parametrize("n,expected", [(0, 0), (5, 5), (10, 10), (26, 26), (1000, 26)])
def test_headtail_size(base_clumper, n, expected):
    assert len(base_clumper.head(n)) == expected
    assert len(base_clumper.tail(n)) == expected

It totally fails. You may be able to see why when you look at the code that was in our Clumper class.

class Clumper:
    def __init__(self, blob):
        self.blob = blob

    def __len__(self):
        return len(self.blob)

    def keep(self, *funcs):
        data = self.blob
        for func in funcs:
            data = [d for d in data if func(d)]
        return Clumper(data)

    def head(self, n):
        return Clumper([self.blob[i] for i in range(n)])

    def tail(self, n):
        return Clumper([self.blob[-i] for i in range(1, n + 1)])

If you're interested in seeing how we refactored this code you can check the code on the live github repository. You can also find a refactored set of tests there.