Hello, I'm looking for some insight for some odd behavior with some unit 
tests, which I think stems from some misunderstanding of how these classes 
actually work. 

1. For TestCase, the Django docs say that it does matter what order in 
which the tests are run. But elsewhere in the same doc, it says that 
TestCase methods are individually wrapped in a single transaction block 
that is rolled back after the test completes. So... does each test method 
start 'fresh' or not? I currently have a test method that is supposed to 
query two records, but so far it finds either 4 or 6 with no apparent 
pattern; so I don't know if it's my code that is causing problems or the 
order in which my tests are running. 

2. The strangest behavior comes from a test method that passes by itself, 
fails when run with all the others, but the code under test actually does 
seem to work. As one might expect, the section under test takes some 
records created especially for that purpose and modifies a few fields; the 
test calls the function, queries the test database, and assert-tests the 
results. The test fails, saying that only some of the fields were 
successfully changed. I thought that I perhaps forgot to call save() in one 
of my conditional branches, but I looked and found that save() was in 
indeed where it belonged. I stepped through the code and everything 
appeared to work as expected. The values looked correct. Finally, I stuck a 
query into the code under test itself immediately before the return 
statement, and printed the results; and the query proves that the database 
field values themselves did indeed change correctly. But after the return 
statement (when it goes back to the test method itself), an identical query 
gets different results. So I'm rather puzzled. I do use select_for_update 
in my section under test, which I know sometimes causes problems with 
TestCase, but all I'm doing is testing for correct results.

3. I thought subclassing TransactionTestCase instead might solve both 
problems, but set-up data that works flawlessly with TestCase breaks every 
single test with TransactionTestCase--get() complains about non-existent 
records, the db complains about broken constraints, etc. I use both 
create() and bulk_create() in setting up my data, but neither appears to 
work at all. I tried using them in setUpTestData(), setUpClass(), and 
setUp(), and not a single test would run with any of them. 

The docs say that the two classes 'are identical except for the manner in 
which the database is reset to a known state', so I'm not sure where I went 
wrong.

Using Django 2.0. And unfortunately we use MS SQL Server, which probably 
isn't helping. 

Thank you!
Heather

-- 
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 https://groups.google.com/group/django-users.
To view this discussion on the web visit 
https://groups.google.com/d/msgid/django-users/671d7dc0-ce5a-4e90-9a44-70357ecd8f6b%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Reply via email to