Use the ModelMomMy Library to perform the steps and skills of unit testing
Use the ModelMomMy Library to perform the steps and skills of unit testing
Modelmommy is a Python test library used to generate a model instance to simplify the object creation process in the unit test.Here are the steps and skills of using the ModelMomMy Library for unit testing.
step:
1. Install the ModelMomMy Library: Install the ModelMomMMy Library by running the `PIP Install Modelmomommy` command in the terminal.
2. Import the required classes and functions: import the `Mommy` function and model class at the beginning of the test file.
3. Create test case: Write the test function and use the `Mommy.make` function to create a model instance.The field name of the model class can be used as the value of the field specified by the parameter.
4. Run test: Run the test by using the `python manage.py test` command in the terminal.Make sure to run in the right test environment.
Skill:
1. Facing data provided by Modelmommy's Faker: Modelmommy uses the Faker library to generate the field value of the model instance.Therefore, you can use forgery data to fill various fields of the model to create more authentic test cases.
2. Specific field values of custom model instance: `Mommy` Function allows the specific field value of the model instance generated by the definition of the parameter.Therefore, the specific value of the field of the model can be specified in the test function to meet the test needs.
3. Create an associated model instance: Modelmommy supports the creation of a model instance associated with other models.You can create an associated model instance by passing the fields of associated models in the `Mommy.Make` function.
4. Using other functions of Modelmommy: Modelmommy also provides other functions, such as automatic creation of data from multi -relationships, automatic generating files, and picture fields.You can check the documentation of Modelmommy as needed to understand more functions and usage.
Sample code and configuration:
Suppose we have a simple Django application, which contains a `user` model and a` userprofile` model.
python
# models.py
from django.db import models
from django.contrib.auth.models import User
class UserProfile(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
bio = models.TextField(blank=True)
age = models.PositiveIntegerField()
python
# tests.py
from django.test import TestCase
from model_mommy import mommy
from myapp.models import UserProfile
class UserProfileTestCase(TestCase):
def test_userprofile_creation(self):
# Use Modelmommy to create User model instance
user = mommy.make('auth.User')
# Use a specified field value to create an userprofile model instance
user_profile = mommy.make(UserProfile, user=user, age=25)
# Is the UserProfile instance generated whether it meets expectations?
self.assertEqual(user_profile.user, user)
self.assertEqual(user_profile.age, 25)
self.assertEqual(user_profile.bio, '')
The above example code demonstrates how to create and test the use of the Modelmommy class library to create and test the example of the model of the model.In this test case, we use the `Mommy.make` function to create an instance of a` User` model, and use this instance as the value of the outer key field of the `Userprofile` model.At the same time, we also specify a specific value for the `Age` field of the` Userprofile` model.Finally, we asserted the generated `Userprofile" instances to ensure that its field values are consistent with expectations.
Before running the test, make sure to correctly configure the Django application and test environment, including database settings, application registration and test configuration.For detailed Django configuration and test environment settings, please refer to the Django document.