There are several hooks we can utilise, but for our example, we’re going to focus on: We can use our environment.py file to define what should happen before and after certain points in our tests. Major_tom = UserFactory ( first_name = 'Major', last_name = 'Tom' ) # Create a User with Interestsĭjango = InterestFactory ( name = 'Django' ) public_speaking = InterestFactory ( name = 'Public Speaking' ) lucy_diamond = UserFactory ( first_name = 'Lucy', last_name = 'Diamond', interests = ( django, public_speaking )) Configuring environment.py User = UserFactory () # Will generate a user with the name 'Standard User' ![]() ![]() For example, we can: # Create a User with the default settings Voila! Now we’re all set to create objects in our tests. If we now create two instances of InterestFactory, they will each have a unique name - the first will be interest1, the second interest2.įinally, to define the many-to-many relationship between User and Interest, we need to setup our interests as a method using the post_generation hook. In our example, all of our users will have the first name of ‘Standard’ unless we specify otherwise.įor the email field (in our UserFactory) and name field (in our InterestFactory), we can use a factory sequence, so that each object in our factory is unique. Next, we define defaults for the corresponding model fields. format ( n ))įirst, we define the model we want to instantiate by setting the model inside the class Meta block. DjangoModelFactory ): class Meta : model = Interest name = factory. add ( interest ) class InterestFactory ( factory. """ if not create : return if extracted : for interest in extracted : self. Where 'interests' are defined, add them to this user. post_generation def interests ( self, create, extracted, ** kwargs ): """ format ( n )) password = make_password ( 'pass' ) is_active = True factory. """ class Meta : model = User first_name = 'Standard' last_name = 'User' # Emails must be unique - so use a sequence here:Įmail = factory. models import Interest, User class UserFactory ( factory. Models.py # Our User and Interest models live hereįactories.py # This is where we'll create our Factory Boy factoriesįactories.py import factory from import make_password from. We’ll setup our factories in the same application that our User and Interest models are defined: project_root/ To create these, we’ll use Factory Boy - a features replacement tool. Setting Up Our Test Environment Creating Factoriesīecause we’ve already written our feature file, we know that we’ll need Users and Interests in the database to run our test scenarios. Remember to add bdd to your INSTALLED_APPS in your settings.py file. However, utilising one central bdd application allows us to share the same environment for all of our tests, whilst accounting for situations where individual tests cases span multiple Django applications. We could instead include feature folders inside individual existing Django applications. Factory Boy will allow us to generate Users and Interests to use in our tests.Īfter installing all of the above, update settings.py:.Splinter sits on top of PhantomJS (and others) and will help us write simpler, more elegant test code. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |