Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Child class setUpTestData is not called when time_machine is used as a class decorator for Django tests #402

Closed
vkhudiakova opened this issue Nov 16, 2023 · 5 comments

Comments

@vkhudiakova
Copy link

Python Version

3.11

pytest Version

No response

Package Version

2.13.0

Description

Here is a repository with example: https://github.com/vkhudiakova/time-machine-bug
If I use time_machine as a class decorator for the base class, that is inherited from Django TestCase, then the child setUpTestData method is not called.

from datetime import datetime

import time_machine
from django.test import TestCase


@time_machine.travel(datetime(2023, 1, 1))
class BaseTest(TestCase):
    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()
        print('Base class method')


class ChildTest(BaseTest):
    @classmethod
    def setUpTestData(cls):
        super().setUpTestData()
        print('Child class method')

    def test_something(self):
        self.assertTrue(False)

Expected result:
Test outputs:

Base class method
Child class method

Running both base and child classes' setUpTestData methods.

Actual result

Test outputs only Base class method

If @time_machine decorator is removed from the class, the test works as expected.

@adamchainz
Copy link
Owner

Aww dammit. Class decorators are hard to get right.

Can you try recreating this in a test case in time-machine’s test suite? Rather than using setUpTestData, since there’s no Django in the project, you should be able to get the same effect with setUpClass. (Django’s setUpClass calls setUpTestData.) Run the tests with tox, such as tox -e py311 for Python 3.11.

@vkhudiakova
Copy link
Author

@adamchainz Thank you for the quick response and for the library :)
I'll take a look at the test suite.

@bp72
Copy link
Contributor

bp72 commented Nov 19, 2023

Hi! I added a small failing test, I hope it'll help!
#404

@adamchainz
Copy link
Owner

Fixed in #404 which has been released.

@vkhudiakova
Copy link
Author

Thank you for the fix!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants