Configure Pytest discovery to ignore class name

后端 未结 5 948
生来不讨喜
生来不讨喜 2021-01-11 14:16

Pytest\'s default discovery rules will import all Class starting with Test that do not have an __init__(). I have a situation where this causes an

5条回答
  •  谎友^
    谎友^ (楼主)
    2021-01-11 14:34

    This is an older question, but it seems to be the only relevant hit on stackoverflow, so I thought I'd leave an alternate answer here for posterity.

    Another workaround involves disabling all classname-based discovery and relying on subclass discovery only. In other words:

    In your config file: (setup.cfg or pytest.ini):

    [pytest]
    python_classes = 
    

    And in your test files:

    class TestWillNotBeRun(object):
      # Not a subclass of unittest.TestCase, so not collected regardless of name
    class TestWillBeRun(unittest.TestCase):
      # Still okay to use TestName convention for readability
      # It just doesn't actually do anything.
    class StillGoingToBeRun(unittest.TestCase): 
      # Relying on subclassing means *all* subclasses will be picked up, regardless of name
    

    One of the advantages of this is that it doesn't require changing your non-test class names. For a library where these classes are exposed to users, there can be good reasons for not renaming. Also, it doesn't require massive renaming of test classes (since they can now be literally anything). Finally, unlike name-based discovery, it's unlikely that non-test code will somehow be a subclass of unittest.TestCase. (I'm sure someone out there is an exception.)

    The drawback is that you must ensure that all your test classes must be subclasses of unittest.TestCase. For all of my code, that's already true, so there was no cost. That's not necessarily universally true, though.

提交回复
热议问题