我有很多简单的 Django 视图,如下所示:
@team_leader_required
def view_all_teams(request):
teams = Team.objects.all()
template_vars = {'toolbar': 'teams',
'teams': teams}
return render(request, "all_teams.html", template_vars)
我最终编写了很多形式的单元测试:
def test_view_all_teams_renders(self):
user = self.create_team_leader()
self.log_in(user)
response = self.client.get(reverse('all_teams'))
self.assertHttp200(response)
尽管我有创建用户的便捷方法(例如.create_team_leader
)和各种便捷断言(例如.assertHttp200
),但我的测试中仍然有很多重复。
(我的测试很简单,因为我看不到任何其他有用的东西可以断言这些视图 -TestCase.assertTemplateUsed
如果重命名模板会中断,即使视图是正确的。)
很容易错过测试,这让我在重命名模板时没有信心。有什么办法可以自动生成测试用例?类似(伪代码):
for every view in urls:
if view doesn't take extra arguments:
test that view returns 200 when a logged in superuser does a GET
编辑
这是我的 urls.py 中的一个代表性片段:
urlpatterns = patterns('',
url(r'^teams/$', 'teams.views.view_all_teams', name='all_teams'),
url(r'^teams/major/$', 'teams.views.view_major_teams', name='major_teams'),
url(r'^teams/minor/$', 'teams.views.view_minor_teams', name='minor_teams'),
url(r'^teams/(?P<team_id>\d+)/$', 'teams.views.view_team', name='view_team'),
url(r'^teams/(?P<team_id>\d+)/edit$', 'teams.views.edit_team', name='edit_team'),
url(r'^teams/(?P<team_id>\d+)/delete$', 'teams.views.delete_team', name='delete_team'),
我想自动测试此列表中的前三个视图。