PonyEars
PonyEars

Reputation: 2254

Python nose: setting description attribute on generated tests

I'm using test generators with nose. I'd like to have a custom description for each generated test.

Nose documentation says:

By default, the test name output for a generated test in verbose mode will be the name of the generator function or method, followed by the args passed to the yielded callable. If you want to show a different test name, set the description attribute of the yielded callable.

However, this doesn't work:

class TestGraphics:
   def test_all(self):                                                                                             
        for i, tc in enumerate(testcases):                                                                       
            self.run_sim.description = str(i)
            yield(self.run_sim, tc[0], tc[1]) 

I get:

AttributeError: 'method' object has no attribute 'description'

How do I set the description attribute on the callable here?

Upvotes: 4

Views: 593

Answers (1)

sowa
sowa

Reputation: 1329

The workarounds below, described here and here, appear to work.

from nose.tools import assert_true

class TestEven:

    def test_evens(self):
        for i in range(0, 5):
            yield self.check_even("check_even with {}".format(i)), i

    def check_even(self, desc):
        func = lambda n: assert_true(n % 2 == 0)
        func.description = desc
        return func
        

class TestEven:

    def test_evens(self):
        for i in range(0, 5):
            yield CheckEven(), i

class CheckEven:

    def __call__(self, n):
        self.description = "check_even with {}".format(n)
        assert n % 2 == 0

from functools import partial

class TestEven:

    def test_evens(self):
        for i in range(0, 5):
            f = partial(self.check_even, i)
            f.description = 'check_even with {}'.format(i)
            yield (f, )

    def check_even(self, n):
        assert n % 2 == 0

Upvotes: 4

Related Questions