0
def login_user(request):
    if request.method == "POST":
        username = request.POST['username'] #this is my first field to be tested
        password = request.POST['password']  #this is my second field to be tested
        user = authenticate(username=username, password=password)
        if user is not None:
            if user.is_active:
                login(request, user)
                playlists = Playlist.objects.filter(user=request.user)
                return render(request, 'index.html', {'playlists': playlists})
        else:
            return render(request, 'login.html', {'error_message': 'Invalid login'})
    return render(request, 'login.html')

我如何在 django 中测试这段代码以及我需要安装什么才能测试它

4

1 回答 1

1

如果要测试整个视图,可以查看:

https://docs.djangoproject.com/en/1.10/topics/testing/advanced/

基本上,您可以编写一个测试,在该视图上使用 POST 方法发送一个虚拟请求,然后通过检查响应进行验证。

于 2017-04-05T05:15:34.740 回答