[Tutor] unittest decorators

Peter Otten __peter__ at web.de
Thu Apr 3 15:13:49 CEST 2014


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
$ 

> (other than using if-else inside the test --not really a bad
> solution either ;-)?

I don't understand that remark.



More information about the Tutor mailing list