导入应用程序引擎应用程序父目录进行单元测试
我确信这很容易解决,但我已经盯着它看了太久,以至于我直接盯着解决方案。任何提示/提示/建议/解决方案表示赞赏!
我正在尝试向我的应用程序引擎应用程序添加一些单元测试。我使用 http://code.google 中的 testrunner.py 示例。 com/appengine/docs/python/tools/localunittesting.html(页面底部)。如果我将单元测试文件(名为“test_lib.py”)放在应用程序(myapp)的根目录中,则效果很好,但是我想将测试移动到应用程序内的单独子目录(名为“tests”)。现在我需要从应用程序导入一些模块,但是由于unittest文件的工作目录现在更深了一层,它看不到我的应用程序引擎应用程序中的实际模块。
我尝试将 __ init__.py 添加到测试中,并向其中添加以下代码:
import os
import sys
sys.path.append(os.path.dirname(os.getcwd()))
我希望这能找到当前工作目录并向上一级并将其添加到 sys.path 中,这样我就能够在单元测试文件('test_lib.py')中导入'util.lib'。但是,当我运行 testrunner.py 时,我仍然收到错误“ImportError:没有名为 util.lib 的模块”->我在这里尝试在根“myapp”内名为 util 的子目录中导入名为 lib 的模块。我的目录结构如下:
testrunner.py
|- myapp
|- __init__.py
|- util
|- __init__.py
|- lib.py
|- tests
|- __init__.py ## this file has the import mentioned above.
|- test_lib.py
我还尝试将应用程序根目录的导入添加到测试运行程序中,但这会返回相同的错误。
def main(sdk_path, test_path):
sys.path.append(os.path.dirname(test_path)) ## This line I added to the testrunner.
sys.path.insert(0, sdk_path)
import dev_appserver
dev_appserver.fix_sys_path()
suite = unittest2.loader.TestLoader().discover(test_path)
unittest2.TextTestRunner(verbosity=2).run(suite)
我使用以下命令调用测试:
./testrunner.py ~/sdk/google_appengine/ myapp/tests/
我在这里缺少什么建议吗?
I am sure this is simple to solve, but I have been staring at it for so long that I am staring straight past the solution. Any tips/hints/suggestions/solutions appreciated!
I am trying to add some unittests to my app engine app. I am using the testrunner.py example from http://code.google.com/appengine/docs/python/tools/localunittesting.html (bottom of the page). This works fine if I put the unittest file (named 'test_lib.py') in the root of the app (myapp), however I want to move the tests to a separate subdirectory (named 'tests') within the app. Now I need to import some of the modules from the app, however since the working directory of the unittest file is now one level deeper, it does not sees the actual modules from my app engine app.
I tried adding a __ init__.py to the tests and added the following code to the it:
import os
import sys
sys.path.append(os.path.dirname(os.getcwd()))
I hoped this would find the current working directory and go one level up and add this to the sys.path and from that I would be able to import 'util.lib' in the unittest file ('test_lib.py'). However when I run the testrunner.py I still get error "ImportError: No module named util.lib" -> I am trying here to import a module called lib within a subdirectory called util within the root 'myapp'. My directory structure is as follows:
testrunner.py
|- myapp
|- __init__.py
|- util
|- __init__.py
|- lib.py
|- tests
|- __init__.py ## this file has the import mentioned above.
|- test_lib.py
I also tried adding the import of the root of the app to the testrunner but this returns the same error.
def main(sdk_path, test_path):
sys.path.append(os.path.dirname(test_path)) ## This line I added to the testrunner.
sys.path.insert(0, sdk_path)
import dev_appserver
dev_appserver.fix_sys_path()
suite = unittest2.loader.TestLoader().discover(test_path)
unittest2.TextTestRunner(verbosity=2).run(suite)
And I am calling the test with the following command:
./testrunner.py ~/sdk/google_appengine/ myapp/tests/
Any suggestions what I am missing here?
您是否尝试过使用绝对路径?
Have you tried to use an absolute path?