logo

... test.


We've got a base project going for us, which is nice. But let's add some proper unit tests as well as some code formatters! Note; you can find the live repository for this project here.


Notes

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 here. You can also find a refactored set of tests here.


Feedback? See an issue? Something unclear? Feel free to mention it here.

If you want to be kept up to date, consider getting the newsletter.