Opened 11 years ago

Closed 11 years ago

Last modified 11 years ago

#6923 closed (wontfix) does not delete test users 'testuser' and 'testuser2'

Reported by: anonymous Owned by: nobody
Component: Contrib apps Version: master
Severity: Keywords: auth
Cc: em@… Triage Stage: Unreviewed
Has patch: no Needs documentation: no
Needs tests: no Patch needs improvement: no
Easy pickings: no UI/UX: no


In there are tests for checking that the auth module works correctly. To do this two test users are created, but not deleted. This means that user tests that use the name "testuser" or "testuser2" will get incorrect password errors, and not log in. The test database should be empty.

Change History (6)

comment:1 Changed 11 years ago by anonymous

Cc: em@… added

comment:2 Changed 11 years ago by devin

Resolution: worksforme
Status: newclosed

I've tried reproducing this with both Doctests and Django TestCase, but in both cases, the test database contains no Users. Since test databases are created in the __call__ method of TestCase, it doesn't seem like this could happen for Django TestCase. I don't know about the Doctests, but I couldn't reproduce using a variety of database engines.

comment:3 Changed 11 years ago by Emil Stenström <em@…>

Resolution: worksforme
Status: closedreopened

@devin: Sorry if I was unclear, this bug has to do with TestCase, not Doctests. The file it concerns is: which clearly has two lines of type:

u = User.objects.create_user('testuser', '', 'testpw')

...but no corresponding line for deleting that user. It will of course be deleted after all tests are done, but that's too late. When I run " test" all tests are run, both the and my own, so "testuser" will be in the test database when my tests are run.

I hope this explanation warrants a reopening of the ticket, otherwise you may slap me.

comment:4 Changed 11 years ago by Russell Keith-Magee

Resolution: wontfix
Status: reopenedclosed

This is a known limitation of using doctests. Essentially, you can't write a doctest or vanilla unit test and expect that the database is clean at the start of the test.

The only permanent solutions that exist involve flushing the database at the start or end of every doctest. Unfortunately, flushing is an expensive operation, so we don't want to do it unless we know we need to.

The workaround is to modify your test; Either modify your test conditions to check for deltas rather than absolutes (e.g., find the number of users at the start of the test, and after adding some users check that current count - initial count = expected) or manually flush the database at the start of your test. You can do this by calling:

>>> from import call_command
>>> call_command('flush', verbosity=0, interactive=False)

at the start of your test.

This is somewhat related to #5624; this ticket calls for finding a way to associate fixtures with doctests. Part of this process may be to find a way to explicitly request a database flush without loading a fixture.

comment:5 Changed 11 years ago by Emil Stenström <em@…>

Wouldn't it be easier to delete the users you create in this specific test? Two lines of code:


I've encountered one other user hitting the same wall when trying to name my own test user "testuser" and getting strange errors.

comment:6 Changed 11 years ago by Russell Keith-Magee

Sure - deleting those two users will solve _this_ problem, _this_ time. However, it isn't a general solution. The general solution involves flushing the database, which is an expensive operation if it isn't required.

In your case, it obviously _is_ required. doctests make no guarantees about the state of the database at the start of the test, so you need to modify your test to guarantee the initially empty state of the database, using the manual call to flush I described previously.

Note: See TracTickets for help on using tickets.
Back to Top