Commits

Michael Foord committed 3f50b80

Addition of configs and userConfig parameters to main function

  • Participants
  • Parent commits e0ed4f3
  • Branches plugins

Comments (0)

Files changed (3)

     * growl notifications on test run start and stop
     * filter individual test methods using a regex
     * load test functions from modules as well as TestCases
-    * test generators
+    * test generators *and* parameterized tests
     * integration with the coverage module for coverage reporting
     * display the time of individual tests in verbose reports
     * display a progress indicator as tests are run ([39/430] format) in verbose reports
 useful in some situations. This can be fixed with the use of the optparse
 epilogue.
 
+`main` gains two new parameters: `configs` and `userConfig`. If configs is supplied it can be a string or sequence of strings. It will be used *instead* of loading a config file from the current project directory. If userConfig is False then the userConfig file won't be loaded. If userConfig is False and configs is not None (it can be an empty list) then the --no-plugins option is not available at the command line as the loading of plugins is being completely controlled from the `main(...)` call.
+
 The verbosity argument to the main function and the TextTestRunner can be passed in as strings ('quiet', 'normal' or 'verbose', which correspond to 0, 1, and 2) instead of just integers.
 
 unit2 (the default test runner) runs test discovery if invoked without any arguments.

unittest2/config.py

         configs.append((userPlugins, userParser, userExcludedPlugins))
     
     
-    if not configLocations:
+    if configLocations is None:
         cfgPath = os.path.join(os.getcwd(), CFG_NAME)
         localParser, localPlugins, localExcludedPlugins = loadPluginsConfigFile(cfgPath)
         configs.append((localPlugins, localParser, localExcludedPlugins))

unittest2/main.py

     
     # defaults for testing
     failfast = catchbreak = buffer = progName = module = defaultTest = None
-    pluginsLoaded = verbosity = None
+    pluginsLoaded = verbosity = configs = None
+    userConfig = True
 
     def __init__(self, module='__main__', defaultTest=None,
                  argv=None, testRunner=None,
                  testLoader=loader.defaultTestLoader, exit=True,
-                 verbosity=None, failfast=None, catchbreak=None, buffer=None):
+                 verbosity=None, failfast=None, catchbreak=None, buffer=None,
+                 configs=None, userConfig=True):
         if isinstance(module, basestring):
             __import__(module)
             self.module = sys.modules[module]
         else:
             self.module = module
 
+        if isinstance(configs, basestring):
+            configs = (configs,)
+        self.configs = configs
+        self.userConfig = userConfig
 
         if isinstance(verbosity, basestring):
             # allow string verbosities not in the dictionary
         if isinstance(testLoader, type):
             testLoader = testLoader()
         self.testLoader = testLoader
-        self.progName = os.path.basename(argv[0])
+
+        # allow argv[0] to be None
+        self.progName = os.path.basename(argv[0] or '')
 
         self.parseArgs(argv)
         self.runTests()
         # an initial pass over command line options to load config files
         # and plugins
         parser = _ImperviousOptionParser()
-        parser.add_option('--config', dest='configLocations', action='append')
-        parser.add_option('--no-user-config', dest='noUserConfig', default=False,
-                          action='store_true')
-        parser.add_option('--no-plugins', dest='pluginsDisabled', default=False,
-                          action='store_true')
+        if self.configs is None:
+            parser.add_option('--config', dest='configLocations', action='append')
+        if self.userConfig:
+            parser.add_option('--no-user-config', dest='noUserConfig', default=False,
+                              action='store_true')
+        if self.configs is None or self.userConfig:
+            parser.add_option('--no-plugins', dest='pluginsDisabled', default=False,
+                              action='store_true')
 
         if TestProgram.pluginsLoaded:
             # only needed because we call several times during tests
 
         # we catch any optparse errors here as they will be
         # reraised on the second pass through
-        try:
-            options, _ = parser.parse_args(argv)
-        except optparse.OptionError:
-            pluginsDisabled = False
-            noUserConfig = False
-            configLocations = []
-        else:
-            pluginsDisabled = options.pluginsDisabled
-            noUserConfig = options.noUserConfig
-            configLocations = options.configLocations
+        noUserConfig = not self.userConfig
+        configLocations = self.configs
+        pluginsDisabled = False
+        if self.configs is None or self.userConfig:
+            try:
+                options, _ = parser.parse_args(argv)
+            except optparse.OptionError:
+                pluginsDisabled = False
+                configLocations = self.configs or []
+            else:
+                pluginsDisabled = options.pluginsDisabled
+                if not noUserConfig:
+                    noUserConfig = options.noUserConfig
+                if configLocations is None:
+                    configLocations = options.configLocations or None
 
         loadPlugins(pluginsDisabled, noUserConfig, configLocations)
         TestProgram.pluginsLoaded = True
         parser.add_option('-q', '--quiet', dest='quiet', default=False,
                           help='Quiet output', action='store_true')
         
-        parser.add_option('--config', dest='configLocations', action='append',
-                          help='Specify local config file location')
-        parser.add_option('--no-user-config', dest='noUserConfig', default=False,
-                          action='store_true',
-                          help="Don't use user config file")
-        parser.add_option('--no-plugins', dest='pluginsDisabled', default=False,
-                          action='store_true', help="Disable all plugins")
+        if self.configs is None:
+            parser.add_option('--config', dest='configLocations', action='append',
+                              help='Specify local config file location')
+        if self.userConfig:
+            parser.add_option('--no-user-config', dest='noUserConfig', default=False,
+                              action='store_true',
+                              help="Don't use user config file")
+        if self.configs is None or self.userConfig:
+            parser.add_option('--no-plugins', dest='pluginsDisabled', default=False,
+                              action='store_true', help="Disable all plugins")
         
         if self.failfast != False:
             parser.add_option('-f', '--failfast', dest='failfast', default=None,