当我在Django中为我的应用程序运行测试时,我有以下异常
.......NameError: global name 'STDOUT_LINE' is not defined
测试用例中只有9种测试方法。
可能导致此问题的原因可能是某人已经遇到过这类问题?
$ : ./manage.py test profile
/Users/sultan/.virtualenvs/yoda/lib/python2.7/site-packages/nose/util.py:14: DeprecationWarning: The compiler package is deprecated and removed in Python 3.x.
from compiler.consts import CO_GENERATOR
nosetests --verbosity 1 profile --verbosity=2 --with-snot --with-xtraceback --with-coverage --cover-package=profile
/Users/sultan/.virtualenvs/yoda/lib/python2.7/site-packages/django/db/backends/sqlite3/base.py:53: RuntimeWarning: SQLite received a naive datetime (2013-04-22 11:19:12.808338) while time zone support is active.
RuntimeWarning)
Creating test database for alias 'default'...
Test page to create order ... ok
Test with anonymous user, index page should give a login form ... ok
Test with anonymous user, index page should give a login form ... ok
Test drivers list ... ok
Test operators list ... ok
Test if login works ... ok
Test if user logged in as manager will be redirected to /manager page ... ok
Test if user logged in as operator will be redirected to /operator page ... NameError: global name 'STDOUT_LINE' is not defined
# -*- coding: utf-8 -*-
from django.test import TestCase
from django.core.urlresolvers import reverse
from profile.tests.factories import UserFactory
from profile.models import ROLE_OPERATOR, ROLE_USER
from profile.models import ROLE_ADMIN, ROLE_DRIVER, ROLE_MANAGER
class TestUrls(TestCase):
""" Test common project wide urls """
def setUp(self):
self.user = UserFactory.create(role=ROLE_USER)
self.admin = UserFactory.create(role=ROLE_ADMIN, is_superuser=True)
self.driver = UserFactory.create(role=ROLE_DRIVER)
self.manager = UserFactory.create(role=ROLE_MANAGER)
self.operator = UserFactory.create(role=ROLE_OPERATOR)
def get(self, url, follow=True):
return self.client.get(url, follow=follow)
def login(self):
self.client.login(username=self.manager.username, password='pass')
def test_index_page_login(self):
""" Test with anonymous user, index page should give a login form """
res = self.get(reverse('profile:index'))
self.assertContains(res, 'Login')
self.assertEqual(res.status_code, 200)
def test_login(self):
""" Test if login works """
res = self.get(reverse('profile:login'))
res2 = self.get(reverse('profile:login'), follow=False)
self.assertEqual(res.status_code, 200)
self.assertEqual(res2.status_code, 302)
def test_operator_redirect(self):
"""
Test if user logged in as operator
will be redirected to /operator page
"""
self.login()
res = self.get(reverse('profile:index'))
self.assertTrue('/operator' in res.request['PATH_INFO'])
def test_manager_redirect(self):
"""
Test if user logged in as manager
will be redirected to /manager page
"""
self.login()
res = self.get(reverse('profile:index'))
self.assertTrue('/manager' in res.request['PATH_INFO'])
def test_references_page(self):
""" Test references """
self.login()
res = self.get(reverse('profile:reference'))
self.assertEqual(res.status_code, 200)
self.assertTemplateUsed('profile/reference.html')
def test_list_drivers(self):
""" Test drivers list """
self.login()
res = self.get(reverse('profile:drivers'))
self.assertEqual(res.status_code, 200)
self.assertTemplateUsed('profile/driver_list.html')
def test_list_operators(self):
""" Test operators list """
self.login()
res = self.get(reverse('profile:operators'))
self.assertEqual(res.status_code, 200)
self.assertTemplateUsed('profile/operator_list.html')
def test_error500(self):
""" Test with anonymous user, index page should give a login form """
res = self.get(reverse('profile:error500'))
self.assertEqual(res.status_code, 200)
def test_create_order_page(self):
""" Test page to create order """
res = self.get(reverse('profile:new_order'), follow=False)
self.assertEqual(res.status_code, 302)
self.login()
res = self.get(reverse('profile:new_order'))
self.assertEqual(res.status_code, 200)
答案 0 :(得分:0)
问题解决了,因为
的断言失败了def test_operator_redirect(self):
""" Test if user logged in as operator will be redirected to /operator page """
self.login(self.operator)
res = self.get(reverse('taxi:index'))
self.assertTrue('/operator' in res.request['PATH_INFO'])
所以我更改了self.login
方法,但如果self.assertTrue
失败则会再次出现此错误
def login(self, user):
self.client.login(username=user.username, password='pass')