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