vendredi 29 mai 2015

UnitTesting a function that reads a text file and returns a dict

I want to unit test this function. It opens a file from disk, but I would like the unit test to not rely on external data, and be totally decoupled from the file system.

def instrument_map(map_filename='Instruments.txt'):
    """Maps instrument prefix to filename"""
    with open(map_filename) as f:
        return dict(line.rstrip().split(' ', 1) for line in f)

I've read about mocking and StringIO, but are not able figure out how to go about implementing either of these. Which would be the better approach, and where to start?

The contents of Instruments.txt looks like this:

00 Score
01 SopranoCn
02 SoloCn

Aucun commentaire:

Enregistrer un commentaire