Skip to content

TestCase.setUpClass is not called in some cases in pytest>=6.2.0 #8509

Open
@Bobronium

Description

@Bobronium
  • a detailed description of the bug or problem you are having
  • output of pip list from the virtual environment you are using
  • pytest and operating system versions: pytest>=6.2.0,<=6.2.2
  • minimal example if possible
python version: 3.8.7 (v3.8.7:6503f05dd5, Dec 21 2020, 12:45:15)  [Clang 6.0 (clang-600.0.57)]
platform: macOS-10.16-x86_64-i386-64bit

The problem

pytest doesn't call setUpClass, if test method has / in its name. Test method itself is executed.

In example below setUpClass is executed only after running first test method. If there wasn't another test method without / in its name, setUpClass wouldn't be called at all.

from unittest import TestCase

class BaseTest(TestCase):

    def __init_subclass__(cls, **kwargs):
        def test_method(self):
            self.assertEqual('foo', self.setup_class_attr)

        # / symbol on the next line causes the problem
        setattr(cls, "test_setup_class_attr['/foo']", test_method)
        setattr(cls, "test_setup_class_attr['foo']", test_method)

    @classmethod
    def setUpClass(cls):
        cls.setup_class_attr = 'foo'

class Test(BaseTest):
    ...

Running with unittests (gives expected results):

Ran 2 tests in 0.005s

OK

Running with pytest<6.2.0 (gives expected results):

$ pytest
====================================== test session starts ======================================
platform darwin -- Python 3.8.7, pytest-6.1.2, py-1.10.0, pluggy-0.13.1
rootdir: /Users/rocky/py/test
collected 2 items                                                                               

test_setup_cls.py ..                                                                      [100%]

======================================= 2 passed in 0.04s =======================================

Running with pytest >=6.2.0:

$ pytest
====================================== test session starts =======================================
platform darwin -- Python 3.8.7, pytest-6.2.2, py-1.10.0, pluggy-0.13.1
rootdir: /Users/rocky/py/test
collected 2 items                                                                                

test_setup_cls.py F.                                                                       [100%]

============================================ FAILURES ============================================
_______________________________ Test.test_setup_class_attr['/foo'] _______________________________

self = <test_setup_cls.Test testMethod=test_setup_class_attr['/foo']>

    def test_method(self):
>       self.assertEqual('foo', self.setup_class_attr)
E       AttributeError: 'Test' object has no attribute 'setup_class_attr'

test_setup_cls.py:7: AttributeError
==================================== short test summary info =====================================
FAILED test_setup_cls.py::Test::test_setup_class_attr['/foo'] - AttributeError: 'Test' object h...
================================== 1 failed, 1 passed in 0.20s ===================================
$ pip list          
Package    Version
---------- -------
attrs      20.3.0
iniconfig  1.1.1
packaging  20.9
pip        21.0.1
pluggy     0.13.1
py         1.10.0
pyparsing  2.4.7
pytest     6.2.2
setuptools 54.2.0
toml       0.10.2

Metadata

Metadata

Assignees

No one assigned

    Labels

    plugin: unittestrelated to the unittest integration builtin plugintype: bugproblem that needs to be addressed

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions