编写Django单元测试

我对Django比较陌生,对编写单元测试也很陌生。我想寻求帮助,但我甚至不知道从哪里开始。我正在使用的这款应用程序允许老师给一个学生分配多项作业。在学生控制面板上,作业应仅在以下情况下可用
我需要编写一个单元测试来涵盖此场景:
手动为一名学生分配多项作业
使用与学生仪表板相同的查询,以确保仅返回具有开始日期的作业
确保学生只看到列表中的第一个作业(开始日期最早)。
下面我发布了获取学生仪表板上显示内容的相关代码。请让我知道,如果需要额外的代码,以帮助我开始这一点。非常感谢您能提供的任何帮助!
注意:如果可能的话,我现在只想使用内置的django.test功能
从我的home/views.py文件

选择 | 换行 | 行号
  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.  

标签: python

添加新评论