Writing Django Unit Tests

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • kglaser89
    New Member
    • Sep 2021
    • 1

    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

    Code:
    @login_required
    def index(request):
        user_type = request.user.type.text
        if user_type == 'Student':
            """ Only return the first test so the student sees one test at a time"""
    
            assignment = Assignment.objects.filter(
                student=request.user,
                start_date__lte=datetime.date.today(),
                completed=False).first()
    
            if (assignment):
                context = {
                    'test_pk': assignment.test.pk,
                }
            else:
                context = {}
            return render(request, 'home/student.html', context)
Working...