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
Priority: normal Keywords: patch

Created on 2020-06-20 00:36 by jameshcorbett, last changed 2020-07-28 23:44 by jameshcorbett.

Pull Requests
URL Status Linked Edit
PR 20996 open jameshcorbett, 2020-06-20 00:39
Messages (3)
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).
History
Date User Action Args
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