3

I am working on a personal project using python. Currently I am using nose for unit testing and there is a python file for storing all the settings, like files' paths, db config, etc.

Because the application will connect to a database, I hope when I do unit testing, the database used and the files the application reads/writes are not the ones storing all the real data.

I think a good way I could do is setting up several stages, but because I am using pure python without using any frameworks like Django, so I am not sure how to do that.

I can also write a simple script or something similar to switch between different config files before running unit tests/real work, but I think this way is not as good as setting up different stages.

XXXHHHH
  • 90
  • 1
  • 7
  • Your Title is a bit misleading. You should maybe make it something like "using different configs for unit-tests". Also, reformatting your block of text with some practical examples would help make it easier to understand what you are looking for. – idjaw Sep 20 '15 at 12:35

2 Answers2

2

There are different avenues you can take here to help achieve what you are looking for.

To start, I strongly suggest looking at these two modules. I use these all the time for my unit testing.

Mock & flexmock

If you really want to test against a database, you can make use of a context manager to set up a temporary database that will do your testing and once it is done it will destroy itself. For information on context managers, take a look at this to start you off:

Stackoverflow - with

With mocking, what you can do is in this case just mock out where you initially import your settings and use your unit-test version for settings.

Alternatively, you can also take a look at fixtures, which will help pre-set some data objects for you and you can test accordingly: Fixtures

Community
  • 1
  • 1
idjaw
  • 25,487
  • 7
  • 64
  • 83
2

One working way to switch context to testing one is to store configs in files, so that they can be accessed with ConfigArgParse. When you are launching the code in production mode you can use default config files location unless default location is redefined with environment variable:

import configargparse
import os 
config_location = os.getenv("MY_PROJECT_CONFIG_LOCATION", default="configs/production.yaml")
parser = configargparse.ArgParser(default_config_files=[config_location], ignore_unknown_config_file_keys=True)

When you are launching the code in test mode you can redefine default config file location using system environment variables:

class SomeTestCase(unittest.TestCase):
    def someTestMethod(self):
        os.environ['MY_PROJECT_CONFIG_LOCATION'] = 'configs/test.yaml'
        #any test code here
Ivan Sudos
  • 1,423
  • 2
  • 13
  • 25