繁体   English   中英

测试期间最新的重复初始化类

[英]Nosetest repeated init class during testing

我正在尝试使用鼻子测试并具有以下结构

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

根据鼻子测试文档,我知道鼻子测试将索引所有测试/文件夹和其中的文件,并对以test_zzz开头的功能进行测试。 但是,这使我感到困惑,如何在类C和类B中实际使用objIWant,它们是从类A派生的,而没有两次打印“ Hello World”(初始化时只应打印一次,其他类应该可以访问)来自父类的相同objIWant)

我如何才能做到这一点,从而更好地将我的单元测试模块结构化?

docs开始 ,为每个新的测试类调用setUpClass

当测试套件遇到来自新类的测试时,将调用前一个类(如果有)的tearDownClass(),然后调用新类的setUpClass()。

如果您希望所有测试类仅调用一次该方法,则可以使用该链接的下一段中所述的setUpModule

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM