Skip to content

Unexpected test order when mixing pytest / unittest tests and transactional tests #827

Closed
@pmourlanne

Description

@pmourlanne

Let's say I have the following tests:

import pytest
from django.test import TestCase, TransactionTestCase

@pytest.mark.django_db()
def test_function_with_db():
    ...

@pytest.mark.django_db(transaction=True)
def test_function_with_transactional_db():
    ...

class MyTestCase(TestCase):
    def test_method(self):
        ...

class MyTransactionTestCase(TransactionTestCase):
    def test_method(self):
        ...

I expect transactional tests, here test_function_with_transactional_db and MyTransactionTestCase.test_method to run last.

Running pytest yields:

$ pytest -v

prototype/test_prototype.py::test_function_with_db PASSED
prototype/test_prototype.py::test_function_with_transactional_db PASSED
prototype/test_prototype.py::MyTestCase::test_method PASSED
prototype/test_prototype.py::MyTransactionTestCase::test_method PASSED

I tried fixing this by adding a pytest_collection_modifyitems in my project but my custom ordering runs before pytest-django's ordering. So pytest-django gets the last word, and in this case it's not what I want :>

I think this can be fixed by improving on what's being done in pytest_collection_modifyitems, I'll try something out and hopefully open a PR.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions