469,282 Members | 1,715 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 469,282 developers. It's quick & easy.

Writing Django Unit Tests

I am relatively new to Django and very new to writing unit tests. I'd like to ask for assistance but I'm a bit stuck with where to even begin. The app I'm working with allows a teacher to assign multiple assignments to a student. On the student dashboard, an assignment should only be available if the start date <= today's date. The student should only see the first assignment in the list.

I need to compose a unit test to cover this scenario:
  • manually assign multiple assignments to a student
  • use the same query that is used for the student dashboard to ensure that the only assignments returned are the ones with a start date <= today's date
  • ensure that the student only sees the first assignment (with the earliest start date) in the list.
Below I have posted the relevant code that is pulling what displays on the student dashboard. Please let me know if additional code is needed to help me get started with this. Thanks very much for any help you can offer!

Note: I would like to only use the built in django.test features for now, if possible

from my home/views.py file

Expand|Select|Wrap|Line Numbers
  1. @login_required
  2. def index(request):
  3.     user_type = request.user.type.text
  4.     if user_type == 'Student':
  5.         """ Only return the first test so the student sees one test at a time"""
  6.  
  7.         assignment = Assignment.objects.filter(
  8.             student=request.user,
  9.             start_date__lte=datetime.date.today(),
  10.             completed=False).first()
  11.  
  12.         if (assignment):
  13.             context = {
  14.                 'test_pk': assignment.test.pk,
  15.             }
  16.         else:
  17.             context = {}
  18.         return render(request, 'home/student.html', context)
  19.  
  20.  
4 Weeks Ago #1
0 1795

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

1 post views Thread by Chris Burgess | last post: by
2 posts views Thread by Ben Finney | last post: by
6 posts views Thread by Ben Finney | last post: by
6 posts views Thread by Michael Bray | last post: by
7 posts views Thread by Andrew Ducker | last post: by
1 post views Thread by Richard Lewis Haggard | last post: by
5 posts views Thread by =?Utf-8?B?cmFuZHkxMjAw?= | last post: by
reply views Thread by zhoujie | last post: by
reply views Thread by suresh191 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.