Skip to content

v8.2.0 raises Can't instantiate abstract class when a test class implements an abstract class #12275

@MartinBraquet

Description

@MartinBraquet

Before, we could have a test class inheriting from an ABC (and unittest.TestCase), now it errors because pytest is trying to instantiate the ABC itself. Running pytest should only instantiate the child test class (which indeed implements all the abstract methods of its ABC parent).
This issue does not appear in v8.1.2 and was likely introduced in #12089.

Minimal example:

from abc import ABC, abstractmethod from unittest import TestCase class Base(TestCase, ABC): @abstractmethod def foo(self): ... class Tests(Base): def foo(self): ... def test(self): ...

Output:

(test) tests 🔥pytest test.py ============================================================================= test session starts ============================================================================== platform darwin -- Python 3.9.19, pytest-8.2.0, pluggy-1.5.0 rootdir: ... configfile: pytest.ini plugins: env-1.1.3 collected 1 item / 1 error ==================================================================================== ERRORS ==================================================================================== ________________________________________________________________ ERROR collecting test.py ________________________________________________________________ /Users/martin/miniconda3/envs/test/lib/python3.9/site-packages/_pytest/runner.py:341: in from_call result: Optional[TResult] = func() /Users/martin/miniconda3/envs/test/lib/python3.9/site-packages/_pytest/runner.py:389: in collect return list(collector.collect()) /Users/martin/miniconda3/envs/test/lib/python3.9/site-packages/_pytest/unittest.py:90: in collect self.session._fixturemanager.parsefactories(self.newinstance(), self.nodeid) /Users/martin/miniconda3/envs/test/lib/python3.9/site-packages/_pytest/unittest.py:75: in newinstance return self.obj("runTest") E TypeError: Can't instantiate abstract class Base with abstract method foo =========================================================================== short test summary info ============================================================================ ERROR test.py::Base - TypeError: Can't instantiate abstract class Base with abstract method foo !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! =============================================================================== 1 error in 0.04s =============================================================================== 

pip list:

Package Version -------------- ------- exceptiongroup 1.2.1 iniconfig 2.0.0 packaging 24.0 pip 23.3.1 pluggy 1.5.0 pytest 8.2.0 pytest-env 1.1.3 setuptools 68.2.2 tomli 2.0.1 wheel 0.41.2 

Thanks for your work in general, it's a great package.

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: unittestrelated to the unittest integration builtin plugintype: bugproblem that needs to be addressedtype: regressionindicates a problem that was introduced in a release which was working previously

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions