From 123d3e09d90a23b9bba00d7b81ebd8d3b77527e4 Mon Sep 17 00:00:00 2001 From: UbuntuWS Date: Thu, 3 Aug 2017 17:51:01 -0700 Subject: [PATCH 1/3] Add log to unit testing. --- .gitignore | 5 ++-- bin/runUnitTests.py | 27 +++++++++++++++---- .../model_parameters.txt | 0 .../.~lock.exodata_table_dymola_out.csv# | 1 - unittests/test_models.py | 4 +-- 5 files changed, 27 insertions(+), 10 deletions(-) rename unittests/{resources => outputs}/model_parameters.txt (100%) delete mode 100755 unittests/resources/.~lock.exodata_table_dymola_out.csv# diff --git a/.gitignore b/.gitignore index dd29579..0322c0f 100644 --- a/.gitignore +++ b/.gitignore @@ -2,9 +2,10 @@ ######################### *.pyc -# Unit testing figures +# Unit testing outputs ######################### -unittests/resources/*.png +unittests/outputs/*.png +unittests/outputs/*.log # Ignore Dymola output, log and temporary files (in alphabetical order) ####################################################################### diff --git a/bin/runUnitTests.py b/bin/runUnitTests.py index a60e71f..d38f2c1 100755 --- a/bin/runUnitTests.py +++ b/bin/runUnitTests.py @@ -11,8 +11,18 @@ import tempfile import os import shutil +import sys +from mpcpy import utility from doc.userGuide.tutorial import introductory + +def check_result(results): + sys.stdout = sys.__stdout__ + if result.errors or result.failures: + print('{0} errors and {1} failures found in tests. Please consult outputs/log for more info.'.format(len(result.errors), len(result.failures))); + else: + print('All tests successful.') + # Change working directory to temporary cwd = os.getcwd(); tempdir = tempfile.mkdtemp(); @@ -72,9 +82,14 @@ print('{} unit tests found.'.format(n_tests)); if n_tests: # Run test suite - print('Running unit tests...'); - unittest.TextTestRunner(verbosity = 1).run(suite); - + print('Running unit tests...'); + # Configure the log + logpath = os.path.join(utility.get_MPCPy_path(), 'unittests', 'outputs', 'unittests.log') + sys.stdout = open(logpath, 'w') + result = unittest.TextTestRunner(verbosity = 1, stream=sys.stdout).run(suite); + + check_result(result) + # Delete temporary directory and change working directory back to original shutil.rmtree(tempdir, ignore_errors=True) os.chdir(cwd); @@ -84,9 +99,11 @@ if 'test_tutorial' in modules: print('\n\nRunning tutorial doctests...') + sys.stdout = open(logpath, 'w') doctest.ELLIPSIS_MARKER = '-etc-' os.chdir(os.path.dirname(introductory.__file__)) suite_tut = unittest.TestSuite(); suite_tut.addTests(doctest.DocTestSuite(introductory)) - unittest.TextTestRunner(verbosity = 1).run(suite_tut); - os.chdir(cwd) \ No newline at end of file + result = unittest.TextTestRunner(verbosity = 1, stream=sys.stdout).run(suite_tut); + + check_result(result) diff --git a/unittests/resources/model_parameters.txt b/unittests/outputs/model_parameters.txt similarity index 100% rename from unittests/resources/model_parameters.txt rename to unittests/outputs/model_parameters.txt diff --git a/unittests/resources/.~lock.exodata_table_dymola_out.csv# b/unittests/resources/.~lock.exodata_table_dymola_out.csv# deleted file mode 100755 index c8d8c82..0000000 --- a/unittests/resources/.~lock.exodata_table_dymola_out.csv# +++ /dev/null @@ -1 +0,0 @@ -,dhblum,ubuntu,16.09.2016 11:11,file:///home/dhblum/.config/libreoffice/4; \ No newline at end of file diff --git a/unittests/test_models.py b/unittests/test_models.py index e6c5035..b5f56d9 100755 --- a/unittests/test_models.py +++ b/unittests/test_models.py @@ -242,7 +242,7 @@ def test_estimate_and_validate(self): self.building.collect_measurements(self.start_time_validation, self.final_time_validation); self.model.measurements = self.building.measurements; self.model.validate(self.start_time_validation, self.final_time_validation, \ - os.path.join(self.MPCPyPath, 'unittests', 'resources', 'model_validation')); + os.path.join(self.MPCPyPath, 'unittests', 'outputs', 'model_validation')); # Check references RMSE = {}; for key in self.model.RMSE.keys(): @@ -401,7 +401,7 @@ def test_validate(self): self.occupancy.set_simulate_options(simulate_options); np.random.seed(1); self.occupancy.validate(self.start_time, self.final_time, \ - os.path.join(self.MPCPyPath, 'unittests', 'resources', \ + os.path.join(self.MPCPyPath, 'unittests', 'outputs', \ 'occupancy_model_validate')); # Check references RMSE = {}; From 33764dcdc6ccd9b044577e3417d8ec348bf9f2d5 Mon Sep 17 00:00:00 2001 From: UbuntuWS Date: Fri, 4 Aug 2017 11:23:10 -0700 Subject: [PATCH 2/3] Remove logging and code updates. --- bin/runUnitTests.py | 86 ++++++++++++++++++++++++++++++--------------- 1 file changed, 57 insertions(+), 29 deletions(-) diff --git a/bin/runUnitTests.py b/bin/runUnitTests.py index d38f2c1..62ab0b0 100755 --- a/bin/runUnitTests.py +++ b/bin/runUnitTests.py @@ -11,31 +11,52 @@ import tempfile import os import shutil -import sys from mpcpy import utility from doc.userGuide.tutorial import introductory -def check_result(results): - sys.stdout = sys.__stdout__ - if result.errors or result.failures: - print('{0} errors and {1} failures found in tests. Please consult outputs/log for more info.'.format(len(result.errors), len(result.failures))); +def check_result(result, name): + '''Check and report results of testing. + + Parameters + ---------- + result : unittest.TextTestRunner result + The results of the test. + result.errors and results.failures are lists. + name : string + Type of test: unit or tutorial + + ''' + + if result: + if result.errors or result.failures: + print('{0} errors and {1} failures found in '.format(len(result.errors), len(result.failures)) + name + ' tests. Please consult terminal output for more info.'); + else: + print(name + ' tests OK.') else: - print('All tests successful.') + print(name + ' tests not run.') + return None + + +# Main program +# ============ + +# Setup +# ----- # Change working directory to temporary cwd = os.getcwd(); tempdir = tempfile.mkdtemp(); os.chdir(tempdir); - +# Configure the log +logpath = os.path.join(utility.get_MPCPy_path(), 'unittests', 'outputs', 'unittests.log') # Configure the argument parser parser = argparse.ArgumentParser(description='Run the unit tests for mpcpy.'); unit_test_group = parser.add_argument_group("arguments to run unit tests"); unit_test_group.add_argument('-s', '--specify_test', \ metavar='module.class', \ help='test only the module and class specified'); -args = parser.parse_args(); - +args = parser.parse_args(); # Define test modules and classes, if any modules = []; classes = []; @@ -56,6 +77,8 @@ def check_result(results): 'test_tutorial']; classes = []; +# Unit tests +# ---------- # Load Tests print('Loading tests...'); suite = unittest.TestSuite(); @@ -76,34 +99,39 @@ def check_result(results): # Add test classes to suite for obj in module_classes: suite.addTests(unittest.TestLoader().loadTestsFromTestCase(obj)); - # Report number of tests found n_tests = suite.countTestCases(); -print('{} unit tests found.'.format(n_tests)); +print('\n{} unit tests found.'.format(n_tests)); if n_tests: # Run test suite - print('Running unit tests...'); - # Configure the log - logpath = os.path.join(utility.get_MPCPy_path(), 'unittests', 'outputs', 'unittests.log') - sys.stdout = open(logpath, 'w') - result = unittest.TextTestRunner(verbosity = 1, stream=sys.stdout).run(suite); - - check_result(result) - + print('\nRunning unit tests...'); + result1 = unittest.TextTestRunner(verbosity = 1).run(suite); +else: + result1 = None; # Delete temporary directory and change working directory back to original shutil.rmtree(tempdir, ignore_errors=True) os.chdir(cwd); -# Run tutorial doctest -#--------------------- - +# Tutorial tests +#--------------- if 'test_tutorial' in modules: - print('\n\nRunning tutorial doctests...') - sys.stdout = open(logpath, 'w') - doctest.ELLIPSIS_MARKER = '-etc-' - os.chdir(os.path.dirname(introductory.__file__)) + # Collect tests suite_tut = unittest.TestSuite(); suite_tut.addTests(doctest.DocTestSuite(introductory)) - result = unittest.TextTestRunner(verbosity = 1, stream=sys.stdout).run(suite_tut); - - check_result(result) + # Report number of tests found + n_tests = suite_tut.countTestCases(); + print('\n{} tutorials found.'.format(n_tests)); + # Run tests + print('\nRunning tutorial doctests...') + doctest.ELLIPSIS_MARKER = '-etc-' + os.chdir(os.path.dirname(introductory.__file__)) + result2 = unittest.TextTestRunner(verbosity = 1).run(suite_tut); + os.chdir(cwd); +else: + result2 = None; + +# Check and report results +#------------------------- +print('\nResults\n-------') +check_result(result1, 'Unit') +check_result(result2, 'Tutorial') \ No newline at end of file From 76a20e550dc6c73c907ed5e9782b36d115086109 Mon Sep 17 00:00:00 2001 From: UbuntuWS Date: Fri, 4 Aug 2017 11:23:36 -0700 Subject: [PATCH 3/3] Fix bug. --- unittests/test_optimization.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/unittests/test_optimization.py b/unittests/test_optimization.py index 53ce4d5..13b488f 100755 --- a/unittests/test_optimization.py +++ b/unittests/test_optimization.py @@ -312,7 +312,7 @@ def setUp(self): self.control = exodata.ControlFromCSV(self.control_path, self.control_variable_map, tz_name = self.weather.tz_name); self.control.collect_data(self.start_time_exodata, self.final_time_exodata); # Parameters - self.parameters_path = self.MPCPyPath + os.sep + 'unittests' + os.sep + 'resources' + os.sep + 'model_parameters.txt'; + self.parameters_path = self.MPCPyPath + os.sep + 'unittests' + os.sep + 'outputs' + os.sep + 'model_parameters.txt'; self.parameters = exodata.ParameterFromCSV(self.parameters_path); self.parameters.collect_data(); # Constraints