pyunit: remove a test case on the fly

Konstantin Veretennicov kveretennicov at gmail.com
Wed Jun 15 19:21:06 EDT 2005


On 15 Jun 2005 14:13:09 -0700, chris <chris.levis at gmail.com> wrote:
> We have a number of TestCase classes that have multiple test methods.
> We are interested in removing any of the individual test methods on the
> fly (dynamically, at runtime, whatever).

Here's a simple approach imitating NUnit's CategoryAttribute. I don't
know whether it'll work for you, it depends on what exactly
isSupported() does.

- kv


import unittest

def category(*test_categories):
    tc = frozenset(test_categories)
    def f(g):
        if tc & frozenset(active_categories):
            return g
        # else return None,
        # effectively removing test from TestCase
    return f

active_categories = ['mac', 'nt']

class T(unittest.TestCase):
    
    @category('mac')
    def test_a(self):
        print 'mac only'
    
    @category('posix')
    def test_b(self):
        print 'posix only'
    
    @category('posix', 'nt')
    def test_c(self):
        print 'posix or nt'
    
    def test_d(self):
        print 'platform-independent'


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



More information about the Python-list mailing list