Django Nose, how to write this test?

I am completely new to testing in Django. I started by installing nose and selenium , and now I want to test the following code (below). It sends an SMS message.

This is the actual code:

views.py

@login_required def process_all(request): """ I process the sending for a single or bulk message(s) to a group or single contact. :param request: """ #If we had a POST then get the request post values. if request.method == 'POST': batches = Batch.objects.for_user_pending(request.user) for batch in batches: ProcessRequests.delay(batch) batch.complete_update() return HttpResponseRedirect('/reports/messages/') 

So where to start? This is what I have done so far ...

1) created a folder called tests and added init .py.

2) created a new python file called test_views.py (I assume this is correct).

Now, how do I write this test?

Can someone show me an example of how I am writing a test for the presentation above?

Thanks:)

+7
source share
1 answer

First of all, you do not need selenium to test views. Selenium is a tool for high-level testing in a browser - it is good and useful when you write user interface tests that simulate a real user.

The nose is a tool that makes testing easier by providing features such as automatic test detection, a number of helper functions, etc. The best way to combine the nose with the django project is to use django_nose . All you have to do is:

  • add django_nose to INSTALLED_APPS
  • define TEST_RUNNER = 'django_nose.NoseTestSuiteRunner'

Then, every time you run the python manage.py test <project_name> nose, you will use your tests.


So, speaking of testing this particular kind, you should check:

  • login_required decorator operation - in other words, an unauthorized user will be redirected to the login page
  • if request.method not POST, messages are not sent + are redirected to /reports/messages
  • sending SMS messages using the POST method + redirecting to /reports/messages

Testing the first two statements is pretty straight forward, but to verify the last statement you need to provide more detailed information about what Batch , ProcessRequests and how it works. I mean that you probably don't want to send real SMS messages during testing - this will help mocking . Basically, you need to make fun of (replace with your own on-the-fly implementation) Batch , ProcessRequests objects. Here is an example of what you should have in test_views.py :

 from django.contrib.auth.models import User from django.core.urlresolvers import reverse from django.test.client import Client from django.test import TestCase class ProcessAllTestCase(TestCase): def setUp(self): self.client = Client() self.user = User.objects.create_user('john', ' lennon@thebeatles.com ', 'johnpassword') def test_login_required(self): response = self.client.get(reverse('process_all')) self.assertRedirects(response, '/login') def test_get_method(self): self.client.login(username='john', password='johnpassword') response = self.client.get(reverse('process_all')) self.assertRedirects(response, '/reports/messages') # assert no messages were sent def test_post_method(self): self.client.login(username='john', password='johnpassword') # add pending messages, mock sms sending? response = self.client.post(reverse('process_all')) self.assertRedirects(response, '/reports/messages') # assert that sms messages were sent 

See also:

Hope this helps.

+14
source

All Articles