21

I was playing with pyUnit framework for unittest testing of my application. Is there any any way to skip all the tests in class if certain condition in setUpClass fails?

Currently, I am setting up environment (creating resources, configuring global settings) in setUpClass. But, if any of these resource creation fails..I am raising exception. Instead of that I want to skip the whole test suite.

Nakilon
  • 34,866
  • 14
  • 107
  • 142
Mayur
  • 3,063
  • 10
  • 42
  • 55

2 Answers2

42

Got the answer:

For those who gets stuck here-

unittest can be skipped from setUpClass in following way-

raise unittest.SkipTest(message)
Mayur
  • 3,063
  • 10
  • 42
  • 55
  • 1
    Here is the doc http://docs.python.org/library/unittest.html#skipping-tests-and-expected-failures – Facundo Casco Jul 12 '12 at 13:53
  • 4
    you can also raise this from the tests themselves if they determine they shouldn't be run for whatever reason. – gps Jul 13 '12 at 01:07
  • 1
    Is there a way we can skip it without raising exception? I want to see 0 exception and 0 failures when we skip it in setupclass. This method shows exception in the run results. – Gaurav Aradhye Apr 21 '15 at 06:23
15

Instead of explicitly raising the SkipTest exception, the documentation suggests using TestCase.skipTest(). For example:

def setUp(self):
    if skip_tests_flag:
        self.skipTest('skipped test due to skip_tests_flag')
Cartucho
  • 3,257
  • 2
  • 30
  • 55
studgeek
  • 14,272
  • 6
  • 84
  • 96
  • yes, but you spend additional time in setUpClass and setUp methods for the test which you are going to skip – Andriy Ivaneyko Jun 01 '17 at 09:58
  • @AndriyIvaneyko But if the condition can only be evaluated in the `setUp` class, it's only possible there. Also, if you use inheritance, that's the way to go. And this is what the OP asked for. – sebix Jan 11 '21 at 16:04
  • @sebix thanks, OP asked clearly that skip shall be done if condition in setUpClass fails. Not sure that there mentioning on inheritance in that use case. – Andriy Ivaneyko Jan 11 '21 at 16:30
  • @AndriyIvaneyko Sorry for the incomprehensible wording. My last sentence did not refer to the one before. – sebix Jan 11 '21 at 21:24