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

We can make our test even more concise by adding a conftest.py file in our tests directory. In it we can define a fixture, which is a function can supply our tests with data for greater re-usability. Here's the fixture from the video:

from clumper import Clumper
import pytest


@pytest.fixture(scope="module")
def base_clumper():
    data = [
        {"data": [i for _ in range(2)], "i": i, "c": c}
        for i, c in enumerate("abcdefghijklmnopqrstuvwxyz")
    ]
    return Clumper(data)

This will allow us to refactor this test:

def test_headtail_size():
    data = [{'i': i} for i in range(10)]
    c = Clumper(data)
    assert len(c.head(10)) == 10
    assert len(c.tail(10)) == 10
    assert len(c.head(5)) == 5
    assert len(c.tail(5)) == 5

Into this test:

def test_headtail_size(base_clumper):
    assert len(base_clumper.head(10)) == 10
    assert len(base_clumper.tail(10)) == 10
    assert len(base_clumper.head(5)) == 5
    assert len(base_clumper.tail(5)) == 5

This is a whole lot better. But we're not done yet!


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.