import unittest
class MyTestCase(unittest.TestCase):
def setup()
import webtest # app from your webapp2.WSGIApplication() for example
self.testapp = webtest.TestApp(app)
self.testbed = testbed.Testbed()
self.testbed.activate()
# Create a consistency policy that will simulate the # High Replication consistency model.
# See https://cloud.google.com/appengine/docs/python/tools/localunittesting#Python_Writing_High_Replication_Datastore_tests
self.policy = datastore_stub_util.PseudoRandomHRConsistencyPolicy(probability=1)
# Initialize the datastore stub with this policy.
self.testbed.init_datastore_v3_stub(consistency_policy=self.policy)
# you eventually want to add more stubs
# ...
# Clear ndb's in-context cache between tests.
# This prevents data from leaking between tests.
# Alternatively, you could disable caching by
# using ndb.get_context().set_cache_policy(False)
# ndb.get_context().clear_cache()
ndb.get_context().set_cache_policy(False)
# If you need initial data in datastore for test, # add them here (probably better to use the original NDB lib)
def tearDown(self):
self.testbed.deactivate()
def testCreateRead(self):
"""Test the handler that create objects of entity kind 'Foo' and the handler that read Foo object with given ID.
POST request will return a JSON object which contains the created foo objects in the `results` array
GET request will return a JSON object which represents the retrieved foo object
"""
# POST request to produce 2 foo objects, each with title
payload = [
{'title': 'hello'},
{'title': 'world'}
]
response = self.testapp.post_json('/api/v1/foos/create', payload, headers={'Accept': 'application/json'}, status='*')
# assertions as in https://docs.python.org/2/library/unittest.html#test-cases
self.assertEqual(response.status_int, 201)
self.assertTrue('results' in response.json, 'response to create foos has no results array')
self.assertDictEqual(response.json, {'results': [{'id': 1, 'title': 'hello'}, {'id': 2, 'title': 'world'}]}, 'result in create foo response is wrong')
# you can also use NDB in tests
self.assertEqual(ndb.Query(kind='Foo').count(), 2, 'expected 2 foo created')
# GET request with JSON response
response = self.testapp.get('/api/v1/foo/1', headers={'Accept': 'application/json'}, status='*', xhr=True)
self.assertEqual(response.status_int, 200)
self.assertDictEqual(response.json, {'id': 1, 'title': 'hello'}, 'response to getting foo #1 is wrong')if __name__ == '__main__':
unittest.main()
--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengi...@googlegroups.com.
To post to this group, send email to google-a...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine/cd123e20-c5fc-4faa-8717-49ddd012bd22%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.
--
I'm really confused with the way to try Datastore in local. Please, give me a minute to explain.
import sys, os
import unittest
app_engine = r"C:\Program Files (x86)\Google\google_appengine"
sys.path.insert(1, app_engine)
sys.path.insert(1, os.path.join(app_engine, 'lib', 'yaml', 'lib'))
import dev_appserver
dev_appserver.fix_sys_path()
suite = unittest.TestLoader().discover('.')
unittest.TextTestRunner().run(suite)
import os
import unittest
from google.appengine.ext import testbed
class GaeTestCase(unittest.TestCase):
def setUp(self):
# First, create an instance of the Testbed class.
self.testbed = testbed.Testbed()
# Then activate the testbed, which prepares the service stubs for use.
self.testbed.activate()
# Next, declare which service stubs you want to use.
self.testbed.init_datastore_v3_stub() # it's need for datastore tests
self.testbed.init_memcache_stub()
self.testbed.init_app_identity_stub()
self.testbed.init_blobstore_stub()
self.testbed.init_files_stub()
self.testbed.init_taskqueue_stub(root_path="path_to_your_project")
self.taskqueue_stub = self.testbed.get_stub(testbed.TASKQUEUE_SERVICE_NAME)
self.testbed.init_urlfetch_stub()
self.testbed.init_user_stub()
def tearDown(self):
self.testbed.deactivate()
Anyway, I'm using testbed and it works fine, but I' ve found a problem, when I execute a test over the method that insert a student in the datastore all work fine, I check that the user is inserted. But after when I check other method that read specific user I see that I dont have entities that read. I don't understand why if both methods are in the same Test class and one is after other.
Any idea?
First for all,
Thanks thanks thanks you so much for all the help guys!
I think that I'm starting to understand. When I use the dev server he start much of services. But when I execute the test with testbed and datastore_v3_stub this stub emulated only the datastore. And if I was using other tools that i would connect to Cloud Datastore maybe I will need use the datastore emulator.
But in this case, why don't use the own development server which I use to run my app to test my lib (ndb wrapper) or anything ?
Anyway, I'm using testbed and it works fine, but I' ve found a problem, when I execute a test over the method that insert a student in the datastore all work fine, I check that the user is inserted. But after when I check other method that read specific user I see that I dont have entities that read. I don't understand why if both methods are in the same Test class and one is after other.
Any idea?
--
You received this message because you are subscribed to the Google Groups "Google App Engine" group.
To unsubscribe from this group and stop receiving emails from it, send an email to google-appengi...@googlegroups.com.
To post to this group, send email to google-a...@googlegroups.com.
Visit this group at https://groups.google.com/group/google-appengine.
To view this discussion on the web visit https://groups.google.com/d/msgid/google-appengine/cf9c0e4b-f00a-476b-aa37-2e0a064e6bea%40googlegroups.com.