Unittest setUpClass not working

I'm trying to get started with unittest, but I got a problem with setUpClass() . Here is my test code ...

 import unittest class TestRepGen(unittest.TestCase): """ Contains methods for training data testing """ testvar = None @classmethod def setUpClass(cls): cls.testvar = 6 def test_method(self): """ Ensure data is selected """ self.assertIsNotNone(self.testvar,"self.testvar is None!") # run tests if __name__ == '__main__': unittest.main() 

An error message is displayed indicating that self.testvar == None , and has not been changed to setUpClass() . Is there something wrong with my code?

I get the same result if I run the code from my IDE (Wing) or directly from the command line. For the record, I use Python 3.2.1 under Windows7.

+6
source share
3 answers

Well - this is the time to admit that it was my mistake. I used 3.1.2 when I should (and thought I was) using 3.2.1. Now I changed the correct version, everything is fine, and the test passes. Many thanks to everyone who answered (and sorry for wasting your time :-().

+1
source

setUpClass is new to the unittest environment in versions 2.7 and 3.2. If you want to use it with the old version, you will need to use your nose as a test runner instead of unittest.

+10
source

I am going to assume that you are not calling this test class directly, but a derived class.

If this is the case, you need to call up setUpClass() manually - it will not be automatically called.

 class TestB(TestRepGen): @classmethod def setUpClass(cls): super(TestB, cls).setUpClass() 

Also, accoding to docs class-level fixtures are implemented by the test suite . So, if you call TestRepGen or test_method some weird way that you did not send messages, setUpClass may not start.

+5
source

Source: https://habr.com/ru/post/894787/


All Articles