Ooh! Wait! I found another method that is similar in style and appears to work ...
class TestFileTags(unittest.TestCase): pass for test_name, file, key, value in list_of_tests: def test_func(self): self.assertEqual(file.tags[key], value) setattr(TestFileTags, test_name, test_func) I'm not sure if it is the *best* or *right* way to do it, but it does the trick! Damon On Thu, May 6, 2010 at 1:53 PM, Damon Timm <damont...@gmail.com> wrote: > Hi Lie - > > Thanks for that idea -- I tried it but am getting an error. I read a > little about the __dict__ feature but couldn't figure it. I am going > to keep searching around for how to dynamically add methods to a class > ... here is the error and then the code. > > Thanks. > > # ERROR: > > $ python tests_tagging.py > Traceback (most recent call last): > File "tests_tagging.py", line 25, in <module> > class TestFileTags(unittest.TestCase): > File "tests_tagging.py", line 31, in TestFileTags > __dict__[test] = new_test > NameError: name '__dict__' is not defined > > # CODE: > > import unittest > from mlc.filetypes import * > > TAG_VALUES = ( > ('title', 'Christmas Waltz'), > ('artist', 'Damon Timm'), > ('album', 'Homemade'), > ) > > FILES = ( > FLACFile('data/lossless/01 - Christmas Waltz.flac'), > MP3File('data/lossy/04 - Christmas Waltz (MP3-79).mp3'), > OGGFile('data/lossy/01 - Christmas Waltz (OGG-77).ogg'), > MP4File('data/lossy/06 - Christmas Waltz (M4A-64).m4a'), > ) > > list_of_tests = [] > for file in FILES: > for k, v in TAG_VALUES: > test_name = 'test_' + file.exts[0] + '_' + k > list_of_tests.append((test_name, file, k, v)) > > class TestFileTags(unittest.TestCase): > > for test in list_of_tests: > def new_test(self): > self.assertEqual(test[1].tags[test[2]],test[3]) > > __dict__[test] = new_test > > if __name__ == '__main__': > unittest.main() > > > On Thu, May 6, 2010 at 12:26 PM, Lie Ryan <lie.1...@gmail.com> wrote: >> On 05/06/10 10:37, Damon Timm wrote: >>> Hi - am trying to write some unit tests for my little python project - >>> I had been hard coding them when necessary here or there but I figured >>> it was time to try and learn how to do it properly. >>> <snip> >>> This test works, however, it only runs as *one* test (which either >>> fails or passes) and I want it to run as 12 different tests (three for >>> each file type) and be able to see which key is failing for which file >>> type. I know I could write them all out individually but that seems >>> unnecessary. >> >> One way to do what you wanted is to harness python's dynamicity and >> generate the methods by their names: >> >> class TestFiles(unittest.TestCase): >> for methname, case in somedict: >> def test(self): >> ... >> __dict__[methname] = test >> >> _______________________________________________ >> Tutor maillist - tu...@python.org >> To unsubscribe or change subscription options: >> http://mail.python.org/mailman/listinfo/tutor >> > _______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor