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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T09:19:30+00:00 2026-06-12T09:19:30+00:00

The question is in context of unit testing. I have created an instance of

  • 0

The question is in context of unit testing.
I have created an instance of the class I am testing, and trying to test one of its methods. The method uses data it gets from different class defined in separate module. I am going to mock that module.
How I can access my instance’s name space – I have to do it before running the method I am testing, to mock module which contain definition of the class my method is getting data from?

  • 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-12T09:19:30+00:00Added an answer on June 12, 2026 at 9:19 am

    I am going to create an example here which I think parallels what you are trying to do.

    Say you have some class that we’ll call Data that is defined in the module foo. The foo module imports bar and a method of foo.Data calls bar.get_data() to populate itself.

    You want to create a module test that will create an instance of foo.Data, but instead of using the actual module bar you want that instance to use a mocked version of this.

    You can set this up by importing foo from your test module, and then rebinding foo.bar to your mocked version of the module.

    Here is an example of how this might look:

    • bar.py:

      def get_data():
          return 'bar'
      
    • foo.py:

      import bar
      
      class Data(object):
          def __init__(self):
              self.val = bar.get_data()
      
      if __name__ == '__main__':
          d = Data()
          print d.val    # prints 'bar'
      
    • test.py:

      import foo
      
      class bar_mock(object):
          @staticmethod
          def get_data():
              return 'test'
      
      if __name__ == '__main__':
          foo.bar = bar_mock
          d = foo.Data()
          print d.val    # prints 'test'
      

    Although this will get you by for a simple test case, you are probably better off looking into a mocking library to handle this for you.

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

Sidebar

Related Questions

I have a prototype question in the context of Ruby on Rails. QUESTION -
In my most recent question: Unit Testing Best Practice? / C# InternalsVisibleTo() attribute for
I'm just starting out with unit testing in C#. I have been reading about
I read a lot of question and answer on TDD and unit testing on
I have two unit test projects in my VS 2010 solution. Each project has
I have created a generic unit of work/repository pattern and it works in most
Context I am writing a simple JUnit test for the MyObject class. A MyObject
I don't have much experience doing unit testing. From what I learned, code should
After reading several questions/answers about unit testing Entity Framework, I have decided to forgo
What's the best way to unit test the Serialization of an Exception class like

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.