This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

classification
Title: unittest: make skipTest a classmethod
Type: enhancement Stage: patch review
Components: Library (Lib) Versions: Python 3.9
process
Status: open Resolution:
Dependencies: Superseder:
Assigned To: Nosy List: dorosch, jameshcorbett, serhiy.storchaka
Priority: normal Keywords: patch

Created on 2020-06-20 00:36 by jameshcorbett, last changed 2022-04-11 14:59 by admin.

Pull Requests
URL Status Linked Edit
PR 20996 open jameshcorbett, 2020-06-20 00:39
Messages (4)
msg371914 - (view) Author: James Corbett (jameshcorbett) * Date: 2020-06-20 00:36
The `unittest.TestCase.skipTest` method, used to skip the current test, is currently an instance method. There's nothing to stop it from being a `classmethod` or a `staticmethod` though---it doesn't use its reference to `self` since it's just a wrapper around the `SkipTest` exception. Making it a `classmethod` or `staticmethod` would allow calling the method from `setUpClass`. Here's an example:

```
import unittest

class MyTestCase(unittest.TestCase):

    @classmethod
    def ready_for_tests(cls):
        pass

    @classmethod
    def setUpClass(cls):
        if not cls.ready_for_tests():
            cls.skipTest()
```
msg372625 - (view) Author: Andrei Daraschenka (dorosch) * Date: 2020-06-29 20:04
Hello and thanks for your issue. Can you explain why we need to make method as `classmethod` because in your example you don't provide argument for `cls.skipTest()`.
msg374542 - (view) Author: James Corbett (jameshcorbett) * Date: 2020-07-28 23:44
I was careless in my example, it would need to be `cls.skipTest(reason)`. However, that really doesn't have anything to do with why it should be a `classmethod` instead of an instance method: it's so that you can call `skipTest` from `classmethods`, namely `setUpClass` which is called by the `unittest` framework. You can currently call `skipTest` from `setUpClass` only with something ugly like `cls.skipTest(None, reason)` (i.e. passing `None` for the `self` parameter, which works because `self` isn't used).
msg400558 - (view) Author: Serhiy Storchaka (serhiy.storchaka) * (Python committer) Date: 2021-08-29 20:40
Since you always can raise a SkipTest in setUpClass() and setUpModule() and changing skipTest() can break existing code (for example the code which calls cls.skipTest(None, reason) for some strange reasons), I think that it is not worth to change it.
History
Date User Action Args
2022-04-11 14:59:32adminsetstatus: pending -> open
github: 85218
2021-08-29 20:40:00serhiy.storchakasetstatus: open -> pending
nosy: + serhiy.storchaka
messages: + msg400558

2020-07-28 23:44:04jameshcorbettsetmessages: + msg374542
2020-06-29 20:04:19doroschsetnosy: + dorosch
messages: + msg372625
2020-06-20 00:39:15jameshcorbettsetkeywords: + patch
stage: patch review
pull_requests: + pull_request20171
2020-06-20 00:36:33jameshcorbettcreate