我遵循this post的指示,但無法在全局範圍內識別我的方法。Web2py中的單元測試
錯誤消息:
ERROR: test_suggest_performer (__builtin__.TestSearch)
----------------------------------------------------------------------
Traceback (most recent call last):
File "applications/myapp/tests/test_search.py", line 24, in test_suggest_performer
suggs = suggest_flavors("straw")
NameError: global name 'suggest_flavors' is not defined
我的測試文件:
import unittest
from gluon.globals import Request
db = test_db
execfile("applications/myapp/controllers/search.py", globals())
class TestSearch(unittest.TestCase):
def setUp(self):
request = Request()
def test_suggest_flavors(self):
suggs = suggest_flavors("straw")
self.assertEqual(len(suggs), 1)
self.assertEqual(suggs[0][1], 'Strawberry')
我的控制器:
def suggest_flavors(term):
return []
有沒有人成功地完成像這樣的web2py的單元測試?
謝謝!我忘記了我在模塊中有更深一層的真實邏輯。我用'myapp/search.py'代替'controllers/search.py'。 – 2010-05-04 23:09:21