django-testing

Factory Boy random choice for a field with field option "choices"

Factory Boy random choice for a field with field option "choices" Question: When a field in a Django model has the option choices, see Django choices field option, it utilises an iterable containing iterables of 2 items to define which values are allowed. For example: Models class IceCreamProduct(models.Model): PRODUCT_TYPES = ( (0, ‘Soft Ice Cream’), …

Total answers: 6

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

How to use Django's assertJSONEqual to verify response of view returning JsonResponse Question: I’m using Python 3.4 and Django 1.7. I have a view returning JsonResponse. def add_item_to_collection(request): #(…) return JsonResponse({‘status’:’success’}) I want to verify if that view returns correct response using unit test: 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(response.content, {‘status’: …

Total answers: 2

Django tests – patch object in all tests

Django tests – patch object in all tests Question: I need to create some kind of MockMixin for my tests. It should include mocks for everything that calls external sources. For example, each time I save model in admin panel I call some remote URLs. It would be good, to have that mocked and use …

Total answers: 3

How do I test Django QuerySets are equal?

How do I test Django QuerySets are equal? Question: I am trying to test my Django views. This view passes a QuerySet to the template: def merchant_home(request, slug): merchant = Merchant.objects.get(slug=slug) product_list = merchant.products.all() return render_to_response(‘merchant_home.html’, {‘merchant’: merchant, ‘product_list’: product_list}, context_instance=RequestContext(request)) and test: def test(self): “Merchant home view should send merchant and merchant products to …

Total answers: 6

Django : Testing if the page has redirected to the desired url

Django : Testing if the page has redirected to the desired url Question: In my django app, I have an authentication system. So, If I do not log in and try to access some profile’s personal info, I get redirected to a login page. Now, I need to write a test case for this. The …

Total answers: 5

how to get request object in django unit testing?

how to get request object in django unit testing? Question: I have a function as def getEvents(eid, request): …… Now I want to write unit test for the above function separately (without calling the view). So how should I call the above in TestCase. Is it possible to create request ? Asked By: user1003121 || …

Total answers: 5

Writing test cases for django models

Writing test cases for django models Question: Half way through my current project, after suffering the pain of spending uncountable minutes on debugging, I have decided to adopt TDD. To start, I am planning to write a set of unit tests for each existing models. But for models that only have attributes defined (ie. no …

Total answers: 2

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

Mocking a Django Queryset in order to test a function that takes a queryset Question: I have a utility function in my Django project, it takes a queryset, gets some data from it and returns a result. I’d like to write some tests for this function. Is there anyway to ‘mock’ a QuerySet? I’d like …

Total answers: 9

How should I write tests for Forms in Django?

How should I write tests for Forms in Django? Question: I’d like to simulate requests to my views in Django when I’m writing tests. This is mainly to test the forms. Here’s a snippet of a simple test request: from django.tests import TestCase class MyTests(TestCase): def test_forms(self): response = self.client.post(“/my/form/”, {‘something’:’something’}) self.assertEqual(response.status_code, 200) # we …

Total answers: 3