Web2py 中的单元测试

发布于 2024-08-31 08:30:08 字数 1053 浏览 5 评论 0原文

我正在按照这篇文章中的说明进行操作,但无法让我的方法在全球范围内得到认可。

错误消息:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

禾厶谷欠 2024-09-07 08:30:08

请参阅: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'.

稳稳的幸福 2024-09-07 08:30:08

我对 web2py 没有任何经验,但经常使用其他框架。看着你的代码我有点困惑。应该使用 execfile 是否有客观原因?使用常规的 import 语句不是更好吗?因此,您可以编写以下代码,而不是 execfile

from applications.myapp.controllers.search import suggest_flavors

对于 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 of execfile you may write:

from applications.myapp.controllers.search import suggest_flavors

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文