Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9091831
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T22:42:12+00:00 2026-06-16T22:42:12+00:00

I have a flask application that is setting up a database connection in a

  • 0

I have a flask application that is setting up a database connection in a before_filter, very similar to this:

@app.before_request
def before_request():
    g.db = connect_db()

Now: I am writing some unit-tests and I do not want them to hit the database. I want to replace g.db with a mock object that I can set expectations on.

My tests are using app.test_client(), as is demonstrated in the flask documentation here. An example test looks something like

def test(self):
    response = app.test_client().post('/endpoint', data={..})
    self.assertEqual(response.status_code, 200)
    ...

The tests work and pass, but they are hitting the database and as I said I want to replace db access with mock objects. I do not see any way in test_client to access the g object or alter the before_filters.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-16T22:42:13+00:00Added an answer on June 16, 2026 at 10:42 pm

    This works

    test_app.py

    from flask import Flask, g
    
    app = Flask(__name__)
    
    def connect_db():
        print 'I ended up inside the actual function'
        return object()
    
    @app.before_request
    def before_request():
        g.db = connect_db()
    
    
    @app.route('/')
    def root():
        return 'Hello, World'
    

    test.py

    from mock import patch
    import unittest
    
    from test_app import app
    
    
    def not_a_db_hit():
        print 'I did not hit the db'
    
    class FlaskTest(unittest.TestCase):
    
        @patch('test_app.connect_db')
        def test_root(self, mock_connect_db):
            mock_connect_db.side_effect = not_a_db_hit
            response = app.test_client().get('/')
            self.assertEqual(response.status_code, 200)
    
    if __name__ == '__main__':
        unittest.main()
    

    So this will print out ‘I did not hit the db’, rather than ‘I ended up inside the actual function’. Obviously you’ll need to adapt the mocks to your actual use case.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that attempts to get locations settings from the browser, this
I have a Flask application that uses WTForms for user input. It uses a
I have this devise routes in routes.rb file: app::Application.routes.draw do scope :locale, locale: /#{I18n.available_locales.join(|)}/
I have an projector file/Flash application that I need to turn into an interactive
Imagine that I have a form in a flash application with two fields, input1
I have a web application which is deployed by the means of Flask. I'm
I have made a flash application and in this flash application I have included
I have developed a Facebook application using Flash as3. In this application when ever
we have a small flash component on our website/application to upload multiple files. This
I have seen a couple similar questions about this, however none of them work

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.