You can have a look at @unittest.skipIf() or even implement your own skip-decorator:
Example
Here is a working example, where I implemented a custom decorator
def skipIfOverCounter(obj):
This decorator is attached to all tests like this:
@skipIfOverCounter
def test_upper(self):
The decorator increments a count and compares it to the console argument.
Output
Implemented 3 unit tests:
- test_upper()
- test_isupper()
- test_split()
The I called python .\unittests.py 0
Skipped test 0
Ran 'test_isupper'
Ran 'test_split'
With param = 1: python .\unittests.py 1
Skipped test 1
Ran 'test_split'
Ran 'test_upper'
Skip the last test: python .\unittests.py 2
Skipped test 2
Ran 'test_isupper'
Ran 'test_upper'
Full working sample
import sys
import unittest
SKIP_INDEX = 0
COUNTER = 0
if len(sys.argv) > 1:
SKIP_INDEX = int(sys.argv.pop())
def skipIfOverCounter(obj):
global COUNTER
global SKIP_INDEX
if SKIP_INDEX == COUNTER:
print(f"Skipped test {COUNTER}")
COUNTER = COUNTER + 1
return unittest.skip("Skipped test")
COUNTER = COUNTER + 1
return obj
class TestStringMethods(unittest.TestCase):
@skipIfOverCounter
def test_upper(self):
print("Ran 'test_upper'")
self.assertEqual('foo'.upper(), 'FOO')
@skipIfOverCounter
def test_isupper(self):
print("Ran 'test_isupper'")
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper())
@skipIfOverCounter
def test_split(self):
print("Ran 'test_split'")
s = 'hello world'
self.assertEqual(s.split(), ['hello', 'world'])
# check that s.split fails when the separator is not a string
with self.assertRaises(TypeError):
s.split(2)
if __name__ == '__main__':
unittest.main()
You could even extend this by adapting the decorator to only execute the first two tests or something like this