Skip to content

Instantly share code, notes, and snippets.

@jonashaag
Last active December 5, 2022 16:18
Show Gist options
  • Select an option

  • Save jonashaag/834a5f6051094dbed3bc to your computer and use it in GitHub Desktop.

Select an option

Save jonashaag/834a5f6051094dbed3bc to your computer and use it in GitHub Desktop.
Python unittest base class skipper
def test_base(base_cls):
class BaseClassSkipper(base_cls):
@classmethod
def setUpClass(cls):
if cls is BaseClassSkipper:
raise unittest.SkipTest("Base class")
super().setUpClass()
return BaseClassSkipper
@test_base
class TestsBase(TestCase):
...
class ConcreteTest(TestsBase):
...
@supunt
Copy link

supunt commented Sep 10, 2019

Sorry for the delayed response

First, below is my folder structure (Ignore the warning things as I moved a bunch of code from there to clear things up)

image

now to the files

test_base.py

import unittest


def test_base(base_cls):
    class BaseClassSkipper(base_cls):
        @classmethod
        def setUpClass(cls):
            if cls is BaseClassSkipper:
                raise unittest.SkipTest("Base class")
            super().setUpClass()
    return BaseClassSkipper


@test_base
class TestBase(unittest.TestCase):
    def test_st_01_test_something(self):
        pass

    def test_st_02_test_something_else(self):
        pass

test_database.py

from test_cases.test_base.test_base import TestBase


class TestDatabase(TestBase):
    """

    """

test_files.py

from test_cases.test_base.test_base import TestBase


class TestFile(TestBase):
    """

    """

When I run the folder test_impl. I get the following output. the marked bit is the problem that I want to sort out (Not sure if it is possible)

image

@supunt
Copy link

supunt commented Sep 10, 2019

Only thing that worked for me is

in test_base.py

def setUpClass(cls):

    if cls.__name__ == TestBase.__name__:
        raise unittest.SkipTest("Base class")

    # do foo
    # do bar

    super().setUpClass()

def setUp(self):
    if self.__class__.__name__ == TestBase.__name__:
        raise unittest.SkipTest("Base class")

    # do foo
    # do bar
    
    super().setUp()

@jonashaag
Copy link
Author

Please post a full example

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment