Last active
December 5, 2022 16:18
-
-
Save jonashaag/834a5f6051094dbed3bc to your computer and use it in GitHub Desktop.
Python unittest base class skipper
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@test_base | |
class TestsBase(TestCase): | |
... | |
class ConcreteTest(TestsBase): | |
... |
Impossible. Better post a full non working example :)
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)
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)
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()
Please post a full example
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Does not seem to work when you have the following arrangement.
Well it works perfectly for the first not for the second and third
Highly appreciate if you can help
`@test_base
class TestsBase(TestCase):
...
class ConcreteTest(TestsBase):
...
class ConcreteTest2(TestsBase):
...
class ConcreteTest3(TestsBase):
...`