我在我的django應用上使用Google雲端點來提供RESTful服務。所有的API服務都運行良好。現在是編寫測試用例的時候了。所以我寫了這個。編寫Google雲端點的測試用例寫在django之上的API
TestPOIList.py
import unittest
from django.test import TestCase
import endpoints
import webtest
from google.appengine.ext import testbed
from .queries import CategoryQuery
from .queries import PointOfInterestQuery
from .models import PointOfInterest
from .api.internal.point_of_interest import PointOfInterests
class Test(TestCase):
def setUp(self):
self.testbed = testbed.Testbed()
self.testbed.setup_env(current_version_id='testbed.version')
self.testbed.activate()
self.testbed.init_all_stubs()
self.category_a = CategoryQuery.create_category_by_name('Museums')
self.poi = PointOfInterestQuery.create_new_poi(name='bhavan', category=self.category_a,
place='hdgf', phone='3454545', email='[email protected]'
)
app = endpoints.api_server([PointOfInterests], restricted=False)
self.testapp = webtest.TestApp(app)
def tearDown(self):
self.testbed.deactivate()
# Test the handler.
def test_should_return_list_of_pois(self):
msg = {}
resp = self.testapp.post_json('/_ah/spi/PointOfInterests.get_list', msg, status='*')
self.assertEqual(resp.status_code, 200)
self.assertEqual(resp.json['pois']['name'], 'Saravan bhavan')
我試圖與python manage.py test
,python manage.py test appname
運行上述代碼。但它不會工作。我也試過django-nose
。它也不能測試一個測試用例。輸出顯示運行後python manage.py test
是,
----------------------------------------------------------------------
Ran 0 tests in 0.000s
OK
Destroying test database for alias 'default'...
所以無法運行我的第一個測試用例。請提出一些建議。
編輯:
更改文件名testpoilist
找到測試用例,但它無法導入AppEngine上相關的模塊。並導致許多import
錯誤。所以,我想運行nosetests
但它表明,
$ nosetests -q -w src/ --with-gae
Traceback (most recent call last):
File "/home/gemini/.virtualenvs/sample/bin/nosetests", line 11, in <module>
sys.exit(run_exit())
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/core.py", line 121, in __init__
**extra_args)
File "/usr/lib/python2.7/unittest/main.py", line 94, in __init__
self.parseArgs(argv)
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/core.py", line 145, in parseArgs
self.config.configure(argv, doc=self.usage())
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/config.py", line 346, in configure
self.plugins.configure(options, self)
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/plugins/manager.py", line 284, in configure
cfg(options, config)
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/plugins/manager.py", line 99, in __call__
return self.call(*arg, **kw)
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/plugins/manager.py", line 167, in simple
result = meth(*arg, **kw)
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nosegae.py", line 85, in configure
import appengine_config
File "/home/gemini/projects/sample-backend/src/appengine_config.py", line 22, in <module>
vendor.add('lib')
File "/home/gemini/softwares/google_appengine/google/appengine/ext/vendor/__init__.py", line 44, in add
'No such virtualenv or site directory' % path)
ValueError: virtualenv: cannot access lib: No such virtualenv or site directory
編輯2: 改變vendor.add('lib')
在下面回答指定的東西后,我得到這個錯誤。
$ nosetests -q -w src/ --with-gae
Traceback (most recent call last):
File "/home/gemini/.virtualenvs/sample/bin/nosetests", line 11, in <module>
sys.exit(run_exit())
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/core.py", line 121, in __init__
**extra_args)
File "/usr/lib/python2.7/unittest/main.py", line 95, in __init__
self.runTests()
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/core.py", line 207, in runTests
result = self.testRunner.run(self.test)
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/core.py", line 62, in run
test(result)
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/suite.py", line 177, in __call__
return self.run(*arg, **kw)
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/suite.py", line 224, in run
test(orig)
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/suite.py", line 177, in __call__
return self.run(*arg, **kw)
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/suite.py", line 224, in run
test(orig)
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/suite.py", line 177, in __call__
return self.run(*arg, **kw)
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/suite.py", line 217, in run
for test in self._tests:
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/suite.py", line 372, in _get_wrapped_tests
for test in self._get_tests():
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/suite.py", line 99, in _get_tests
for test in self.test_generator:
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/loader.py", line 191, in loadTestsFromDir
entry_path, discovered=True)
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/loader.py", line 433, in loadTestsFromName
discovered=discovered)
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/loader.py", line 354, in loadTestsFromModule
tests.extend(self.loadTestsFromDir(module_path))
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/loader.py", line 191, in loadTestsFromDir
entry_path, discovered=True)
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/loader.py", line 433, in loadTestsFromName
discovered=discovered)
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/loader.py", line 354, in loadTestsFromModule
tests.extend(self.loadTestsFromDir(module_path))
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/loader.py", line 183, in loadTestsFromDir
entry_path, discovered=True)
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/loader.py", line 427, in loadTestsFromName
address=addr.totuple())])
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/suite.py", line 427, in __call__
return self.makeSuite(tests, context, **kw)
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/suite.py", line 482, in makeSuite
for ancestor in self.ancestry(context):
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/suite.py", line 453, in ancestry
yield resolve_name('.'.join(ancestors))
File "/home/gemini/.virtualenvs/sample/local/lib/python2.7/site-packages/nose/util.py", line 322, in resolve_name
obj = getattr(obj, part)
AttributeError: 'module' object has no attribute 'failure'
編輯3:
我也嘗試過使用運行Django的python manage.py test
測試。但它顯示,
ERROR: src.tests.api.internal.test_poi_list (unittest.loader.ModuleImportFailure)
----------------------------------------------------------------------
ImportError: Failed to import test module: src.tests.api.internal.test_poi_list
Traceback (most recent call last):
File "/usr/lib/python2.7/unittest/loader.py", line 254, in _find_tests
module = self._get_module_from_name(name)
File "/usr/lib/python2.7/unittest/loader.py", line 232, in _get_module_from_name
__import__(name)
File "/home/gemini/projects/backend/src/tests/api/internal/test_poi_list.py", line 9, in <module>
from guide.api.internal.point_of_interest import PointOfInterests
File "/home/gemini/projects/backend/src/cityguide/api/internal/point_of_interest.py", line 12, in <module>
from guide.api.common import GuideInternalAPI
File "/home/gemini/projects/backend/src/guide/api/common.py", line 9, in <module>
GuideInternalAPI = endpoints.api(name='GuideInternalAPI',
AttributeError: 'module' object has no attribute 'api'
----------------------------------------------------------------------
Ran 1 test in 0.000s
FAILED (errors=1)
似乎我幾乎接近。 PYTHONPATH
變量顯示google_appengine sdk路徑。
您是否有單獨的測試設置文件?而且,這個文件有這些測試的名稱是什麼? – AKS
不,只有一個settings.py文件。 –
Django默認使用'DiscoverRunner'來運行測試套件,它遵循'test * .py'模式來獲取包含單元測試的文件。你可以相應地更改文件名並再次測試嗎? – AKS