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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 8, 20262026-06-08T20:00:57+00:00 2026-06-08T20:00:57+00:00

Coming from a static programming language background, I am wondering how to best do

  • 0

Coming from a static programming language background, I am wondering how to best do mocking in Python. I am accustomed to dependency injection. Within tests, mocks are created and passed to the System Under Test (SUT). However, looking at Mock and other mock frameworks for Python, it appears that the types/functions/etc. in the module are replaced on a test-by-test basis.

Particularly, with Mock, atop each unit test you say @patch('some.type.in.the.module.under.test') for each type/function/etc. you want to mock. For the lifetime of the test those things are mocked, then they are reverted. Unfortunately, across tests, the fixture is pretty close to the same and you end up repeating your @patches over and over again.

I want a way to share a collection of patches across unit tests. I also want carry through tweaks to the fixture in a composable way. I am okay using a context manager instead of a decorator.

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

    You can patch the test class which flows down to each method in that class. And then you could inherit from a super class and work with the the setUp and tearDown methods.

    import unittest 
    
    @patch('some.type.in.the.module.under.test')
    class MySuperTestCase(unittest.TestCase):
        pass
    
    class MyActualTestCase(MySuperTestCase):
    
        def test_method(self, mock_function)
            mock_function.return_value = False
    

    This is less general than you might think though. Because you need to patch the object in the exact location where it is being used. You don’t patch ‘sys.stdout’, you patch ‘my_dir.my_module.sys.stdout’. So it would really only be of use when testing a particular module. But to test that particular model you could certainly only need one single patch decorator.

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

Sidebar

Related Questions

Coming from a Java background, I'm quite fond of static type safety and wonder
Coming from a non-web background I'm struggling with cookie uniqueness. When I read and
Coming from a .NET background I'm use to reusing string variables for storage, so
Coming from a PHP background, I'm used to writing small functions that return a
Coming from a C# Background I never used any pointers. I'm creating a vector
Coming from a C++ background, I am finding cloning of objects in C# a
Is there an easy way to add expires headers to static content coming from
Okay: I'm fairly new to C++ and static languages on a whole. Coming from
Coming from a C background, I've always assumed the POD types (eg ints) were
Coming from a C background, what would Linked Frameworks and Other Frameworks mean? Having

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.