From b33215cfe2c41439c50c91455d32efc898d570cd Mon Sep 17 00:00:00 2001
From: Bill Sacks <sacks@ucar.edu>
Date: Tue, 2 Aug 2016 21:21:11 -0600
Subject: [PATCH] Implementation #3 of running other unit tests from
 scripts_regression_tests

This implementation puts unit tests defined elsewhere on equal footing
with the unit tests defined in scripts_regression_tests, rather than
wrapping them in their own unit test.

Test suite: None
Test baseline: N/A
Test namelist changes: N/A
Test status: N/A

Fixes: None

User interface changes?: No

Code review: None
---
 .../python/tests/scripts_regression_tests.py  | 37 ++++++++++---------
 1 file changed, 19 insertions(+), 18 deletions(-)

diff --git a/utils/python/tests/scripts_regression_tests.py b/utils/python/tests/scripts_regression_tests.py
index 36dc536cbe4..2234dea2e95 100755
--- a/utils/python/tests/scripts_regression_tests.py
+++ b/utils/python/tests/scripts_regression_tests.py
@@ -39,23 +39,6 @@ def do_unit_tests(self, script,from_dir=SCRIPT_DIR):
         stat, output, _ = run_cmd("./%s --test 2>&1" % script, from_dir=from_dir)
         self.assertEqual(stat, 0, msg=output)
 
-    def test_unittests(self):
-        # Finds all files contained in LIB_DIR or its subdirectories that match
-        # the pattern 'test*.py', and runs the unit tests found there (i.e.,
-        # tests defined using python's unittest module).
-        #
-        # This is analogous to running:
-        #     python -m unittest discover
-        #
-        # It seems kind of funny to run a bunch of other unit test suites from
-        # within this single unit test, but doing it this way makes it
-        # consistent with how we run other tests in this module.
-
-        cmd = 'python -m unittest discover'
-        stat, output, _ = run_cmd("%s 2>&1"%cmd, from_dir = LIB_DIR)
-
-        self.assertEqual(stat, 0, msg=output)
-
     def test_cime_bisect_unit_test(self):
         self.do_unit_tests("cime_bisect",from_dir=TOOLS_DIR)
 
@@ -1647,7 +1630,25 @@ def _main_func():
 
     CIME.utils.handle_standard_logging_options(args)
 
-    unittest.main(verbosity=2, catchbreak=True)
+    # Finds all files contained in LIB_DIR or its subdirectories that match the
+    # pattern 'test*.py', and runs the unit tests found there (i.e., tests
+    # defined using python's unittest module).
+    #
+    # This is analogous to running:
+    #     python -m unittest discover
+    testsuite_from_discovery = \
+      unittest.defaultTestLoader.discover(start_dir=LIB_DIR)
+
+    # Add tests defined in this module
+    testsuite_from_this_module = \
+      unittest.defaultTestLoader.loadTestsFromModule(sys.modules[__name__])
+
+    # Run all tests found here and elsewhere
+    suitelist = [testsuite_from_discovery, testsuite_from_this_module]
+    fullsuite = unittest.TestSuite(suitelist)
+    unittest.installHandler()
+    unittest.TextTestRunner(verbosity=2).run(fullsuite)
+
 
 if (__name__ == "__main__"):
     _main_func()