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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T10:25:47+00:00 2026-06-15T10:25:47+00:00

I have several TestCase classes in my django application. On some of them, I

  • 0

I have several TestCase classes in my django application. On some of them, I mock out a function which calls external resources by decorating the class with @mock.patch, which works great. One TestCase in my test suite, let’s call it B(), depends on that external resource so I don’t want it mocked out and I don’t add the decorator. It looks something like this:

@mock.patch("myapp.external_resource_function", new=mock.MagicMock)
class A(TestCase):
    # tests here

class B(TestBase):
    # tests here which depend on external_resource_function

When I test B independently, things work as expected. However, when I run both tests together, A runs first but the function is still mocked out in B. How can I unmock that call? I’ve tried reloading the module, but it didn’t help.

  • 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-15T10:25:47+00:00Added an answer on June 15, 2026 at 10:25 am

    Patch has start and stop methods. Based on what I can see from the code you have provided, I would remove the decorator and use the setUp and tearDown methods found in the link in your classes.

    class A(TestCase):
      def setUp(self):
        self.patcher1 = patch('myapp.external_resource_function', new=mock.MagicMock)
        self.MockClass1 = self.patcher1.start()
    
      def tearDown(self):
        self.patcher1.stop()
    
      def test_something(self):
        ...
    
    >>> A('test_something').run()
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have found following function calls in several frameworks which appear to me as
I have a XML where I have several <testcase> tags. Some of them have
I have a unittest.TestCase child, which defines several domain specific assert Checks. I would
I have several Delphi programs that maintain connections to a database (some Oracle, some
I have several listviews with two collumns like. I am building a stats application
I have several Select which depend on each other if I select a value
I have an MVC application with a single model and several views (something like
I have a link which contains several images. Upon hovering an interval is set
Using ActiveSupport::TestCase. I think that's the old Test::Unit. I have several tables that represent
I have several XML elements like this: <testcase starttime=2012-09-03 10:41:29 timestamp= 622.922000> <testlogfile file=

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.