Are scenarios run in a database transaction?
See original GitHub issueHello, I have some test cases that appear to be failing because of how the database is reset after each scenario. Specifically, database sequences are not being reset. I stumbled on what seems like conflicting statements in the documentation.
From https://pythonhosted.org/behave-django/usage.html#database-transactions-per-scenario
Each scenario is run inside a database transaction, just like your regular TestCases
From the last paragraph of https://pythonhosted.org/behave-django/usage.html#fixture-loading
This is because Django’s LiveServerTestCase resets the test database after each scenario
Now if you dig into LiveServerTestCase
via https://docs.djangoproject.com/en/1.9/topics/testing/tools/…
LiveServerTestCase does basically the same as TransactionTestCase
Or, LiveServerTestCase
is a subclass of TransactionTestCase
. And further down…
- A TransactionTestCase resets the database after the test runs by truncating all tables. A TransactionTestCase may call commit and rollback and observe the effects of these calls on the database.
- A TestCase, on the other hand, does not truncate tables after a test. Instead, it encloses the test code in a database transaction that is rolled back at the end of the test. This guarantees that the rollback at the end of the test restores the database to its initial state.
My first question is… is it correct that that behave-django is using a LiveServerTestCase, which means there is no database transaction happening?
Second, is there any way to change what happens when the database is reset? i.e. specify that the TestCase
transaction behavior be used. Or alternatively, allow support modifying things like TransactionTestCase.reset_sequences
Issue Analytics
- State:
- Created 8 years ago
- Comments:11 (6 by maintainers)
Yep, sorry I forgot to reply earlier; I saw a shiny object that day.
It is good to close.
As mentioned by a friendly user in our Gitter chat room a solution approach may be to set
serialized_rollback = True
on the testcase.See also