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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T19:36:06+00:00 2026-06-09T19:36:06+00:00

Or, How to design your Database Schema for easy Unit testing? By the way,

  • 0

Or, “How to design your Database Schema for easy Unit testing?”

By the way, there is a very similar question to this here:
How to test Models in Django with Foreign Keys

I’m trying to follow TDD methodology for a project that uses the framework Django. I’m creating and testing models and its functionality (save methods, signals,…)
and other high level functions that relies on the models.

I understand that unit testing must be as isolated as possible but I find myself
creating a lot of tables and relations using FactoryBoy for each test, so my test is not strong enough because if something changes in a model many tests could be broken.

How to avoid all these dependencies and make the test cleaner?

What do you guys recommend to avoid all that boilerplate before the actual test?

What are the best practices?

  • 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-09T19:36:08+00:00Added an answer on June 9, 2026 at 7:36 pm

    There is no list of best practices for testing, it’s a lot of what works for you and the particular project you’re working on. I agree with pyriku when he says:

    You should not design your software basing on how you want to test it

    But, I would add that if you have a good and modular software design, it should be easy to test properly.

    I’ve been recently a little into unit testing in my job, and I’ve found some interesting and useful tools in Python, FactoryBoy is one of those tools, instead of preparing a lot of objects in the setUp() method of your test class, you can just define a factory for each model and generate them in bulk if needed.

    You can also try Mocker, it’s a library to mock objects and, since in Python everything is an object, you can mock functions too, it is useful if you need a test a function that generates X event at a certain time of the day, for example, send a message at 10:00am, you write a mock of datetime.datetime.now() that always returns ’10:00am’ and call that function with that mock.

    If you also need to test some front-end or your test needs some human interaction (like when doing OAuth against ), you have those forms filled and submitted by using Selenium.

    In your case, to prepare objects with relations with FactoryBoy, you can try to overwrite the Factory._prepare() method, let’s do it with this simple django model:

    class Group(models.Model):
        name = models.CharField(max_length=128)
        members = models.ManyToManyField(User, blank=True, null=True)
    
        # ...
    

    Now, let’s define a simple UserFactory:

    class UserFactory(factory.Factory):
        FACTORY_FOR = User
    
        first_name = 'Foo'
        last_name = factory.Sequence(lambda n: 'Bar%s' % n)
        username = factory.LazzyAttribute(lambda obj: '%s.%s' % (obj.first_name, obj.last_name))
    

    Now, let’s say that I want or need that my factory generates groups with 5 members, the GroupFactory should look like this

    class GroupFactory(factory.Factory):
        FACTORY_FOR = Group
    
        name = factory.Sequence(lambda n: 'Test Group %s' % n)
    
        @classmethod
        def _prepare(cls, create, **kwargs):
            group = super(GroupFactory, cls)._prepare(create, **kwargs)
            for _ in range(5):
                group.members.add(UserFactory())
            return group
    

    Hope this helps, or at least gave you a light. Here I’ll leave some links to resources related with the tools I mentioned:

    Factory Boy: https://github.com/rbarrois/factory_boy

    Mocker: http://niemeyer.net/mocker

    Selenium: http://selenium-python.readthedocs.org/en/latest/index.html

    And another useful thread about testing:

    What are the best practices for testing "different layers" in Django?

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

Sidebar

Related Questions

I want to know is there any software design the schema of oracle database?I
Hi there I have a short question about database design. I also tried the
This IS NOT a Homework question! While building my current student database project I
This is a more a philosophical question about Apple's design decisions than a question
I have a naive question about OOP design patterns. This is over-architecture, but I
Looking for a scalable, flexible and fast database design for 'Build your own form'
I'm struggling to design a database schema that represents opening hours for buildings and
When developing a site with a responsive CSS design, your site changes the way
I have this database design: **users_tbl** id username name **posts_tbl** id url users_id *FK
Being stuck with a legacy database schema that no longer reflects your data model

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.