Skip to content

Commit 8135455

Browse files
bpo-37094: Add example for TestCase.skipTest in unittest doc (GH-13645)
Also includes other minor test skipping doc improvements. https://bugs.python.org/issue37094 (cherry picked from commit ffed76b) Co-authored-by: Makdon <[email protected]>
1 parent ee114d7 commit 8135455

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

Doc/library/unittest.rst

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -510,7 +510,8 @@ that is broken and will fail, but shouldn't be counted as a failure on a
510510
:class:`TestResult`.
511511

512512
Skipping a test is simply a matter of using the :func:`skip` :term:`decorator`
513-
or one of its conditional variants.
513+
or one of its conditional variants, calling :meth:`TestCase.skipTest` within a
514+
:meth:`~TestCase.setUp` or test method, or raising :exc:`SkipTest` directly.
514515

515516
Basic skipping looks like this::
516517

@@ -531,16 +532,23 @@ Basic skipping looks like this::
531532
# windows specific testing code
532533
pass
533534

535+
def test_maybe_skipped(self):
536+
if not external_resource_available():
537+
self.skipTest("external resource not available")
538+
# test code that depends on the external resource
539+
pass
540+
534541
This is the output of running the example above in verbose mode::
535542

536543
test_format (__main__.MyTestCase) ... skipped 'not supported in this library version'
537544
test_nothing (__main__.MyTestCase) ... skipped 'demonstrating skipping'
545+
test_maybe_skipped (__main__.MyTestCase) ... skipped 'external resource not available'
538546
test_windows_support (__main__.MyTestCase) ... skipped 'requires Windows'
539547

540548
----------------------------------------------------------------------
541-
Ran 3 tests in 0.005s
549+
Ran 4 tests in 0.005s
542550

543-
OK (skipped=3)
551+
OK (skipped=4)
544552

545553
Classes can be skipped just like methods::
546554

@@ -568,7 +576,7 @@ the test unless the passed object has a certain attribute::
568576
return lambda func: func
569577
return unittest.skip("{!r} doesn't have {!r}".format(obj, attr))
570578

571-
The following decorators implement test skipping and expected failures:
579+
The following decorators and exception implement test skipping and expected failures:
572580

573581
.. decorator:: skip(reason)
574582

0 commit comments

Comments
 (0)