test logo test: conftest

1 2 3 4 5 6 7 8 9

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

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!