How to fail a python unittest in setUpClass?
Question:
I am doing some unittests with python and some pre-test checks in setUpClass
. How can I throw a unitest
–fail
within the setUpClass
, as the following simple example:
class MyTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
unittest.TestCase.fail("Test")
def test1(self):
pass
if __name__ == '__main__':
unittest.main()
gives the error TypeError: unbound method fail() must be called with TestCase instance as first argument (got str instance instead)
.
I understand the error I get as fail is a instance method, and I don’t have an instance of MyClass
yet. Using an instance on-the-fly like
unittest.TestCase().fail("Test")
also does not work, as unittest.TestCase
itself has no tests. Any ideas how to fail all tests in MyClass
, when some condition in setUpClass
is not met?
Followup question: Is there a way to see the tests in setUpClass
?
Answers:
self.fail("test")
put into your setUp instance method fails all the tests
I think the easiest way to do this at the class level is to make a class variable so something like:
@classmethod
def setUpClass(cls):
cls.flag = False
def setUp(self):
if self.flag:
self.fail("conditions not met")
Hope this is what you want.
Using a simple assert should work
assert False, "I mean for this to fail"
I’m not an expert in python but with same problem, I’ve resolved adding cls param:
...
@classmethod
def setUpClass(cls):
cls.fail(cls, "Test")
...
I think is strange but clearer.
When you pass cls, warning appear (Expected type ‘TestCase’, got ‘Type[MyTests]’ instead) and MyTests inherits from TestCase thus can
ignoring adding: # noinspection PyTypeChecker
I am doing some unittests with python and some pre-test checks in setUpClass
. How can I throw a unitest
–fail
within the setUpClass
, as the following simple example:
class MyTests(unittest.TestCase):
@classmethod
def setUpClass(cls):
unittest.TestCase.fail("Test")
def test1(self):
pass
if __name__ == '__main__':
unittest.main()
gives the error TypeError: unbound method fail() must be called with TestCase instance as first argument (got str instance instead)
.
I understand the error I get as fail is a instance method, and I don’t have an instance of MyClass
yet. Using an instance on-the-fly like
unittest.TestCase().fail("Test")
also does not work, as unittest.TestCase
itself has no tests. Any ideas how to fail all tests in MyClass
, when some condition in setUpClass
is not met?
Followup question: Is there a way to see the tests in setUpClass
?
self.fail("test")
put into your setUp instance method fails all the tests
I think the easiest way to do this at the class level is to make a class variable so something like:
@classmethod
def setUpClass(cls):
cls.flag = False
def setUp(self):
if self.flag:
self.fail("conditions not met")
Hope this is what you want.
Using a simple assert should work
assert False, "I mean for this to fail"
I’m not an expert in python but with same problem, I’ve resolved adding cls param:
...
@classmethod
def setUpClass(cls):
cls.fail(cls, "Test")
...
I think is strange but clearer.
When you pass cls, warning appear (Expected type ‘TestCase’, got ‘Type[MyTests]’ instead) and MyTests inherits from TestCase thus can
ignoring adding: # noinspection PyTypeChecker