【问题标题】:Nosetest repeated init class during testingNosetest 在测试期间重复初始化类
【发布时间】:2016-11-04 10:35:09
【问题描述】:

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

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 开头的函数进行测试。 然而,它让我感到困惑,如何在 C 类和 B 类中实际拥有派生自 A 类的 objIWant,而不打印两次“Hello World”(它应该只在初始化时打印一次,其他类应该可以访问来自父类的相同 objIWant)

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

【问题讨论】:

    标签: python unit-testing nose


    【解决方案1】:

    setUpClassdocs 中为每个新的测试类调用:

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-02-10
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-03-10
      • 2018-04-05
      • 1970-01-01
      • 2020-06-29
      相关资源
      最近更新 更多