Django Unittests客户端登录:在测试套件中失败,但不在Shell中 [英] Django Unittests Client Login: fails in test suite, but not in Shell

查看:79
本文介绍了Django Unittests客户端登录:在测试套件中失败,但不在Shell中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对我家庭视图进行基本测试。当从shell工作的日志记录客户端时,同一行代码在使用测试套件时无法登录客户端。



正确的方法是什么在使用Django测试套件时记录客户端?





任何想法为什么客户端没有使用我当前的方法登录?



Shell测试:

  import unittest 
from django.test.client import Client
from django.test.utils import setup_test_environment
setup_test_environment()

client = Client()
login = client.login(username ='agconti',password ='password')

#login - > evals to True

Django测试套件:

  ####查看测试#### 
import unittest
from django.test.client import Client
from shopping_cart.models import Store,Order ,项目,交易

class Home_ViewTest(unittest.TestCase):
def setUp(self):
self.client = Client()
#login user
login = self.client.login(username ='agconti',password ='password')
#检查登录
self.assertEqual(login,True)

def test_details(self):
#获取主视图
response = self.client.get('/')
self.assertEqual(response.status_code,200)

#检查渲染的上下文不是没有
self.assertTrue(response.context ['stores']!= None)

#self.assertEqual(login,True) - >登录evals为False,提出断言


解决方案

应该被创建如下所示:

  def setUp(self):
self.client = Client()
self。 username ='agconti'
self.email ='test@test.com'
self.password ='test'
self.test_user = User.objects.create_user(self.username,self $ e
login = self.client.login(username = self.username,password = self.password)
self.assertEqual(login,True)

在您的shell测试中,用户可能已经在您的数据库中,而在单元测试中可能没有用户...


I'm running a basic test of my home view. While logging the client in from the shell works, the same line of code fails to log the client in when using the test suite.

What is the correct way to log the client in when using the Django test suite?

Or

Any idea why the client is not logging in with my current method?

Shell test:

import unittest
from django.test.client import Client
from django.test.utils import setup_test_environment
setup_test_environment()

client = Client()
login = client.login(username='agconti', password='password')

# login --> evals to True

Django test suite:

#### View Tests ####
import unittest
from django.test.client import Client
from shopping_cart.models import Store, Order, Item, Transaction

class Home_ViewTest(unittest.TestCase):
    def setUp(self):
        self.client = Client()
        # login user
        login = self.client.login(username='agconti', password='password')
        # Check login
        self.assertEqual(login, True)

    def test_details(self):
        # get the home view
        response = self.client.get('/')      
        self.assertEqual(response.status_code, 200)

        # Check that the rendered context is not none 
        self.assertTrue(response.context['stores'] != None)

# self.assertEqual(login, True) --> login evals to False, raises the assertion

解决方案

A test user should be created. Something like:

  def setUp(self):
    self.client = Client()
    self.username = 'agconti'
    self.email = 'test@test.com'
    self.password = 'test'        
    self.test_user = User.objects.create_user(self.username, self.email, self.password)
    login = self.client.login(username=self.username, password=self.password)
    self.assertEqual(login, True)

In your shell test the user is probably already present in your db, whereas in your unittest there might not be a user...

这篇关于Django Unittests客户端登录:在测试套件中失败,但不在Shell中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆