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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T00:01:17+00:00 2026-05-23T00:01:17+00:00

I am working on a series of unit tests in Python, some of which

  • 0

I am working on a series of unit tests in Python, some of which depend on the value of a configuration variable. These variables are stored in a global Python config file and are used in other modules. I would like to write unit tests for different values of the configuration variables but have not yet found a way to do this.

I do not have the possibility to rewrite the signatures of the methods I’m testing.

This is what I would like to achieve:

from my_module import my_function_with_global_var

class TestSomething(self.unittest):

    def test_first_case(self):
         from config import MY_CONFIG_VARIABLE
         MY_CONFIG_VARIABLE = True
         self.assertEqual(my_function_with_global_var(), "First result")

    def test_second_case(self):
         from config import MY_CONFIG_VARIABLE
         MY_CONFIG_VARIABLE = False
         self.assertEqual(my_function_with_global_var(), "Second result")

Thanks.

Edit: Made the example code more explicite.

  • 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-05-23T00:01:17+00:00Added an answer on May 23, 2026 at 12:01 am

    Use unittest.mock.patch as in @Flimm’s answer, if that’s available to you.


    Original Answer

    Don’t do this:

    from my_module import my_function_with_global_var
    

    But this:

    import my_module
    

    And then you can inject MY_CONFIG_VARIABLE into the imported my_module, without changing the system under test like so:

    class TestSomething(unittest.TestCase): # Fixed that for you!
    
        def test_first_case(self):
             my_module.MY_CONFIG_VARIABLE = True
             self.assertEqual(my_module.my_function_with_global_var(), "First result")
    
        def test_second_case(self):
             my_module.MY_CONFIG_VARIABLE = False
             self.assertEqual(my_module.my_function_with_global_var(), "Second result")
    

    I did something similar in my answer to How can I simulate input to stdin for pyunit? .

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

Sidebar

Related Questions

I am working on a simple Rails/jQuery HTML templater app which stores a series
I'm working on developing a series of reports. These reports require various columns to
I'm working on a series of data structures. Some require a specific number of
I am working on a site in which there are a series of elements,
I'm working on a school project, which requires JAVA to send a series of
I'm working on a project whereby I have a series of configuration classes in
I am working on time series data, for which the key column is a
I'm working with very long time series -- hundreds of millions of data points
Part of the series of controls I am working on obviously involves me lumping
I am working a project where I need to generate a series of classes

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.