#24652 closed Cleanup/optimization (fixed)
SimpleTestCase should prevent execution of database queries
| Reported by: | Simon Charette | Owned by: | Simon Charette |
|---|---|---|---|
| Component: | Testing framework | Version: | dev |
| Severity: | Normal | Keywords: | |
| Cc: | Triage Stage: | Ready for checkin | |
| Has patch: | yes | Needs documentation: | no |
| Needs tests: | no | Patch needs improvement: | no |
| Easy pickings: | no | UI/UX: | no |
Description
SimpleTestCase is a simple subclass of unittest.TestCase with added assertion methods to deal with Django related stuff such as the TestClient.
While it's documented that SimpleTestCase shouldn't be used to test anything related to the ORM, nothing prevent users from doing it in unexpected ways. This can result in hard to detect test isolation issues as we have experienced with the Django test suite.
I suggest to prevent the usage of the ORM during the execution of a SimpleTestCase by raising an exception pointing to the usage of TestCase (or TransactionTestCase) on query execution attempts.
This will also be a good safenet for our internal usage, we should feel confident asking contributors to use SimpleTestCase where appropriate to avoid useless transaction wrapping.
Change History (16)
comment:1 by , 11 years ago
| Owner: | changed from to |
|---|---|
| Status: | new → assigned |
comment:2 by , 11 years ago
| Has patch: | set |
|---|---|
| Patch needs improvement: | set |
comment:3 by , 11 years ago
comment:4 by , 11 years ago
Maybe we could go with an approach where SimpleTestCase disables database queries unless the test class sets attribute allow_database_queries to True. Then the exception could tell why it isn't a good idea to allow database queries, but also allow users to execute queries if they want to do so.
This is what the actual implementation does. I'll alter the message error to suggest setting the allow_database_queries attribute to True with a big warning.
comment:5 by , 11 years ago
| Triage Stage: | Unreviewed → Accepted |
|---|
comment:6 by , 10 years ago
| Patch needs improvement: | unset |
|---|
comment:7 by , 10 years ago
| Triage Stage: | Accepted → Ready for checkin |
|---|
I'm not sure full prevention of database queries is a good idea. There are cases where you do want to run outside transaction control but prevent database flushing. The most important reasons for the latter part are that you want to preserve some of your dataset between test runs, and performance of database flushing.
Maybe we could go with an approach where SimpleTestCase disables database queries unless the test class sets attribute
allow_database_queriesto True. Then the exception could tell why it isn't a good idea to allow database queries, but also allow users to execute queries if they want to do so.Another approach could be to support TransactionTestCase with "disable_db_flush" attribute. If you use that class, then your database isn't flushed pre or post run. This one is more explicit in opting in to the unsafe behavior. The hard part might be that all flushing must be disabled until all TransactionTestCases with disable_db_flush set to True are ran.