Django test FileField using test fixtures

Django provides a great way to write tests on FileFields without mucking about in the real filesystem – use a SimpleUploadedFile. from django.core.files.uploadedfile import SimpleUploadedFile my_model.file_field = SimpleUploadedFile(‘best_file_eva.txt’, b’these are the contents of the txt file’) It’s one of django’s magical features-that-don’t-show-up-in-the-docs :). However it is referred to here.

pytest and Failed: Database access not allowed, use the “django_db” mark, or the “db” or “transactional_db” fixtures to enable it

Solution: import pytest @pytest.mark.django_db class TestExample: def test_one(): … Assume that you’ve created a TestExample class inside your test file and it should be decorated with @pytest.mark.django_db. It should solve your problem.

Specify Django Test Database names in settings.py

In Django 1.6 and below, TEST_NAME should be a key of one of your database dictionaries. But in Django 1.7 and above, you use a TEST key which is a dictionary of settings for test databases. You probably want: DATABASES = { ‘default’:{ ‘ENGINE’:’mysql’, ‘NAME’:’testsqldb’, ‘USER’:'<username>’, ‘PASSWORD’:'<password>’, ‘TEST’: { ‘NAME’: ‘auto_tests’, } }, ‘dynamic_data’:{ ‘ENGINE’: … Read more

How to use Django’s assertJSONEqual to verify response of view returning JsonResponse

It looks like you’re working with Python 3 so you’ll need to turn response.content into a UTF-8 encoded string before passing it to self.assertJSONEqual: class AddItemToCollectionTest(TestCase): def test_success_when_not_added_before(self): response = self.client.post(‘/add-item-to-collection’) self.assertEqual(response.status_code, 200) self.assertJSONEqual( str(response.content, encoding=’utf8′), {‘status’: ‘success’} ) If you want to simultaneously support both Python 2.7 and Python 3, use the six compatibility … Read more

Mocking a Django Queryset in order to test a function that takes a queryset

For an empty Queryset, I’d go simply for using none as keithhackbarth has already stated. However, to mock a Queryset that will return a list of values, I prefer to use a Mock with a spec of the Model’s manager. As an example (Python 2.7 style – I’ve used the external Mock library), here’s a … Read more

How to test custom template tags in Django?

This is a short passage of one of my test files, where self.render_template a simple helper method in the TestCase is: rendered = self.render_template( ‘{% load templatequery %}’ ‘{% displayquery django_templatequery.KeyValue all() with “list.html” %}’ ) self.assertEqual(rendered,”foo=0\nbar=50\nspam=100\negg=200\n”) self.assertRaises( template.TemplateSyntaxError, self.render_template, ‘{% load templatequery %}’ ‘{% displayquery django_templatequery.KeyValue all() notwith “list.html” %}’ ) It is very … Read more

How do I modify the session in the Django test framework

The client object of the django testing framework makes possible to touch the session. Look at http://docs.djangoproject.com/en/dev/topics/testing/?from=olddocs#django.test.client.Client.session for details Be careful : To modify the session and then save it, it must be stored in a variable first (because a new SessionStore is created every time this property is accessed) I think something like this … Read more

What are the best practices for testing “different layers” in Django? [closed]

UPDATE 08-07-2012 I can tell you my practices for unit testing that are working pretty well for my own ends and I’ll give you my reasons: 1.- Use Fixtures only for information that is necessary for testing but is not going to change, for example, you need a user for every test you do so … Read more

What are the differences between setUpClass, setUpTestData and setUp in TestCase class?

EDIT: Update/Correction after Alasdair’s comment setUpClass setUpClass is used to perform class-wide initialization/configuration (e.g. creating connections, loading webdrivers). When using setUpClass for instance to open database connection/session you can use tearDownClass to close them. setUpClass is called once for the TestCase before running any of the tests. Similarly tearDownClass is called after all the tests … Read more