Unit testing in Web2py

I am following the instructions in this post , but cannot recognize my methods globally.

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?

+2


a source to share


2 answers


See: http://web2py.com/AlterEgo/default/show/260



Note that in your example, the 'suggest_flavors' function must be defined in 'applications / myapp / controllers / search.py'.

+3


a source


I have no experience with web2py, but I am using other frameworks. And looking at your code I am a little confused. Is there an objective reason for using it execfile

? Isn't it better to use a regular import operation. So instead, execfile

you can write:

from applications.myapp.controllers.search import suggest_flavors

      



This is clearer code for pythoners.

Note that you have to put __init__.py

in each directory along the path in this case for the dirs to form a package / module hierarchy.

0


a source







All Articles