Python Nose Test Inheritance: loading unit test fixtures from subclasses

I am converting a Python project test package from unittest to nose. The existing project structure (based on unittest) is pretty awkward, with lots of highly customizable codes for detecting and running tests, so I'm trying to move my nose to make things more streamlined.

However, I ran into problems with code creating test packages.

The project structure has two ways to run tests. One of them -

class TestSomething(unittest.TestCase): def setUp(self): ... def test_x(self): ... def test_y(self): ... suite = unittest.TestSuite() suite.addTest(unittest.makeSuite(TestSomething)) 

which is the “easy” way, this is what all Nose examples and tutorials show, and it works. However, the second way is to define a test class that contains all the test logic, and then create test cases in various subclasses that contain different configurations of settings and inherit tests from the superclass:

 class TestSomething(unittest.TestCase): def test_x(self): ... def test_y(self): ... class TestCase1(TestSomething): def setUp(self): ... class TestCase2(TestSomething): def setUp(self): ... suite = unittest.TestSuite() cases = [TestCase1,TestCase2] suite.addTests([unittest.makeSuite(case) for case in cases]) 

This is what Nose fails. First, he tries to run the testing methods, which obviously does not work, because the superclass does not have setUp (), and many of the variables used in test_x () and test_y () are not yet defined.

I have not found examples that can be done anywhere, and the Nose documentation (quite rare and difficult to navigate) does not seem to mention this either. How can this be done to work with the nose? Any help would be greatly appreciated.

+6
source share
1 answer

First of all, as unutbu pointed out, you should not give TestSomething name starting with Test , because nose automatically processes classes like test ones. In addition, nose executes all subclasses of TestCase that it finds by doing:

 class Something(unittest.TestCase): ... 

gives exactly the same results that you have. I think you should not inherit from TestCase and use this class as a mix:

 class Something(object): def test_x(self): # here the various assertEqual etc. do not resolve, but you can use them # as if they were present, since in real test-cases they will be inherited # from unittest.TestCase. ... ... class TestCase1(unittest.TestCase, Something): def setUp(self): ... 

Another way to do this is to set the class __test__ attribute to False :

 class TestSomething(unittest.TestCase): __test__ = False def test_x(self): ... class TestCase1(TestSomething): __test__ = True #must put this def setUp(self): ... 

Alternatively, you can use nose.istest and nose.nottest to mark which class is a test register and which is not:

 @tools.nottest class TestSomething(unittest.TestCase): def test_x(self): ... @tools.istest class TestCase1(TestSomething): sef setUp(self): ... 
+13
source

All Articles