2

I have bunch of unit-tests in my unit-test file. However, one of the tests I would like to skip only when running the unit-tests from command line. I know how to always skip it (@unittest.skip), but I want to somehow skip it only when running the unit-test file from command line. Is this possible?

Something like this:

test_all_my_tests.py -exclude test_number_five()

Thanks

2 Answers 2

1

Great question. One idea could be command with arguments and in the arguments specify which tests to skip. Then in your script you would parse the passed arguments and call your tests accordingly.

Your input would look like: test_all_my_tests.py -exclude 5

and in the python script it would check for a "-exclude" argument and take the following argument as well.

Good luck!

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.