Skip to content

Commit

Permalink
Merge pull request #68 from lbl-srg/issue67_unittestUpdate
Browse files Browse the repository at this point in the history
Issue67 unittest update
  • Loading branch information
dhblum authored Aug 4, 2017
2 parents cb9c89b + 76a20e5 commit ba762fa
Show file tree
Hide file tree
Showing 6 changed files with 67 additions and 22 deletions.
5 changes: 3 additions & 2 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -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)
#######################################################################
Expand Down
77 changes: 61 additions & 16 deletions bin/runUnitTests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = [];
Expand All @@ -46,6 +77,8 @@
'test_tutorial'];
classes = [];

# Unit tests
# ----------
# Load Tests
print('Loading tests...');
suite = unittest.TestSuite();
Expand All @@ -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)
# 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')
File renamed without changes.
1 change: 0 additions & 1 deletion unittests/resources/.~lock.exodata_table_dymola_out.csv#

This file was deleted.

4 changes: 2 additions & 2 deletions unittests/test_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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 = {};
Expand Down
2 changes: 1 addition & 1 deletion unittests/test_optimization.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit ba762fa

Please sign in to comment.