如何为此进行单元测试?

时间:2017-04-05 01:56:05

标签: django unit-testing automated-tests pytest pytest-django

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中对此代码进行测试以及为了测试它需要安装的所有内容

1 个答案:

答案 0 :(得分:1)

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

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

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