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..62ab0b0 100755 --- a/bin/runUnitTests.py +++ b/bin/runUnitTests.py @@ -11,21 +11,52 @@ import tempfile import os import shutil +from mpcpy import utility from doc.userGuide.tutorial import introductory + +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(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 = []; @@ -46,6 +77,8 @@ 'test_tutorial']; classes = []; +# Unit tests +# ---------- # Load Tests print('Loading tests...'); suite = unittest.TestSuite(); @@ -66,27 +99,39 @@ # 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...'); - unittest.TextTestRunner(verbosity = 1).run(suite); - + 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...') - doctest.ELLIPSIS_MARKER = '-etc-' - os.chdir(os.path.dirname(introductory.__file__)) + # Collect tests 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 + # 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 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 = {}; 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