From 0d3845c32744b59dc1c99b3bb28111b6ad793a0f Mon Sep 17 00:00:00 2001 From: Paul Romano Date: Mon, 29 Jan 2018 13:46:40 -0600 Subject: [PATCH] Fix command-line arguments for regression tests --- tests/conftest.py | 17 +++++++++ tests/regression_tests/__init__.py | 9 +++++ tests/regression_tests/mg_convert/test.py | 25 ++++++------- .../mgxs_library_ce_to_mg/test.py | 17 ++++----- tests/regression_tests/plot/test.py | 11 +++--- .../statepoint_restart/test.py | 9 ++--- tests/testing_harness.py | 35 +++++++------------ tests/unit_tests/test_capi.py | 1 + tools/ci/travis-script.sh | 14 ++++---- 9 files changed, 75 insertions(+), 63 deletions(-) create mode 100644 tests/conftest.py diff --git a/tests/conftest.py b/tests/conftest.py new file mode 100644 index 0000000000..422c036fb3 --- /dev/null +++ b/tests/conftest.py @@ -0,0 +1,17 @@ +from tests.regression_tests import config as regression_config + + +def pytest_addoption(parser): + parser.addoption('--exe') + parser.addoption('--mpi', action='store_true') + parser.addoption('--mpiexec') + parser.addoption('--mpi-np') + parser.addoption('--update', action='store_true') + parser.addoption('--build-inputs', action='store_true') + + +def pytest_configure(config): + opts = ['exe', 'mpi', 'mpiexec', 'mpi_np', 'update', 'build_inputs'] + for opt in opts: + if config.getoption(opt) is not None: + regression_config[opt] = config.getoption(opt) diff --git a/tests/regression_tests/__init__.py b/tests/regression_tests/__init__.py index e69de29bb2..965a7f94db 100644 --- a/tests/regression_tests/__init__.py +++ b/tests/regression_tests/__init__.py @@ -0,0 +1,9 @@ +# Test configuration options for regression tests +config = { + 'exe': 'openmc', + 'mpi': False, + 'mpiexec': 'mpiexec', + 'mpi_np': '2', + 'update': False, + 'build_inputs': False +} diff --git a/tests/regression_tests/mg_convert/test.py b/tests/regression_tests/mg_convert/test.py index e6a8690c22..4bbbcbeb9a 100755 --- a/tests/regression_tests/mg_convert/test.py +++ b/tests/regression_tests/mg_convert/test.py @@ -5,6 +5,7 @@ import numpy as np import openmc from tests.testing_harness import PyAPITestHarness +from tests.regression_tests import config # OpenMC simulation parameters batches = 10 @@ -133,24 +134,18 @@ class MGXSTestHarness(PyAPITestHarness): for case in cases: build_mgxs_library(case) - if self._opts.mpi_exec is not None: - mpi_args = [self._opts.mpi_exec, '-n', self._opts.mpi_np] - openmc.run(openmc_exec=self._opts.exe, mpi_args=mpi_args) + if config['mpi']: + mpi_args = [config['mpiexec'], '-n', config['mpi_np']] + openmc.run(openmc_exec=config['exe'], mpi_args=mpi_args) else: - openmc.run(openmc_exec=self._opts.exe) + openmc.run(openmc_exec=config['exe']) - sp = openmc.StatePoint('statepoint.' + str(batches) + '.h5') - - # Write out k-combined. - outstr += 'k-combined:\n' - form = '{0:12.6E} {1:12.6E}\n' - outstr += form.format(sp.k_combined[0], sp.k_combined[1]) - - # Enforce closing statepoint and summary files so HDF5 - # does not throw an error during the next OpenMC execution - sp._f.close() - sp._summary._f.close() + with openmc.StatePoint('statepoint.{}.h5'.format(batches)) as sp: + # Write out k-combined. + outstr += 'k-combined:\n' + form = '{0:12.6E} {1:12.6E}\n' + outstr += form.format(sp.k_combined[0], sp.k_combined[1]) return outstr diff --git a/tests/regression_tests/mgxs_library_ce_to_mg/test.py b/tests/regression_tests/mgxs_library_ce_to_mg/test.py index 9dc640acd1..922b2c302c 100644 --- a/tests/regression_tests/mgxs_library_ce_to_mg/test.py +++ b/tests/regression_tests/mgxs_library_ce_to_mg/test.py @@ -5,6 +5,7 @@ import openmc.mgxs from openmc.examples import pwr_pin_cell from tests.testing_harness import PyAPITestHarness +from tests.regression_tests import config class MGXSTestHarness(PyAPITestHarness): @@ -31,11 +32,11 @@ class MGXSTestHarness(PyAPITestHarness): def _run_openmc(self): # Initial run - if self._opts.mpi_exec is not None: - mpi_args = [self._opts.mpi_exec, '-n', self._opts.mpi_np] - openmc.run(openmc_exec=self._opts.exe, mpi_args=mpi_args) + if config['mpi']: + mpi_args = [config['mpiexec'], '-n', config['mpi_np']] + openmc.run(openmc_exec=config['exe'], mpi_args=mpi_args) else: - openmc.run(openmc_exec=self._opts.exe) + openmc.run(openmc_exec=config['exe']) # Build MG Inputs # Get data needed to execute Library calculations. @@ -63,11 +64,11 @@ class MGXSTestHarness(PyAPITestHarness): sp._summary._f.close() # Re-run MG mode. - if self._opts.mpi_exec is not None: - mpi_args = [self._opts.mpi_exec, '-n', self._opts.mpi_np] - openmc.run(openmc_exec=self._opts.exe, mpi_args=mpi_args) + if config['mpi']: + mpi_args = [config['mpiexec'], '-n', config['mpi_np']] + openmc.run(openmc_exec=config['exe'], mpi_args=mpi_args) else: - openmc.run(openmc_exec=self._opts.exe) + openmc.run(openmc_exec=config['exe']) def _cleanup(self): super(MGXSTestHarness, self)._cleanup() diff --git a/tests/regression_tests/plot/test.py b/tests/regression_tests/plot/test.py index 64f2d4d31a..e7115c4dde 100644 --- a/tests/regression_tests/plot/test.py +++ b/tests/regression_tests/plot/test.py @@ -6,6 +6,7 @@ import h5py import openmc from tests.testing_harness import TestHarness +from tests.regression_tests import config class PlotTestHarness(TestHarness): @@ -15,20 +16,18 @@ class PlotTestHarness(TestHarness): self._plot_names = plot_names def _run_openmc(self): - openmc.plot_geometry(openmc_exec=self._opts.exe) + openmc.plot_geometry(openmc_exec=config['exe']) def _test_output_created(self): """Make sure *.ppm has been created.""" for fname in self._plot_names: - assert os.path.exists(os.path.join(os.getcwd(), fname)), \ - 'Plot output file does not exist.' + assert os.path.exists(fname), 'Plot output file does not exist.' def _cleanup(self): super(PlotTestHarness, self)._cleanup() for fname in self._plot_names: - path = os.path.join(os.getcwd(), fname) - if os.path.exists(path): - os.remove(path) + if os.path.exists(fname): + os.remove(fname) def _get_results(self): """Return a string hash of the plot files.""" diff --git a/tests/regression_tests/statepoint_restart/test.py b/tests/regression_tests/statepoint_restart/test.py index 2ee2e7be79..d36ff93950 100644 --- a/tests/regression_tests/statepoint_restart/test.py +++ b/tests/regression_tests/statepoint_restart/test.py @@ -4,6 +4,7 @@ import os import openmc from tests.testing_harness import TestHarness +from tests.regression_tests import config class StatepointRestartTestHarness(TestHarness): @@ -46,12 +47,12 @@ class StatepointRestartTestHarness(TestHarness): statepoint = statepoint[0] # Run OpenMC - if self._opts.mpi_exec is not None: - mpi_args = [self._opts.mpi_exec, '-n', self._opts.mpi_np] - openmc.run(restart_file=statepoint, openmc_exec=self._opts.exe, + if config['mpi']: + mpi_args = [config['mpiexec'], '-n', config['mpi_np']] + openmc.run(restart_file=statepoint, openmc_exec=config['exe'], mpi_args=mpi_args) else: - openmc.run(openmc_exec=self._opts.exe, restart_file=statepoint) + openmc.run(openmc_exec=config['exe'], restart_file=statepoint) def test_statepoint_restart(): diff --git a/tests/testing_harness.py b/tests/testing_harness.py index 9901dcb718..55a5660f1c 100644 --- a/tests/testing_harness.py +++ b/tests/testing_harness.py @@ -10,29 +10,21 @@ import shutil import sys import numpy as np - import openmc from openmc.examples import pwr_core +from tests.regression_tests import config + class TestHarness(object): """General class for running OpenMC regression tests.""" def __init__(self, statepoint_name): self._sp_name = statepoint_name - self.parser = OptionParser() - self.parser.add_option('--exe', dest='exe', default='openmc') - self.parser.add_option('--mpi_exec', dest='mpi_exec', default=None) - self.parser.add_option('--mpi_np', dest='mpi_np', default='2') - self.parser.add_option('--update', dest='update', action='store_true', - default=False) - self._opts = None - self._args = None def main(self): """Accept commandline arguments and either run or update tests.""" - (self._opts, self._args) = self.parser.parse_args() - if self._opts.update: + if config['update']: self.update_results() else: self.execute_test() @@ -60,11 +52,11 @@ class TestHarness(object): self._cleanup() def _run_openmc(self): - if self._opts.mpi_exec is not None: - openmc.run(openmc_exec=self._opts.exe, - mpi_args=[self._opts.mpi_exec, '-n', self._opts.mpi_np]) + if config['mpi']: + mpi_args = [config['mpiexec'], '-n', config['mpi_np']] + openmc.run(openmc_exec=config['exe'], mpi_args=mpi_args) else: - openmc.run(openmc_exec=self._opts.exe) + openmc.run(openmc_exec=config['exe']) def _test_output_created(self): """Make sure statepoint.* and tallies.out have been created.""" @@ -179,9 +171,9 @@ class ParticleRestartTestHarness(TestHarness): def _run_openmc(self): # Set arguments - args = {'openmc_exec': self._opts.exe} - if self._opts.mpi_exec is not None: - args['mpi_args'] = [self._opts.mpi_exec, '-n', self._opts.mpi_np] + args = {'openmc_exec': config['exe']} + if config['mpi']: + args['mpi_args'] = [config['mpiexec'], '-n', config['mpi_np']] # Initial run openmc.run(**args) @@ -231,8 +223,6 @@ class ParticleRestartTestHarness(TestHarness): class PyAPITestHarness(TestHarness): def __init__(self, statepoint_name, model=None): super(PyAPITestHarness, self).__init__(statepoint_name) - self.parser.add_option('-b', '--build-inputs', dest='build_only', - action='store_true', default=False) if model is None: self._model = pwr_core() else: @@ -242,10 +232,9 @@ class PyAPITestHarness(TestHarness): def main(self): """Accept commandline arguments and either run or update tests.""" - (self._opts, self._args) = self.parser.parse_args() - if self._opts.build_only: + if config['build_inputs']: self._build_inputs() - elif self._opts.update: + elif config['update']: self.update_results() else: self.execute_test() diff --git a/tests/unit_tests/test_capi.py b/tests/unit_tests/test_capi.py index ba9ac5c9e0..e1e87e27b3 100644 --- a/tests/unit_tests/test_capi.py +++ b/tests/unit_tests/test_capi.py @@ -12,6 +12,7 @@ import openmc.capi @pytest.fixture(scope='module') def pincell_model(): """Set up a model to test with and delete files when done""" + openmc.reset_auto_ids() pincell = openmc.examples.pwr_pin_cell() # Add a tally diff --git a/tools/ci/travis-script.sh b/tools/ci/travis-script.sh index 2934cb59a0..468cd0cc8a 100755 --- a/tools/ci/travis-script.sh +++ b/tools/ci/travis-script.sh @@ -1,15 +1,15 @@ #!/bin/bash set -ex -# Run regression test suite -cd build -ctest - # Run source check -cd ../tests +cd tests if [[ $TRAVIS_PYTHON_VERSION == "3.4" && $OMP == 'n' && $MPI == 'n' ]]; then ./check_source.py fi -# Run unit tests -pytest --cov=../openmc -v unit_tests/ +# Run regression and unit tests +if [[ $MPI == 'y' ]]; then + pytest --cov=../openmc -v --mpi +else + pytest --cov=../openmc -v +fi