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 中成功完成了这样的单元测试吗?
I'm following the instructions from this post but cannot get my methods recognized globally.
The error message:
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
My test file:
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')
My controller:
def suggest_flavors(term):
return []
Has anyone successfully completed unit testing like this in web2py?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请参阅:http://web2py.com/AlterEgo/default/show/260
请注意,在您的示例中,函数“suggest_flavors”应在“applications/myapp/controllers/search.py”中定义。
Please see: http://web2py.com/AlterEgo/default/show/260
Note that in your example the function 'suggest_flavors' should be defined at 'applications/myapp/controllers/search.py'.
我对 web2py 没有任何经验,但经常使用其他框架。看着你的代码我有点困惑。应该使用 execfile 是否有客观原因?使用常规的 import 语句不是更好吗?因此,您可以编写以下代码,而不是
execfile
:对于 Python 开发人员来说,这是更清晰的代码。
请注意,在这种情况下,您应该将
__init__.py
放置在路径中的每个目录中,以便目录形成包/模块层次结构。I don't have any experience with web2py, but used other frameworks a lot. And looking at your code I'm confused a bit. Is there an objective reason why
execfile
should be used? Isn't it better to use regular import statement. So instead ofexecfile
you may write:It's more clear code for pythoners.
Note, that you should place
__init__.py
in each directory along the path in this case, so that dirs will form package/module hierarchy.