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

  • Home
  • SEARCH
  • 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 8317551
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T21:38:36+00:00 2026-06-08T21:38:36+00:00

I have the following simplified class I’m mocking: class myClass(object): @staticmethod def A(): #…

  • 0

I have the following simplified class I’m mocking:

class myClass(object):
    @staticmethod
    def A():
        #...

    def check(self):
        #code...
        value = self.A()
        #more code...

In my first test I mock only the method A

from django.test import TestCase
from mock import MagicMock
import myClass

class FirstTest(TestCase):

def setUp(self):
    myClass.A = MagicMock(return_value = 'CPU')

def test(self):
    #some tests 
    myClassObj = myClass()
    myClassObj.check()

Whereas in my second test I mock the entire check method:

from django.test import TestCase
from mock import MagicMock
import myClass

class SecondTest(TestCase):

def setUp(self):
    myClass.check = MagicMock(return_value = someObject)

def test(self):
    #some tests 
    myClassObj = myClass()
    myClassObj.check()

Now my assertions from my first test fail because, instead of calling check() and mocking A() inside check(), it calls the completely mocked check() from my second test.

Is there any way to clear and set the method to be ‘normal’ after the test? I tried myClass.check.reset_mock() already, but it doesn’t seem to do anything. Moving the order of my tests doesn’t do anything either.

I’m using mock 1.0b1 for python from http://pypi.python.org/pypi/mock/

  • 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-08T21:38:38+00:00Added an answer on June 8, 2026 at 9:38 pm

    You can stash the function away on self and put it back when you’re done.

    import unittest
    
    from mock import MagicMock
    from MyClass import MyClass
    
    class FirstTest(unittest.TestCase):
    
        def setUp(self):
            self.A = MyClass.A
            MyClass.A = MagicMock(name='mocked A', return_value='CPU')
    
    
        def tearDown(self):
            MyClass.A = self.A
    
        def test_mocked_static_method(self):
            print 'First Test'
            print MyClass.check
            print MyClass.A
    
    
    class SecondTest(unittest.TestCase):
    
        def setUp(self):
            MyClass.check = MagicMock(name='mocked check', return_value=object)
    
        def test_check_mocked_check_method(self):
            print 'Second Test'
            print MyClass.check
            print MyClass.A
    
    
    if __name__ == '__main__':
        unittest.main()
    

    Running this file gives the following output:

    First Test
    <unbound method MyClass.check> 
    <MagicMock name='mocked A' id='141382732'>
    Second Test
    <MagicMock name='mocked check' id='141382860'>
    <unbound method MyClass.A>
    

    I found myself using the patch decorator a lot more than setUp and tearDown now. In this case you could do

    from mock import patch
    
    @patch('MyClass.A')
    def test_mocked_static_method(self, mocked_A)
        mocked_A.return_value = 'CPU'
        # This mock will expire when the test method is finished
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have the following (obviously simplified) class class A(object) def __init__(self, a): self.a =
I have the following code(simplified). public class OrderProcessor { public virtual string PlaceOrder(string test)
I have the following setup (simplified, obviously): An abstract class, with an A object
Consider the following simplified version of my code. I have a template class A
I have the following C++ code (simplified version): class Shape { bool isCircle =
I have the following (simplified) code periodically run by a Thread in the class
I have the following (simplified) HTML structure <td> <DIV class=t-numerictextbox> <DIV class=t-formatted-value>$0.00</DIV> <INPUT id=MyObj_PropertyName
If I have the following code (simplified version of the problem): class TestA {
I have the following code (I simplified it & removed unrelevant parts) public class
I have the following simplified ViewModel public class UserViewModel : IUserViewModel { public DelegateCommand<object>

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.