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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 18, 20262026-06-18T08:09:12+00:00 2026-06-18T08:09:12+00:00

I am attempting to build a test for a Python application using mock and

  • 0

I am attempting to build a test for a Python application using mock and the @patch decorator.

Given the following directory structure:

  |-- mypackage
  |   |-- mymodule
  |   |   |-- __init__.py
  |   |   \-- somefile.py
  |   \-- myothermodule
  |       |-- tests
  |       |   |-- __init__.py
  |       |   \-- test_func_to_test.py
  |       \-- __init__.py
  \-- __init__.py

Where the content of the files are:


#mypackage/mymodule/somefile.py

def some_function():
    return 'A'

#mypackage/myothermodule/__init__.py

from mypackage.mymodule.somefile import some_function

def func_to_test():
    return some_function()

#mypackage/myothermodule/tests/test_func_to_test.py

from unittest import TestCase
from mock import patch

class TestFunc_to_test(TestCase):
    def test_func_to_test(self):
        from mypackage.myothermodule import func_to_test
        self.assertEqual('A', func_to_test())

    @patch('mypackage.mymodule.somefile.some_function')
    def test_func_to_test_mocked(self, some_mock_function):
        from mypackage.myothermodule import func_to_test
        some_mock_function.return_value = 'B'
        self.assertEqual('B', func_to_test())

The problem I have is that, while the first test passes (test_func_to_test), the second test (test_func_to_test_mocked) does not (due to an AssertionError).

I have been able to mock function from “built-in” modules (like requests.get, for example) using the same approach, however I can’t seem to get the @patch working when trying to patch a function from one of my modules…

Any help would be appreciated 🙂

  • 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-18T08:09:13+00:00Added an answer on June 18, 2026 at 8:09 am

    mypackage.myothermodule has already been imported, so the name some_function is already bound within that module. You need to mock the usage of that name within the module it is being called from:

    @patch('mypackage.myothermodule.some_function')
    

    You could alternatively reload mypackage.myothermodule:

    import mypackage.myothermodule
    reload(mypackage.myothermodule)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm attempting to build a JSF/Hibernate application using Java 7 and Tomcat 7. I
I'm attempting to build an application using the Salesforce API (with custom PatronManager objects)
I'm using Google Test and Google Mock for my C++/Qt application. I've been having
I'm attempting to build a test infrastructure Active Directory + Lync + Exchange. After
I'm attempting to build a c# console application to test a rest service that
I'm attempting to build an app using the Sencha Touch 2 builder tools. I
I'm attempting to build Python 2.6.2 from source on my Linux system. It has
I am attempting to get the Jersey test framework working. We are building using
I'm attempting to build a simple project using Yagarto and Eclipse for an ARM
I'm attempting to build a Virtual Machine to develop a Django application. The OS

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.