Skip to content

Changing settings for unit tests #264

Closed
@quantoid

Description

@quantoid

The documentation really needs a brief section explaining how to inject different settings into the MongoEngine constructor for unit tests.

This package looks like it adds a bunch of good stuff on top of mongoengine for use in Flask apps, so I really want to use it, but I'm having trouble working out how to set things up for unit tests, which is a blocker if you're doing test-driven development.

Say you have a main module something like this:

app = Flask(__name__)
app.config.from_object(settings)
ddb = MongoEngine(app)
...

and unit tests like this:

from myapp.main import app

class APIAcceptanceTests(TestCase):
    def setup(self):
        self.client = app.test_client()

    def test_fruits(self):
         """API returns list of fruits"""
        reply = self.client.get('/api/fruits')
        self.assertEqual(200, response.status_code)

The trouble here is that the MongoEngine wrapper has already been created when you import app. That constructor calls create_connection which has special test-friendly behaviour when the app is configured in testing mode, so in unit tests you really want to change the configuration before calling it.

What's the neatest way to inject different settings from unit tests so the MongoEngine wrapper gets those?

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions