[Tutor] unittest decorators

Albert-Jan Roskam fomcl at yahoo.com
Fri Apr 4 08:58:12 CEST 2014


________________________________
> From: Peter Otten <__peter__ at web.de>
>To: tutor at python.org 
>Sent: Thursday, April 3, 2014 3:13 PM
>Subject: Re: [Tutor] unittest decorators
> 
>
>Albert-Jan Roskam wrote:
>
>> The unittest module has some really handy decorators: @unittest.skip
>> and @unittest.skipIf. I use the former for temporary TODO or FIXME things,
>> but I use the latter for a more permanent thing:
>> @unittest.skipif(sys.version_info()[0] > 2). Yet, in the test summary you
>> just see error, skipped, failed. Is it possible to not count the skipIf
>> tests? 
>
>You mean like this?
>
>$ cat skiptest.py
>import unittest
>import sys
>
>def hide_if(condition):
>    def g(f):
>        return None if condition else f
>    return g
>
>class T(unittest.TestCase):
>    @hide_if(sys.version_info[0] > 2)
>    def test_two(self):
>        pass
>    @hide_if(sys.version_info[0] < 3)
>    def test_three(self):
>        pass
>
>if __name__ == "__main__":
>    unittest.main()
>$ python skiptest.py -v
>test_two (__main__.T) ... ok
>
>----------------------------------------------------------------------
>Ran 1 test in 0.000s
>
>OK
>$ python3 skiptest.py -v
>test_three (__main__.T) ... ok
>
>----------------------------------------------------------------------
>Ran 1 test in 0.000s
>
>OK
>$ 


Wow, yes, this is exactly what I meant! Thank you! Now the "bad/code smell/TODO" skips are separated from the legitimately skipped tests.



>> (other than using if-else inside the test --not really a bad
>> solution either ;-)?
>
>I don't understand that remark.

Ok, that was indeed a bit cryptic, sorry. I meant something like this:

class T(unittest.TestCase): 

    def test_combined(self):
        if(sys.version_info[0] > 2:

            pass 
        else: 

            pass


More information about the Tutor mailing list