![]() Nose provides extra tools for testing.We can override any default value by specifying it when a new instance is created.But as balkon does not have any default value, we need to specify it when we create the new instance. By default, HouseFactory creates a house in Barcelona with 3 rooms.In the test file, we create the class HouseFactory, which creates instances of a House every time is called.We create a model with 3 different mandatory fields: location, rooms and balkon.House2 = HouseFactory.create(location="Hannover", balkon=False) Self.house1 = HouseFactory.create(balkon=True) Next, we create the tests for that model:Ĭlass HouseFactory(factory.DjangoModelFactory): Return _("House located at %s") % self.location Help_text=_("Enter if the house has a balkon or not"), Help_text=_("Enter the number of rooms of the House"), Rooms = models.PositiveSmallIntegerField( Help_text=_("Enter the location of the house"), Now, let’s look an example of a test that uses some Nose tools and a Factory Model! First, we create a model in models.py:įile: marinamele_models_tests_nose_factory.pyįrom import ugettext_lazy as _ One alternative they recommend is the library factory_boy, which helps in the creation of models in your tests. Now your tests will run faster! Factory BoyĪs recommended by the 2 Scoops of Django book (amazing Best practices book!) fixtures are hard to maintain as the project evolves and data structures change. To use this method, open the settings.py file you use for testing and add: With this method, when the database is created, instead of running manage.py migrate in the apps managed by South, it runs directly manage.py syncdb. Disable South migrations in your tests.You can find more information in this post. With this method, you can create a database for testing, and instead of deleting it when all the tests are done, it remains there to be reused every time the tests are run. Create and reuse a database for testing.This can take some time, so if you want to prevent this from happening, there are two different ways: You’ll see that every time you run the tests, a new database is created and then all the South fixtures are applied. If you change the verbosity displayed in your tests, by adding -v 3 Run your tests again and you’ll see that the migrations don’t show in the coverage report □ In order to omit these files, create a file in the root directory (at the same level of manage.py) named. However, if you are using South, this report includes all your migration files, and your overall cover score will be affected. Only the packages myapp1 and myapp2 will be tested, and you’ll see the corresponding coverage report. And the third option, –cover-inclusive, indicates coverage to scan all the files in the working directories (this is useful to find files that are not being tested). In this case we cover two apps, myapp1 and myapp2 (these names are the ones you use in INSTALLED_APPS). The second option, –cover-package indicates the packages to cover with coverage. If you want to run your tests with this default behavior, open your test settings file and add the following: It is nicely integrated with django-nose, so to install it, just run: This library is an amazing tool that tells you which parts of your code are covered by tests. If you get an error like “permission denied to create database” check this post on Django testing. Save and make sure everything is installed properly by running TEST_RUNNER = ‘django_nose.NoseTestSuiteRunner’ In case you have a testing settings file that inherits from another settings file that contains all your installed apps, you can add this package by:įinaly, you have to indicate Django to use the Nose test runner. This way, you make sure that the django_nose’s test command is used. ![]() This is because South installs its own test command that turns off migrations during testing. Note: if you use South, make sure you include it before django_nose in the installed apps. Then, add django-nose to the installed apps in your testing settings.py file:
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |