What approach(es) have you used for lightweight Python unit-tests on App Engine?

后端 未结 6 1873
借酒劲吻你
借酒劲吻你 2021-01-30 02:16

I\'m about to embark on some large Python-based App Engine projects, and I think I should check with Stack Overflow\'s \"wisdom of crowds\" before committing to a unit-testing s

6条回答
  •  [愿得一人]
    2021-01-30 02:53

    I might also add that Fixture has been very useful in my unit tests. It lets you create models in a declarative syntax, which it converts into stored entities that you can load in your tests. This way you have the same data set at the beginning of every test case!, which saves you from having to create data by hand at the start of every test. Here is an example, from the Fixture documentation: Given this model:

    from google.appengine.ext import db
    
    class Entry(db.Model):
        title = db.StringProperty()
        body = db.TextProperty()
        added_on = db.DateTimeProperty(auto_now_add=True)
    

    Your fixture would look like this:

    from fixture import DataSet
    
    class EntryData(DataSet):
        class great_monday:
            title = "Monday Was Great"
            body = """\
    Monday was the best day ever.
    """
    

    Note however, that I ran into the following issues: 1. This bug, but the included patch does remedy it. 2. The datastore is not -by default- reset between test cases. So I use this to force a reset for each test case:

    class TycoonTest(unittest.TestCase):
        def setUp(self):
            # Clear out the datastore before starting the test.
            apiproxy_stub_map.apiproxy._APIProxyStubMap__stub_map['datastore_v3'].Clear()    
            self.data = self.load_data()
            self.data.setup()
            os.environ['SERVER_NAME'] = "dev_appserver"
            self.after_setUp()
    
        def load_data(self):
            return datafixture.data(*dset.__all__)
    
        def after_setUp(self):
            """ After setup
            """
            pass
    
        def tearDown(self):
            # Teardown data.
            try:
                self.data.teardown()
            except:
                pass
    

提交回复
热议问题