python - Is it possible to get access to class-level attributes from a nose plugin? -
say have following test class:
# file tests.py class mytests(object): nose_use_this = true def test_something(self): assert 1
i can write plugin run before test:
class helloworld(plugin): # snip def starttest(self, test): import ipdb; ipdb.set_trace()
the test want be, type of test
nose.case.test
:
ipdb> str(test) 'tests.mytests.test_something' ipdb> type(test) <class 'nose.case.test'>
and can't see allow me @ nose_use_this
attribute defined in testcase-ish class.
edit:
i think best way access context startcontext
/stopcontext
method pair, , set attributes on instance there:
class myplugin(plugin): def __init__(self, *args, **kwargs): super(myplugin, self).__init__(self, *args, **kwargs) self.using_this = none def startcontext(self, context): if hasattr(context, 'nose_use_this'): self.using_this = context.nose_use_this def stopcontext(self, context): self.using_this = none def starttest(self, test): if self.using_this not none: do_something()
to robust you'll need implement stack of things since "startcontext" called (at least) modules , classes, if place attribute can set on class think simple thing should work. seems working me. (nose 1.3.0 , plugin api version 0.10)
original:
oh, it's _context()
function on test
instance:
ipdb> test._context() <class 'tests.mytests'>
and indeed have expected class-level attributes. i'm still open better way of doing this, _
makes me think nose doesn't want me treating part of api.
Comments
Post a Comment