logo

... test: structure



Notes

Your testing code should be structured. This makes it easy to pinpoint where the problem is when there's an error. After making the test_verbs folder we've also created two files. There's the test_keep.py file.

from clumper import Clumper 

def test_can_collect_all():
    data = [{'i': i} for i in range(10)]
    c = Clumper(data)
    assert len(c.keep(lambda d: d['i'] < 20).collect()) == 10


def test_can_collect_half():
    data = [{'i': i} for i in range(10)]
    c = Clumper(data)
    assert len(c.keep(lambda d: d['i'] < 5).collect()) == 5


def test_can_collect_none():
    data = [{'i': i} for i in range(10)]
    c = Clumper(data)
    assert len(c.keep(lambda d: d['i'] < 0).collect()) == 0

And there's the test_headtail.py file.

from clumper import Clumper 

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


def test_can_collect_tail():
    data = [{'i': i} for i in range(10)]
    c = Clumper(data).tail(1)
    collected = c.collect() 
    assert collected[0]['i'] == 9


def test_can_collect_head():
    data = [{'i': i} for i in range(10)]
    c = Clumper(data).head(1)
    collected = c.collect() 
    assert collected[0]['i'] == 0

In later videos we'll refactor this code, but it serves as a starting point. If nothing else, it's nice to have a separation of concerns here. You can confirm everything runs green via pytest on the command line.

pytest

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



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