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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:26:01+00:00 2026-05-10T17:26:01+00:00

I am in a project where we are starting refactoring some massive code base.

  • 0

I am in a project where we are starting refactoring some massive code base. One problem that immediately sprang up is that each file imports a lot of other files. How do I in an elegant way mock this in my unit test without having to alter the actual code so I can start to write unit-tests?

As an example: The file with the functions I want to test, imports ten other files which is part of our software and not python core libs.

I want to be able to run the unit tests as separately as possible and for now I am only going to test functions that does not depend on things from the files that are being imported.

EDIT

Thanks for all the answers.

I didn’t really know what I wanted to do from the start but now I think I know.

Problem was that some imports was only possible when the whole application was running because of some third-party auto-magic. So I had to make some stubs for these modules in a directory which I pointed out with sys.path

Now I can import the file which contains the functions I want to write tests for in my unit-test file without complaints about missing modules.

  • 1 1 Answer
  • 1 View
  • 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. 2026-05-10T17:26:02+00:00Added an answer on May 10, 2026 at 5:26 pm

    If you want to import a module while at the same time ensuring that it doesn’t import anything, you can replace the __import__ builtin function.

    For example, use this class:

    class ImportWrapper(object):     def __init__(self, real_import):         self.real_import = real_import      def wrapper(self, wantedModules):         def inner(moduleName, *args, **kwargs):             if moduleName in wantedModules:                 print 'IMPORTING MODULE', moduleName                 self.real_import(*args, **kwargs)             else:                 print 'NOT IMPORTING MODULE', moduleName         return inner      def mock_import(self, moduleName, wantedModules):         __builtins__.__import__ = self.wrapper(wantedModules)         try:             __import__(moduleName, globals(), locals(), [], -1)         finally:             __builtins__.__import__ = self.real_import 

    And in your test code, instead of writing import myModule, write:

    wrapper = ImportWrapper(__import__) wrapper.mock_import('myModule', []) 

    The second argument to mock_import is a list of module names you do want to import in inner module.

    This example can be modified further to e.g. import other module than desired instead of just not importing it, or even mocking the module object with some custom object of your own.

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

Sidebar

Ask A Question

Stats

  • Questions 145k
  • Answers 145k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Take a look at this article that describes how to… May 12, 2026 at 8:52 am
  • Editorial Team
    Editorial Team added an answer return Enumerable.Range(0, individual.Length) .Count(i => individual[i] == target[i]); A more… May 12, 2026 at 8:52 am
  • Editorial Team
    Editorial Team added an answer It seems Microsoft do provide some APIs for intercepting the… May 12, 2026 at 8:52 am

Related Questions

Backgroud: I am a board member of the Fox Valley .Net User Group .
Let us suppose we are going to start new project - application that contains
I'm working on a Flash project right now and I am having to force
Ok, I need help. This is my first question here. Background: I am working

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.