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

  • Home
  • SEARCH
  • 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 8323897
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T23:52:06+00:00 2026-06-08T23:52:06+00:00

With two models: class Model1(models.Model): attribute = models.CharField(max_length=100) class Model2(models.Model): target = models.ForeignKey(Model1) And

  • 0

With two models:

class Model1(models.Model):
    attribute = models.CharField(max_length=100)

class Model2(models.Model):
    target = models.ForeignKey(Model1)

And a unittest tests.py:

class Model1Test(TestCase):
    def test_make_new_model_and_save():
        m = Model1()
        m.attribute = 'test values'
        m.save()

class Model2Test(TestCase):
    def test_make_new_model_and_save():
        # Make new Model1 to FK to
        m = Model1()
        m.attribute = 'test values'
        m.save()   

        m2 = Model2()
        m2.target = m
        m2.save()

I would like to just call the unittest function defined within Model1’s test class, rather than duplicating the code again in order to test Model2. This I imagine to be like:

class Model1Test(TestCase):
    def test_make_new_model_and_save():
        m = Model1()
        m.attribute = 'test values'
        m.save()
        return m

class Model2Test(TestCase):
    def test_make_new_model_and_save():
        # Make new Model1 to FK to
        m = Model1Test('test_make_new_model_and_save')

        m2 = Model2()
        m2.target = m
        m2.save()

But this doesn’t seem to work and I immediately find myself in the internals of the unit testing framework. I can’t find many references to this sort of thing, so being new to unit testing I feel I’ve taken a wrong turn somewhere.

Is there ‘normal’ method of reusing unit tests, or is my approach flawed?

  • 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-08T23:52:08+00:00Added an answer on June 8, 2026 at 11:52 pm

    This is really just a question about objects and classes in Python. The answer is to define a common superclass that contains the initialization code you want, inherit both Model1Test and Model2Test from it, and call it from both.

    class CommonSetUp(TestCase):
        def make_new_model_and_save():
            m = Model1()
            m.attribute = 'test values'
            m.save()
            return m
    
    class Model2Test(CommonSetUp):
        def test_make_new_model_and_save():
            self.make_new_model_and_save()
            self.assertTrue(...your assertions here...)
    
    class Model2Test(CommonSetUp):
        def test_make_new_model_and_save():
            self.make_new_model_and_save()
    
            m2 = Model2()
            m2.target = m
            m2.save()
    
            self.assertTrue(...your assertions here...)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two models: class Contact(models.Model): name = models.CharField(max_length=255) class Campaign(models.Model): contact = models.ForeignKey(Contact,
For example. class One(models.Model): text=models.CharField(max_length=100) class Two(models.Model): test = models.Integer() many = models.ManyToManyField(One, blank=True)
I have two models: class Actor(models.Model): name = models.CharField(max_length=30, unique = True) event =
In Django, I have two models: class Product(models.Model): name = models.CharField(max_length = 50) categories
I have the following model: class A(models.Model): name = models.CharField(max_length=50) content_type = models.ForeignKey(ContentType) This
I've got two models like these: class Schedule(models.Model): name = models.CharField(_('name'), blank=True, max_length=15) class
I have two models : class Album(models.Model): #Attributes title = models.CharField(max_length=200) displayed = models.BooleanField()
I have two models: class Customer(models.Model): (...) class CustomerMemo(models.Model): (...) customer = models.ForeignKey(Customer) text
I have two models like this: class ClassA(models.Model): ida = models.AutoField(primary_key=True) classb = models.ForeignKey(ClassB)
I have two models Profile and Avatar. models.py class Profile(models.Model): user = models.ForeignKey(User) profile_name

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.