On 10/21/2015 02:55 AM, Ulrich Laube wrote: > Am 21.10.2015 um 00:18 schrieb Carl Meyer: >> On 10/19/2015 03:03 AM, Ulrich Laube wrote: >>> It boils down to this: >>> [...] >>> Is it a known problem? >> >> Not really a problem - you just need to be aware that you can't rely on >> consistent database IDs in tests. > > So there is no test isolation where I assumed it to be.
There is test isolation, it just doesn't extend to sequences. Tables are cleared of all data in between tests, so your second test still sees only a single Post in the database, it's just a Post with ID 2 instead of ID 1. > My assumption was, that each > > class younameit(TestCase) > > gets its own fresh database to work with, A completely fresh database would be the ideal situation from an isolation standpoint, but it is prohibitively slow to actually create a new test database for each test or test case. The actual behavior is pretty close to "a fresh database for each test", though -- ID numbering is really the only exception you're likely to commonly see. Really since auto ID numbers are chosen by the database, not your code, it's better practice for your tests not to rely on them regardless. > so that all > > def test_foobar(self): > > within, would work against the same DB. No, the isolation is per test method, not per test class. Every single test method effectively runs in a clean database (modulo the fact that sequences aren't reset). The details of how this isolation is implemented vary by the type of test. For normal TestCases, each test method runs within a transaction, and the test runner rolls back the transaction at the end of each test so none of its changes survive. This is the fastest method, but it means that the code under test cannot start or commit/rollback transactions. For tests which may need to test transaction behavior, we have TransactionTestCase (which LiveServerTestCase inherits from); in this case the isolation is implemented by truncating all tables after each test. This is slower, but doesn't interfere with transactions in the code under test. Carl -- You received this message because you are subscribed to the Google Groups "Django users" group. To unsubscribe from this group and stop receiving emails from it, send an email to django-users+unsubscr...@googlegroups.com. To post to this group, send email to django-users@googlegroups.com. Visit this group at http://groups.google.com/group/django-users. To view this discussion on the web visit https://groups.google.com/d/msgid/django-users/5627A0A3.60902%40oddbird.net. For more options, visit https://groups.google.com/d/optout.
signature.asc
Description: OpenPGP digital signature