Search code examples
pythonnosetests

Inherting test cases in Python nosetests


I have a nose testcase already written and want to inherit the same object for another test case as I'm going to change just one parameter.

So, just to understand how it works I tried to simulate the following using 2 classes NoseTesting and NoseTestingInherit.

When I run this:

  1. I don't see the print statement being printed.
  2. When I run the code I get the following result:

    ----------------------------------------------------------------------
    Ran 0 tests in 0.000s
    

I'm not sure which test method ran - Is it test_this_method_dup or test_this_method?

class NoseTesting():
    def test_this_method_dup():
        print "Test this method"

class NoseTestingInherit(NoseTesting):
    def test_this_method():
        print "Test this method"

New Code:-

import unittest
class NoseTesting(unittest.TestCase):
    def test_this_method_dup(self,):
        print "Test this method"


class NoseTestingInherit(NoseTesting):
    def test_this_method(self,):
        print "Test this method"

Output :

test_this_method_dup (nosetesting.NoseTesting) ... ok
test_this_method (nosetesting.NoseTestingInherit) ... ok
test_this_method_dup (nosetesting.NoseTestingInherit) ... ok

----------------------------------------------------------------------
Ran 3 tests in 0.003s

OK

Solution

  • As @IanAuld said, nosetest did not discover your tests. According to your example, you need not explicitly rewrite the method if you're not overriding the method since it's implicitly inherited.

    class NoseTesting(unittest.TestCase):
        def test_this_method_dup(self):
            print "Test this method"
    
    class NoseTestingInherit(NoseTesting):
        # implicitly inherit test_this_method_dup()
        # self.test_this_method_dup()