[Tutor] Newbie & Unittest ...

Damon Timm damontimm at gmail.com
Fri May 7 20:41:19 CEST 2010


Hello again everyone - and thanks for your responses.  Adding the
unittest method message was something I didn't realize I could do!

On Thu, May 6, 2010 at 10:20 PM, Steven D'Aprano <steve at pearwood.info> wrote:
> With respect to Lie, dynamically adding methods is an advanced technique
> that is overkill for what you seem to be doing, and the code he gave
> you can't work without major modification.

I think you make a good argument for simple testing ... and I already
fell victim to "It's working great!  My tests pass!" when in fact the
test wasn't working at all!

Here is what I ended up doing, and it (currently) runs 52 tests.

I'm not sure if it is worth the trade-off, but I think it saved me
some typing (and makes it easy to add another file or tag key/value
pair).

#!/usr/bin/env python
''' unit tests for tagging.py '''

import unittest

from mlc import filetypes

TAG_VALUES = (
    ('title', 'Christmas Waltz'),
    ('artist', 'Damon Timm'),
    ('album', 'Homemade'),
    ('albumartist', 'Damon Timm'),
    ('compilation', False ),
    ('composer', 'Damon Timm'),
    ('date', '2005'),
    ('description', 'For more music, visit: damonjustisntfunny.com'),
    ('discnumber', 1),
    ('disctotal', 1),
    ('genre', 'Folk'),
    ('tracknumber', 1),
    ('tracktotal', 10),
)
FILES = (
    filetypes.FLACFile('data/lossless/01 - Christmas Waltz.flac'),
    filetypes.MP3File('data/lossy/04 - Christmas Waltz (MP3-79).mp3'),
    filetypes.OGGFile('data/lossy/01 - Christmas Waltz (OGG-77).ogg'),
    filetypes.MP4File('data/lossy/06 - Christmas Waltz (M4A-64).m4a'),
)

class TestTagOutput(unittest.TestCase):
    pass

def add_assert_equal(cls, test_name, value1, value2):
    new_test = lambda self: self.assertEqual(value1, value2)
    new_test.__doc__ = test_name
    setattr(cls, test_name, new_test)

for file in FILES:
    for key, value in TAG_VALUES:
        test_name = 'test_' + file.exts[0] + '_' + key # test_ext_key
        add_assert_equal(TestFileTags, test_name, file.tags[key], value)

if __name__ == '__main__':
    unittest.main()


More information about the Tutor mailing list