简体   繁体   中英

Wrapper for cloud NDB context when unit testing?

I'm migrating a Python 2 GAE app to Python 3, and I'm in the process of replacing ndb with cloud-ndb. This involves changing lines like

do_something_with_ndb()

to

with client.context() as context:
    do_something_with_ndb()

For Flask apps, you can use WSGI middleware (see here ) to handle this automatically, which is pretty awesome.

Is it possible to do something similar for unit tests? It is tedious to add the context throughout my test code.

Looking for solutions for both nose and pytest since I have both.

After going through the exercise of adding with client.context() as context: throughout my testing code, I've come to the conclusion that you probably don't want to have any kind of automatic wrapping of the context in your testing code.

With unit tests, they are generally done outside of the Flask app so you'll want to wrap each unit text in a context.

With route (or handler) tests, much of the processing is within the Flask app so you can't wrap the the test in a context (double wrapping raises an error).

But, with both unit and route tests, there are a lot of edge cases with setting up the tests and other things. You thus need flexibility to add the context as needed and a global wrapper just wouldn't work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM