简体   繁体   中英

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

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

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

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 . 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)

How can I achieve this, that also structurized my unittests modules better ?

setUpClass is called for each new Class of tests, from the 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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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