【问题标题】:how to do unit testing for this?如何为此进行单元测试?
【发布时间】:2017-04-05 01:56:05
【问题描述】:
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 中测试此代码以及我需要安装什么才能测试它

【问题讨论】:

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


【解决方案1】:

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

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

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

【讨论】:

  • 你能给我自动化测试的示例代码吗?我想为登录页面执行 .... 它有两个字段(电子邮件和密码)以及两个按钮(登录和取消)
猜你喜欢
  • 2023-01-09
  • 2023-04-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2012-01-08
相关资源
最近更新 更多