2

Let's say I have my unittest set up like this:

import unittest

class BaseTest(object):
    def setup(self):
        self.foo = None

    def test_something(self):
        self.assertTrue(self.foo.something())

    def test_another(self):
        self.assertTrue(self.foo.another())

    def test_a_third_thing(self):
        self.assertTrue(self.foo.a_third_thing())

class TestA(BaseTest, unittest.TestCase):
    def setup(self):
        self.foo = FooA()


class TestB(BaseTest, unittest.TestCase):
    def setup(self):
        self.foo = FooB()


class TestC(BaseTest, unittest.TestCase):
    def setup(self):
        self.foo = FooC()

Now let's say FooC doesn't have a_third_thing implemented yet, and I want to skip test_a_third_thing for ONLY the TestC class. Is there some way I can use the @unittest.skipif decorator to do this? Or some other handy way to skip this test for only this class?

Python 2.7, in case it matters

C_Z_
  • 7,427
  • 5
  • 44
  • 81

2 Answers2

1

You cannot use @unittest.skipif here because it is evaluated during module, and the check needed should be run during runtime.

To achieve desired result your test_a_third_thing in base class should look like this:

class BaseTest(unittest.TestCase):   
    def test_a_third_thing(self):
        if not getattr(self.foo, "a_third_thing", None):
            self.skipTest(self.foo.__class__.__name__ + ' has no a_third_thing, skip')
        else:
            self.assertTrue(self.foo.a_third_thing())

Also fix typos in your example setup to setUp. Remove 'unittest.TestCase' from inheritance list of test classes and add to base class.

Eduard
  • 897
  • 5
  • 11
  • Thank you. There are no typos in my example, BaseTest deliberately does not inherit from TestCase, because I do not want to run tests for the BaseTest (it has no valid foo attribute). See this question:https://stackoverflow.com/questions/1323455/python-unit-test-with-base-and-sub-class – C_Z_ Sep 24 '18 at 22:45
  • @C_Z_ if this answered your question can you accept this answer? – Eduard Sep 25 '18 at 20:43
  • this does not answer my questions specifically, for instance let's say I do have a `foo` method defined for FooC but it is not completely implemented, and is returning 'False' instead. Possibly overwriting the test in TestC, as pylang suggested, is the way to go. – C_Z_ Sep 25 '18 at 20:47
1

You may not need to "skip" the test. One simple approach is to override the base test with a dummy.

class TestC(BaseTest, unittest.TestCase):
    def setup(self):
        self.foo = FooC()

    def test_a_third_thing(self):
        """Override the assertions of the base test."""
        pass
pylang
  • 40,867
  • 14
  • 129
  • 121