In the hobby project, I intend to use my nose for testing, I want to put all the tests for certain classes into classes, since these tests support tuning and other functions. But I cannot seem that the nose should execute installation methods inside classes.
Here is an example of a tested class:
class mwe(): def __init__(self): self.example = "" def setExample(self, ex): self.example = ex
Tests work when I do not use classes:
from nose.tools import ok_ import mwe exampleList = [] def setUp(): print("setup") exampleList.append("1") exampleList.append("2") exampleList.append("3") def test_Example(): print("test") for ex in exampleList: t = mwe.mwe() t.setExample(ex) yield check, t, ex def check(e, ex): ok_(e.example == ex)
The output will be as expected:
setup test ... ---------------------------------------------------------------------- Ran 3 tests in 0.004s OK
When a test class is used, the installation method is not executed, so the tests are not executed.
from nose.tools import ok_ import mwe class TestexampleClass(object): def __init__(self): print("__init__") self.exampleList = [] def setup(self): print("setup class") self.exampleList.append("1") self.exampleList.append("2") self.exampleList.append("3") def test_ExampleClass(self): print("test class") for ex in self.exampleList: t = mwe.mwe() t.setExample(ex) yield self.check, t, ex def check(self, we, ex): print("check class") ok_(we.example == ex)
I am new to python and new to nose, my question is why the setup is not done? Where is the error in my code?
__init__ test class ---------------------------------------------------------------------- Ran 0 tests in 0.002s OK
I will be happy for any feedback.
When I use the code from this Question on SO , the installation method runs as expected.
SOLUTION: After much despair, I discovered the following: The nose executes the class level setting method before executing the specified function, and not when the test_* methods are called, as I expected, and as is the case for other test_* methods. This obviously contradicts the documentation on the nose:
The tuning and unlocking functions can be used with test generators. However, note that the tuning and breaking attributes attached to the generator function are executed only once. To take measurements for each test received, attach the setup and break attributes to the return function or provide an instance of the called object with the settings and break attributes.
Looking at the error reports, I found an error report on github.
A possible workaround is to use class-level appliances:
@classmethod def setup_class(cls):