简体   繁体   English

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

[英]Nosetest repeated init class during testing

I am trying to use nosetests and have the following structure 我正在尝试使用鼻子测试并具有以下结构

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

inside test_main.py I have the following code 在test_main.py里面,我有以下代码

class A(unittest.TestCase):

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


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

inside test_fileone.py 在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)

inside test_filetwo.py 在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)

The result that I got is (in order): 我得到的结果是(按顺序):

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

According to the nosetest documentation, I understand that nosetests will index all tests/ folder and the files inside, and do the test for function that starts with test_zzz . 根据鼻子测试文档,我知道鼻子测试将索引所有测试/文件夹和其中的文件,并对以test_zzz开头的功能进行测试。 However it confuses me how to actually have the objIWant in class C and class B, which is derived from class A, without having the "Hello World" printed twice (it should only be printed once on initialization, and other class should have access to the same objIWant from parent class) 但是,这使我感到困惑,如何在类C和类B中实际使用objIWant,它们是从类A派生的,而没有两次打印“ Hello World”(初始化时只应打印一次,其他类应该可以访问)来自父类的相同objIWant)

How can I achieve this, that also structurized my unittests modules better ? 我如何才能做到这一点,从而更好地将我的单元测试模块结构化?

setUpClass is called for each new Class of tests, from the docs : docs开始 ,为每个新的测试类调用setUpClass

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. 当测试套件遇到来自新类的测试时,将调用前一个类(如果有)的tearDownClass(),然后调用新类的setUpClass()。

If you want to have that method called only once for all of your test Classes, perhaps use setUpModule as described in the next paragraph of that link. 如果您希望所有测试类仅调用一次该方法,则可以使用该链接的下一段中所述的setUpModule

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

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