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 795483
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:29:15+00:00 2026-05-14T22:29:15+00:00

I have the following django test case that is giving me errors: class MyTesting(unittest.TestCase):

  • 0

I have the following django test case that is giving me errors:

class MyTesting(unittest.TestCase):
    def setUp(self):
        self.u1 = User.objects.create(username='user1')
        self.up1 = UserProfile.objects.create(user=self.u1)

    def testA(self):
        ...

    def testB(self):
        ...

When I run my tests, testA will pass sucessfully but before testB starts, I get the following error:

IntegrityError: column username is not unique

It’s clear that it is trying to create self.u1 before each test case and finding that it already exists in the Database. How do I get it to properly clean up after each test case so that subsequent cases run correctly?

  • 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-05-14T22:29:16+00:00Added an answer on May 14, 2026 at 10:29 pm

    setUp and tearDown methods on Unittests are called before and after each test case. Define tearDown method which deletes the created user.

    class MyTesting(unittest.TestCase):
        def setUp(self):
            self.u1 = User.objects.create(username='user1')
            self.up1 = UserProfile.objects.create(user=self.u1)
    
        def testA(self):
            ...
    
        def tearDown(self):
            self.up1.delete()
            self.u1.delete()
    

    I would also advise to create user profiles using post_save signal unless you really want to create user profile manually for each user.

    Follow-up on delete comment:

    From Django docs:

    When Django deletes an object, it
    emulates the behavior of the SQL
    constraint ON DELETE CASCADE — in
    other words, any objects which had
    foreign keys pointing at the object to
    be deleted
    will be deleted along with
    it.

    In your case, user profile is pointing to user so you should delete the user first to delete the profile at the same time.

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

Sidebar

Related Questions

I have the following django model with an ImageField: class Pics(models.Model): def file_path(instance, filename):
I have the following in tests.py. def setUp(self): self.client = Client() self.client.get('/homepage',{'join':'NPO2','siteid':1450}) self.client.session.save() self.oraganisation_list
I have the following Django and Flex code: Django class Author(models.Model): name = models.CharField(max_length=30)
I have the following abstract Django models: class Food(models.Model): name = models.CharField(max_length=100) class Meta:
In my django views i have the following def create(request): query=header.objects.filter(id=a)[0] a=query.criteria_set.all() logging.debug(a.details) I
Suppose I have the following Event model: from django.db import models import datetime class
I have the following Django models: Bar, Bistro and Restaurant Each of the above
I have the following models in my Django app. How can I from the
I am following the django tutorial here . I have copied everything exactly. After
I have the following models: http://slexy.org/view/s20T8yOiKZ from mxutils.cms_services import generate_secid from django.db import models

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.