Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8098523
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T22:06:02+00:00 2026-06-05T22:06:02+00:00

I have some unit tests I’ve written to test my Django application. One test

  • 0

I have some unit tests I’ve written to test my Django application. One test suite in particular has a lot of code in its setUp() function. The purpose of said code is to create test data for the database. (Yes I know about fixtures and have chosen not to use them in this case). When I run the unit test suite the first test that is run passes, but then the rest of the tests in the suite fail. The message for all the failures is the same: it mentions that the error’s location is “self.database_object.save()” and that the cause is “IntegrityError: column name is not unique”. So, my best guess is that Django is not tearing down the database properly after each test.

Earlier today it was working, but I guess some refactoring I did messed it up. Any ideas on why Django is not properly tearing down the database after each test?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T22:06:03+00:00Added an answer on June 5, 2026 at 10:06 pm

    Do you use TestCase or TransactionTestCase for your base class? Sometimes this behavior is related to the optimization Django makes for TestCase in favor of TransactionTestCase. Here is the difference:

    https://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#django.test.TransactionTestCase

    class TransactionTestCase

    Django TestCase classes make use of database
    transaction facilities, if available, to speed up the process of
    resetting the database to a known state at the beginning of each test.
    A consequence of this, however, is that the effects of transaction
    commit and rollback cannot be tested by a Django TestCase class. If
    your test requires testing of such transactional behavior, you should
    use a Django TransactionTestCase.

    TransactionTestCase and TestCase are identical except for the manner
    in which the database is reset to a known state and the ability for
    test code to test the effects of commit and rollback. A
    TransactionTestCase resets the database before the test runs by
    truncating all tables and reloading initial data. 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 and reload
    initial data at the beginning of a test. Instead, it encloses the test
    code in a database transaction that is rolled back at the end of the
    test
    . It also prevents the code under test from issuing any commit or
    rollback operations on the database, to ensure that the rollback at
    the end of the test restores the database to its initial state. In
    order to guarantee that all TestCase code starts with a clean
    database, the Django test runner runs all TestCase tests first, before
    any other tests (e.g. doctests) that may alter the database without
    restoring it to its original state.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have some unit tests in my Django 1.2.4 app. One of the tests
I have some written a number of unit tests that test a wrapper around
I have some unit tests that I'm writing for a WPF application, and as
I'm writing some unit tests and I have a lot of functions of the
I am writing some unit tests for an extension method I have written on
I'm using Google Test Framework to set some unit tests. I have got three
I have a class which has some unit tests, but when I am running
I have created some unit tests for my node.js application in nodeunit. I want
I used to have a standalone script with some unit tests to test data
I have some unit tests which exercise code which makes calls out to a

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.