Nosetest 在测试期间重复了 init class

Nosetest repeated init class during testing

我正在尝试使用 nosetests 并具有以下结构

tests|-- test_main.py
     |-- test_fileone.py
     |-- test_filetwo.py

里面test_main.py我有下面的代码

class A(unittest.TestCase):

     @classmethod
     def setUpClass(self):
         print "Hello World"
         self.objIwant="12"


      @classmethod
      def tearDownClass(cls):
          self.objIwant.quit()

里面test_fileone.py

class B(A):

         def test_loginpage(self):
             testme(self.objIwant)
          def test_logoutpage(self):
              testme_other(self.objIWant)
         #followed other def test_zzz(self)

里面test_filetwo.py

class C(A):
         def test_clickpage(self):
             clickme(self.objIwant)
          def test_revertpage(self):
              revertme(self.objIWant)
         #followed other def test_zzz(self)

我得到的结果是(按顺序):

1. HelloWorld printed 
2. Result of test_loginpage 
3. Result of test_logoutpage 
4. Helloworld printed 
5. Result of test_clickpage 
6. Result of test_revertpage

根据nosetest的文档,我了解到nosetests会索引所有tests/文件夹和里面的文件,并对以test_zzz开头的函数进行测试。 然而,它让我感到困惑的是如何在 class C 和 class B 中实际拥有 objIWant,它是从 class A 派生的,而不需要打印两次 "Hello World"(它应该只在初始化时打印一次,其他 class 应该可以从父 class)

访问相同的 objIWant

我怎样才能做到这一点,同时更好地构建我的单元测试模块?

setUpClass 为每个新的 Class 测试调用,来自 docs:

When the test suite encounters a test from a new class then tearDownClass() from the previous class (if there is one) is called, followed by setUpClass() from the new class.

如果您希望该方法在所有测试 Class 中只调用一次,也许可以使用 setUpModule,如 link 的下一段所述。