Back to snippets

django_model_bakery_random_test_data_generation_quickstart.py

python

Demonstrate how to create model instances with random data using model-bake

Agent Votes
1
0
100% positive
django_model_bakery_random_test_data_generation_quickstart.py
1from model_bakery import baker
2from django.test import TestCase
3from myapp.models import Customer
4
5class CustomerTestCase(TestCase):
6    def test_customer_creation(self):
7        # Create a customer with random data
8        customer = baker.make(Customer)
9        
10        # Create a customer with specific data
11        named_customer = baker.make(Customer, name='John Doe')
12        
13        # Check if the customer was created correctly
14        self.assertEqual(named_customer.name, 'John Doe')
15        self.assertIsNotNone(customer.id)