Generator and plugins

12 views
Skip to first unread message

Mike

unread,
May 24, 2016, 11:16:10 AM5/24/16
to nose-users
Hello everyone,

I wanted to know if there is any way I could use a plugin when, at the same time, I am using generator methods inside classes.
For example, I have a TestSample class :

class TestSample(object):
   
def test_ok(self):
       
assert False

    def test_nok(self):
       
assert True

    def test_generator(self):
       
for i in range(0, 5):
           
yield self.random_method, i

   
def random_method(self, i):
       
assert i > 2, 'i == %d <= 2' % i

And a MyPlugin class which inherit from Plugin nose class (which is enabled by default) :

class MyPlugin(nose.plugins.Plugin):
    name
= 'my-plugin'

    def __init__(self):
       
super(MyPlugin, self).__init__()
       
self.log = []

   
def configure(self, options, conf):
       
super(MyPlugin, self).configure(options, conf)
       
self.log.append('[MyPlugin] configure')
       
self.enabled = True

    def addSuccess(self, test):
       
self.log.append('> Test succeed !')

   
def addError(self, test, err):
       
self.log.append('> Test error !')

   
def addFailure(self, test, err):
       
self.log.append('> Test failed !')

   
def finalize(self, result):
       
self.log.append("Ran %d test(s) [%d failure[s], %d error(s)]" %
                         
(result.testsRun, len(result.failures), len(result.errors)))

       
for line in self.log:
           
print line

   
def setOutputStream(self, stream):
       
class dummy:
           
def flush(self):
               
pass
            def write(self, *arg):
               
pass
            def writeln(self, *arg):
               
pass

        return dummy()

   
def startTest(self, test):
       
self.log.append('[START] %s ' % str(test))
       
self.log.append(str(test))

   
def stopTest(self, test):
       
self.log.append('--\n')


If I start the tests like following :

tests = nose.loader.TestLoader().loadTestsFromName('TestSample')
nose.run(suite=tests, addplugins=[MyPlugin()])

I got that output :

[MyPlugin] configure
Ran 7 test(s) [4 failure[s], 0 error(s)]

As you can see, all tests are done and gave results as expected. But, as you can see, most of the methods in the plugin are not called.I succeed to make it work (partially) in only one situation : when the TestSample class inherit from unittest.TestCase and also when the tests are loaded with defaultTestLoader.loadTestsFromTestCase. Obviously, because that nose will not be in 'discovery mode', the yield generator is not taken into account.

Does anyone has already tried to do something like that ?

Thanks.
Reply all
Reply to author
Forward
0 new messages