Re: [Tutor] Newbie & Unittest ...

2010-05-07 Thread Damon Timm
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  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()
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newbie & Unittest ...

2010-05-06 Thread Steven D'Aprano
On Fri, 7 May 2010 03:53:08 am Damon Timm 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.

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.

Tests are code too, and the more complicated you make your tests, the 
less confidence you should have in them. The more tricks you use 
(dynamic methods, metaclasses, complicated frameworks, etc.) the higher 
the chances that your test code itself will be buggy, and therefore 
your pass/fail results are meaningless. For example, some time ago I 
was testing some code I had written, and was very happy that all my 
tests were passing. Then I discovered that *dozens* of tests weren't 
even being called due to a bug in the framework. When I worked around 
that problem, I discovered that now my tests were failing. Because my 
framework was complicated, it had a bug in it, which meant my tests 
were buggy, which meant my code was buggy and I didn't know.

The lesson is, keep your test code simple. Don't play tricks or be too 
clever. Don't trust your test framework, not even well-known ones like 
Python's own doctest: you should double check your results, e.g. 
sometimes I will add a test I know will fail, just to make sure that 
the framework will see it.


-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newbie & Unittest ...

2010-05-06 Thread Steven D'Aprano
On Thu, 6 May 2010 10:37:20 am Damon Timm wrote:


> class TestFiles(unittest.TestCase):
>
> # this is the basic test
> def test_values(self):
> '''see if values from my object match what they should
> match''' 
> for file in FILES:  
> for k, v in TAG_VALUES:
> self.assertEqual(self.file.tags[k], v)
>
> This test works, however, it only runs as *one* test (which either
> fails or passes) 

That is correct, because you have written it as one test. In 
unit-testing, a single test can be arbitrarily complex. In your case, 
you've created a single test which makes 12 different comparisons, and 
fails if *any* of them fail.

Here is an analogy... suppose you are comparing the two strings for 
equality. Python does this:

* If the lengths are different, return False (the test fails);
* If the first characters differ, return False;
* If the second characters differ, return False;
* If the third characters differ, return False;
* ... and so on ...
* return True (the test passes)

The data that you feed are the strings "abcd" and "abce". Is that five 
tests, with the first four passing and the last failing? Well, yes, but 
not in any meaningful sense. Even if it is useful to see *where* the 
strings differ, it would be silly to treat each comparison as a 
separate test.


> 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.

Unfortunately, if you really want them to be twelve individual tests, 
then you need to write them out individually as twelve separate tests.

As for the second question, to see where the failure is, you can pass an 
extra argument to assertEqual:

self.assertEqual(self.file.tags[k], v, 
"fails for file %s with tag %s and value %s" % (file, k, v))

Alternatively, you can take a separate approach. Since you have four 
different file types, I would test each type separately, using 
inheritance to reduce the amount of work needed.

# Untested.
class TestMP3(unittest.TestCase):
filename = 'data/lossy/04 - Christmas Waltz (MP3-79).mp3'
filetype = MP3File
def __init__(self):
self.file = self.filetype(self.filename)
def test_title(self):
self.assertEquals(self.file.tags['title'], 'Christmas Waltz')
def test_artist(self):
self.assertEquals(self.file.tags['artist'], 'Damon Timm')
def test_album(self):
self.assertEquals(self.file.tags['album'], 'Homemade')


class TestFLAC(TestMP3):
filename = 'data/lossless/01 - Christmas Waltz.flac'
filetype = FLACFile

class TestOGG(TestMP3):
    filetype = OGGFile
filename = 'data/lossy/01 - Christmas Waltz (OGG-77).ogg'

class TestMP4(TestMP3):
    filetype = MP4File
filename = 'data/lossy/06 - Christmas Waltz (M4A-64).m4a'


And you're done, 12 separate tests with hardly any extra typing. And now 
you can easily add specific tests, e.g. testing that FLAC actually is 
lossless:


class TestFLAC(TestMP3):
filename = 'data/lossless/01 - Christmas Waltz.flac'
filetype = FLACFile
def test_lossless(self):
raw = open('raw sounds.wav', 'r').read()
data = self.file.convert_to_wav()
self.assertEquals(raw, data)




-- 
Steven D'Aprano
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newbie & Unittest ...

2010-05-06 Thread Vincent Davis
On Thu, May 6, 2010 at 1:15 PM, Steve Willoughby  wrote:

> The unit test methods all take message arguments so if you just
> want to customize the reported error, that's easily done.
>
> something like:
>  self.assertEqual(self.file.tags[k], v, "Failure with key "+k)
>
> That's easiest.  If you really want a separate test for each, you
> may want to create a factory function which will generate the individual
> test methods when the testcase object is created.
>
> --steve


Looks like Steve answered the question you had for me,
 "self.assertEqual(self.file.tags[k], v, "Failure with key "+k)" I think
this is the best(how I would do it) solution, 1 test for files with a
meaningful report as to which file is the problem.

  *Vincent Davis
720-301-3003 *
vinc...@vincentdavis.net
 my blog  |
LinkedIn

On Thu, May 6, 2010 at 1:15 PM, Steve Willoughby  wrote:

> The unit test methods all take message arguments so if you just
> want to customize the reported error, that's easily done.
>
> something like:
>  self.assertEqual(self.file.tags[k], v, "Failure with key "+k)
>
> That's easiest.  If you really want a separate test for each, you
> may want to create a factory function which will generate the individual
> test methods when the testcase object is created.
>
> --steve
>
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newbie & Unittest ...

2010-05-06 Thread Steve Willoughby
The unit test methods all take message arguments so if you just
want to customize the reported error, that's easily done.

something like:
  self.assertEqual(self.file.tags[k], v, "Failure with key "+k)

That's easiest.  If you really want a separate test for each, you
may want to create a factory function which will generate the individual
test methods when the testcase object is created.

--steve
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newbie & Unittest ...

2010-05-06 Thread Damon Timm
Sorry for the multiple posts ... I'll be quiet for a while until I
find a real answer!

What I wrote below doesn't actually work -- it appears to work because
all the functions have different names but they all reference a single
function ... I should have looked more closely at my initial output...
I'm going to have to look into why that is.  I need a way to make each
function unique ...

On Thu, May 6, 2010 at 2:04 PM, Damon Timm  wrote:
> 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)
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newbie & Unittest ...

2010-05-06 Thread Damon Timm
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  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 
>    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  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.
>>> 
>>> 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


Re: [Tutor] Newbie & Unittest ...

2010-05-06 Thread Damon Timm
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 
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  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.
>> 
>> 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


Re: [Tutor] Newbie & Unittest ...

2010-05-06 Thread Lie Ryan
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.
> 
> 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  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor


Re: [Tutor] Newbie & Unittest ...

2010-05-06 Thread Damon Timm
Hi Vincent - Thanks for your input.

Where would I put that string ?  In the function's doctsring ?  Or
just as a print method ?

I have been looking online some more and it appears there may be a way
to create some sort of generator ... it's still a little confusing to
me, though.  I was hoping there was an easier way.  I can't imagine I
am the first person with this task to accomplish ...

Thanks,
Damon



On Thu, May 6, 2010 at 9:46 AM, Vincent Davis  wrote:
> By they way you shouldn't need to use str(file) as I did. Unlessit is
> not a string already. Bad habit. I am used to numbers
> vincet
>
> On Thursday, May 6, 2010, Vincent Davis  wrote:
>> I can't think of a way to do what you ask, without defining a test for each. 
>> ButI think what you might actually want is the define the error message to 
>> report which one failed. ie, it's one test with a meaningful error message.
>> 'Failed to load' + str(file)+' '+ str(k)+', '+str(v)I am not ecpert on 
>> unittests
>>
>>
>>
>>
>>
>>   Vincent Davis
>>     720-301-3003
>>
>>     vinc...@vincentdavis.net
>>
>>   my blog  |
>>   LinkedIn 
>> On Wed, May 5, 2010 at 6:37 PM, 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.
>>
>> I've read over Python's guide
>> (http://docs.python.org/library/unittest.html) but I am having a hard
>> time understanding how I can apply it *properly* to my first test case
>> ...
>>
>> What I am trying to do is straightforward, I am just not sure how to
>> populate the tests easily.  Here is what I want to accomplish:
>>
>> # code
>> import unittest
>> from mlc.filetypes import * # the module I am testing
>>
>> # here are the *correct* key, value pairs I am testing against
>> TAG_VALUES = (
>>     ('title', 'Christmas Waltz'),
>>     ('artist', 'Damon Timm'),
>>     ('album', 'Homemade'),
>> )
>>
>> # list of different file types that I want to test my tag grabbing 
>> capabilities
>> # the tags inside these files are set to match my TAG_VALUES
>> # I want to make sure my code is extracting them correctly
>> 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'),
>> )
>>
>> class TestFiles(unittest.TestCase):
>>
>>     # this is the basic test
>>     def test_values(self):
>>         '''see if values from my object match what they should match'''
>>         for file in FILES:
>>             for k, v in TAG_VALUES:
>>                 self.assertEqual(self.file.tags[k], v)
>>
>> 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.
>>
>> I suspect my answer lies in the Suites but I can't wrap my head around it.
>>
>> Thanks!
>>
>> Damon
>> ___
>> Tutor maillist  -  tu...@python.org
>> To unsubscribe or change subscription options:
>> http://mail.python.org/mailman/listinfo/tutor
>>
>>
>>
> ___
> 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


Re: [Tutor] Newbie & Unittest ...

2010-05-06 Thread Vincent Davis
By they way you shouldn't need to use str(file) as I did. Unlessit is
not a string already. Bad habit. I am used to numbers
vincet

On Thursday, May 6, 2010, Vincent Davis  wrote:
> I can't think of a way to do what you ask, without defining a test for each. 
> ButI think what you might actually want is the define the error message to 
> report which one failed. ie, it's one test with a meaningful error message.
> 'Failed to load' + str(file)+' '+ str(k)+', '+str(v)I am not ecpert on 
> unittests
>
>
>
>
>
>   Vincent Davis
> 720-301-3003
>
> vinc...@vincentdavis.net
>
>   my blog  |
>   LinkedIn 
> On Wed, May 5, 2010 at 6:37 PM, 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.
>
> I've read over Python's guide
> (http://docs.python.org/library/unittest.html) but I am having a hard
> time understanding how I can apply it *properly* to my first test case
> ...
>
> What I am trying to do is straightforward, I am just not sure how to
> populate the tests easily.  Here is what I want to accomplish:
>
> # code
> import unittest
> from mlc.filetypes import * # the module I am testing
>
> # here are the *correct* key, value pairs I am testing against
> TAG_VALUES = (
>     ('title', 'Christmas Waltz'),
>     ('artist', 'Damon Timm'),
>     ('album', 'Homemade'),
> )
>
> # list of different file types that I want to test my tag grabbing 
> capabilities
> # the tags inside these files are set to match my TAG_VALUES
> # I want to make sure my code is extracting them correctly
> 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'),
> )
>
> class TestFiles(unittest.TestCase):
>
>     # this is the basic test
>     def test_values(self):
>         '''see if values from my object match what they should match'''
>         for file in FILES:
>             for k, v in TAG_VALUES:
>                 self.assertEqual(self.file.tags[k], v)
>
> 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.
>
> I suspect my answer lies in the Suites but I can't wrap my head around it.
>
> Thanks!
>
> Damon
> ___
> 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


Re: [Tutor] Newbie & Unittest ...

2010-05-06 Thread Vincent Davis
I can't think of a way to do what you ask, without defining a test for each.
ButI think what you might actually want is the define the error message to
report which one failed. ie, it's one test with a meaningful error message.
'Failed to load' + str(file)+' '+ str(k)+', '+str(v)
I am not ecpert on unittests

  *Vincent Davis
720-301-3003 *
vinc...@vincentdavis.net
 my blog  |
LinkedIn

On Wed, May 5, 2010 at 6:37 PM, 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.
>
> I've read over Python's guide
> (http://docs.python.org/library/unittest.html) but I am having a hard
> time understanding how I can apply it *properly* to my first test case
> ...
>
> What I am trying to do is straightforward, I am just not sure how to
> populate the tests easily.  Here is what I want to accomplish:
>
> # code
> import unittest
> from mlc.filetypes import * # the module I am testing
>
> # here are the *correct* key, value pairs I am testing against
> TAG_VALUES = (
>('title', 'Christmas Waltz'),
>('artist', 'Damon Timm'),
>('album', 'Homemade'),
> )
>
> # list of different file types that I want to test my tag grabbing
> capabilities
> # the tags inside these files are set to match my TAG_VALUES
> # I want to make sure my code is extracting them correctly
> 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'),
> )
>
> class TestFiles(unittest.TestCase):
>
># this is the basic test
>def test_values(self):
>'''see if values from my object match what they should match'''
>for file in FILES:
>for k, v in TAG_VALUES:
>self.assertEqual(self.file.tags[k], v)
>
> 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.
>
> I suspect my answer lies in the Suites but I can't wrap my head around it.
>
> Thanks!
>
> Damon
> ___
> Tutor maillist  -  Tutor@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


[Tutor] Newbie & Unittest ...

2010-05-05 Thread Damon Timm
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.

I've read over Python's guide
(http://docs.python.org/library/unittest.html) but I am having a hard
time understanding how I can apply it *properly* to my first test case
...

What I am trying to do is straightforward, I am just not sure how to
populate the tests easily.  Here is what I want to accomplish:

# code
import unittest
from mlc.filetypes import * # the module I am testing

# here are the *correct* key, value pairs I am testing against
TAG_VALUES = (
('title', 'Christmas Waltz'),
('artist', 'Damon Timm'),
('album', 'Homemade'),
)

# list of different file types that I want to test my tag grabbing capabilities
# the tags inside these files are set to match my TAG_VALUES
# I want to make sure my code is extracting them correctly
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'),
)

class TestFiles(unittest.TestCase):

# this is the basic test
def test_values(self):
'''see if values from my object match what they should match'''
for file in FILES:
for k, v in TAG_VALUES:
self.assertEqual(self.file.tags[k], v)

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.

I suspect my answer lies in the Suites but I can't wrap my head around it.

Thanks!

Damon
___
Tutor maillist  -  Tutor@python.org
To unsubscribe or change subscription options:
http://mail.python.org/mailman/listinfo/tutor