문제

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?

도움이 되었습니까?

해결책

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'.

다른 팁

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top