当我在 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
更新 url 测试
# -*- 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)