in Python by
What is Skipping Tests in Python?

1 Answer

0 votes
by
Usually, it is not advisable to skip any of the written tests.

However, in certain circumstances you may prefer skipping few tests. This can be achieved in unittest using one of the following methods.

unittest.skip

unittest.skipIf

unittest.skipUnless

unittest.expectedFailure

Skipping Tests

unittest.skip skips a decorated test unconditionally. You can pass a reason for skipping the test as an argument.

It's usage syntax is shown below.

@unittest.skip('reason for skipping')

def test_sample(self):

   ....

unittest.skipIf skips a decorated test only if a given condition is evaluated to True.

It's usage syntax is shown below.

@unittest.skipIf(condition, reason for skipping)

def test_sample(self):

   ....

Skipping Tests

unittest.skipUnless skips a test unless the given condition is True.

It's syntax is shown below.

@unittest.skipUnless(condition, reason for skipping)

def test_sample(self):

   ....

unittest.expectedFailure marks a test as a failure test. i.e even if the decorated test fails, it is not counted as a failure one.

When you run test_module2 by decorating test_sample method unittest.expectedFailure and modifying TypeError to ValueError in assertRaises expresssion, it produces the below output.

x

----------

Ran 1 test in 0.003s

OK <expected failures = 1>

A failure test is marked as x in the output.

8 of 12
...