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

4
  • BaseTest should inherit TestCase Commented Sep 24, 2018 at 21:36
  • 1
    You could override the test in TestC, e.g. def test_a_third_thing(self): pass Commented Sep 25, 2018 at 2:04
  • @pylang This is what I ended up doing, want to add it as an answer? Commented Sep 27, 2018 at 18:39
  • Done. If you do find a way to skip tests, I'm curious to see what you find. Commented Sep 27, 2018 at 18:52

2 Answers 2

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
Sign up to request clarification or add additional context in comments.

Comments

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.

3 Comments

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:stackoverflow.com/questions/1323455/…
@C_Z_ if this answered your question can you accept this answer?
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.