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 7657425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T13:01:54+00:00 2026-05-31T13:01:54+00:00

I have a python (django) web application. It uses an external web service (Facebook

  • 0

I have a python (django) web application. It uses an external web service (Facebook Graph). All the code for making external (http) calls is wrapped in one extra function (called facebook_api), which takes some arguments and returns a parsed dict (it does some logging, checks for errors etc.) Around this function, I have written some code. I want to do some unittests of this code.

How can I “mock” this one function with some known input? i.e. I want to provide some input & output for my facebook_api function (i.e. if you see this input, return this, and if it’s this, return that, etc.). I then want to check that my code handles it OK.

I want to provide various different scenarios for my code (what if the API started returning this, or that) and check that my code does what I want. I don’t want to actually make external (http) requests.

I tried looking at some mock libraries (e.g.http://www.voidspace.org.uk/python/mock/ ), but they always talk about creating class objects, or just checking that something was called. I can’t see easy ways to say “Intercept this function, and if the input is A, return X, if it’s like D, return B”

  • 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-05-31T13:01:56+00:00Added an answer on May 31, 2026 at 1:01 pm

    I think you are looking for Mock’s side_effect . For example

    def my_facebook_api(input):
        if input=='A':
            return 'X'
        elif input=='B':
            return 'D'
    
    facebook_api =  Mock(side_effect=my_facebook_api)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am writing a Python/Django web application that uses OAuth (for the TwitterAPI, not
Imagine you have a web application written in Django and Python 2.65, and MySQL
I've been developing a web application (written in Python/Django) for a while. All my
I have a Python web application (Django, specifically). I'm reading in some data from
I have a web application im currently working on in python. I'm using Django
I have a web application build in Django + Python that interact with web
I have built Web sites with Python/Django and desktop applications with Objective-C/Cocoa so programming
we have create a web application on top of google app engine and python.
I'm looking for some recommendations for a python web application. We have some memory
Immagine a web application written in python/django in which users authenticate with digital certificates

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.