'Reorder tests in the suite so TransactionTestCase-based tests come
last.
Django has a weird design decision wherein TransactionTestCase doesn\'t
clean up after itself. Instead, it resets the DB to a clean state only
at the *beginning* of each test:
https://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#
django. test.TransactionTestCase. Thus, Django reorders tests so
TransactionTestCases all come last. Here we do the same.
"I think it\'s historical. We used to have doctests also, adding cleanup
after each unit test wouldn\'t necessarily clean up after doctests, so
you\'d have to clean on entry to a test anyway." was once uttered on
#django-dev.'

def _put_transaction_test_cases_last(self, test):

    def filthiness(test):
        "Return    a    comparand    based    on    whether    a    test    is    guessed    to    clean\n                                                up    after    itself.\n\n                                                Django's    TransactionTestCase    doesn't    clean    up    the    DB    on    teardown,\n                                                but    it's    hard    to    guess    whether    subclasses    (other    than    TestCase)    do.\n                                                We    will    assume    they    don't,    unless    they    have    a\n                                                ``cleans_up_after_itself``    attr    set    to    True.    This    is    reasonable\n                                                because    the    odd    behavior    of    TransactionTestCase    is    documented,    so\n                                                subclasses    should    by    default    be    assumed    to    preserve    it.\n\n                                                Thus,    things    will    get    these    comparands    (and    run    in    this    order):\n\n                                                *    1:    TestCase    subclasses.    These    clean    up    after    themselves.\n                                                *    1:    TransactionTestCase    subclasses    with\n                                                                    cleans_up_after_itself=True.    These    include\n                                                                    FastFixtureTestCases.    If    you're    using    the\n                                                                    FixtureBundlingPlugin,    it    will    pull    the    FFTCs    out,    reorder\n                                                                    them,    and    run    them    first    of    all.\n                                                *    2:    TransactionTestCase    subclasses.    These    leave    a    mess.\n                                                *    2:    Anything    else    (including    doctests,    I    hope).    These    don't    care\n                                                                    about    the    mess    you    left,    because    they    don't    hit    the    DB    or,    if\n                                                                    they    do,    are    responsible    for    ensuring    that    it's    clean    (as    per\n                                                                    https://docs.djangoproject.com/en/dev/topics/testing/?from=\n                                                                    olddocs#writing-doctests)\n\n                                                "
        test_class = test.context
        if (is_subclass_at_all(test_class, TestCase) or (is_subclass_at_all(test_class, TransactionTestCase) and getattr(test_class, 'cleans_up_after_itself', False))):
            return 1
        return 2
    flattened = []
    process_tests(test, flattened.append)
    flattened.sort(key=filthiness)
    return ContextSuite(flattened)
