From 0e18e9881c749b4feb08845430f7d60c13ca1188 Mon Sep 17 00:00:00 2001 From: "Martin K. Scherer" Date: Thu, 21 Jul 2016 15:09:54 +0200 Subject: [PATCH 1/4] [testing] removed nose-specific "setup_package" function from __init__ Replaced by patching constructor of unittest.TestCase --- pyemma/__init__.py | 20 +++++++++++++++----- pyemma/coordinates/__init__.py | 6 ------ pyemma/coordinates/tests/__init__.py | 8 -------- 3 files changed, 15 insertions(+), 19 deletions(-) diff --git a/pyemma/__init__.py b/pyemma/__init__.py index d1b659b0d..95169bca3 100644 --- a/pyemma/__init__.py +++ b/pyemma/__init__.py @@ -38,9 +38,19 @@ from . import thermo -def setup_package(): - # purpose is for nose testing only to silence progress bars etc. - import warnings - warnings.warn('You should never see this, only in unit testing!' - ' This switches off progress bars') +def _setup_testing(): + # setup function for testing + from pyemma.util import config + # do not cache trajectory info in user directory (temp traj files) + config.use_trajectory_lengths_cache = False config.show_progress_bars = False + +import unittest as _unittest +# override unittests base class constructor to achieve same behaviour without nose. +_old_init = _unittest.TestCase.__init__ +def _new_init(self, *args, **kwargs): + _old_init(self, *args, **kwargs) + _setup_testing() + +_unittest.TestCase.__init__ = _new_init + diff --git a/pyemma/coordinates/__init__.py b/pyemma/coordinates/__init__.py index 4383617ae..5e4af7fad 100644 --- a/pyemma/coordinates/__init__.py +++ b/pyemma/coordinates/__init__.py @@ -105,9 +105,3 @@ """ from .api import * - - -def setup_package(): - # do not use traj cache for tests - from pyemma import config - config['use_trajectory_lengths_cache'] = False diff --git a/pyemma/coordinates/tests/__init__.py b/pyemma/coordinates/tests/__init__.py index b1b7e364d..ccfa38071 100644 --- a/pyemma/coordinates/tests/__init__.py +++ b/pyemma/coordinates/tests/__init__.py @@ -16,12 +16,4 @@ # You should have received a copy of the GNU Lesser General Public License # along with this program. If not, see . - from __future__ import absolute_import - - -def setup_package(): - # setup function for nose tests (for this package only) - from pyemma.util import config - # do not cache trajectory info in user directory (temp traj files) - config.use_trajectory_lengths_cache = False From 4d77ddc7d9add2ab3dfbb21ff209e60e8e596f07 Mon Sep 17 00:00:00 2001 From: "Martin K. Scherer" Date: Thu, 21 Jul 2016 15:11:19 +0200 Subject: [PATCH 2/4] [contexts] added ctx manager to temporarily set pyemma config values --- pyemma/util/contexts.py | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/pyemma/util/contexts.py b/pyemma/util/contexts.py index 379ff3f4f..61cd608ca 100644 --- a/pyemma/util/contexts.py +++ b/pyemma/util/contexts.py @@ -62,3 +62,25 @@ def random_seed(seed=42): yield finally: random.setstate(old_state) + + +@contextmanager +def settings(**kwargs): + """ apply given PyEMMA config values temporarily within the given context.""" + from pyemma import config + # validate: + valid_keys = config.keys() + for k in kwargs.keys(): + if k not in valid_keys: + raise ValueError("not a valid settings: {key}".format(key=k)) + + old_settings = {} + for k, v in kwargs.items(): + old_settings[k] = getattr(config, k) + setattr(config, k, v) + + yield + + # restore old settings + for k, v in old_settings.items(): + setattr(config, k, v) From 159b4f78509ecd0fb1061d427e0615536a36c89c Mon Sep 17 00:00:00 2001 From: "Martin K. Scherer" Date: Thu, 21 Jul 2016 15:12:15 +0200 Subject: [PATCH 3/4] use settings context manager --- .../coordinates/tests/test_traj_info_cache.py | 21 ++++++------------- 1 file changed, 6 insertions(+), 15 deletions(-) diff --git a/pyemma/coordinates/tests/test_traj_info_cache.py b/pyemma/coordinates/tests/test_traj_info_cache.py index 9b7a3423d..1e5634729 100644 --- a/pyemma/coordinates/tests/test_traj_info_cache.py +++ b/pyemma/coordinates/tests/test_traj_info_cache.py @@ -22,7 +22,6 @@ from __future__ import absolute_import -from contextlib import contextmanager from tempfile import NamedTemporaryFile @@ -40,6 +39,7 @@ from pyemma.coordinates.tests.util import create_traj from pyemma.datasets import get_bpti_test_data from pyemma.util import config +from pyemma.util.contexts import settings from pyemma.util.files import TemporaryDirectory import mdtraj import pkg_resources @@ -56,8 +56,7 @@ class TestTrajectoryInfoCache(unittest.TestCase): @classmethod def setUpClass(cls): cls.old_instance = TrajectoryInfoCache.instance() - cls.old_show_pg = config.show_progress_bars - config.show_progress_bars = False + config.use_trajectory_lengths_cache = True def setUp(self): self.work_dir = tempfile.mkdtemp("traj_cache_test") @@ -77,7 +76,7 @@ def tearDown(self): @classmethod def tearDownClass(cls): TrajectoryInfoCache._instance = cls.old_instance - config.show_progress_bars = cls.old_show_pg + config.use_trajectory_lengths_cache = False def test_get_instance(self): # test for exceptions in singleton creation @@ -117,9 +116,8 @@ def test_exceptions(self): def test_featurereader_xtc(self): # cause cache failures - config['use_trajectory_lengths_cache'] = False - reader = FeatureReader(xtcfiles, pdbfile) - config['use_trajectory_lengths_cache'] = True + with settings(use_trajectory_lengths_cache=False): + reader = FeatureReader(xtcfiles, pdbfile) results = {} for f in xtcfiles: @@ -265,16 +263,9 @@ def test_max_size(self): data = [np.random.random((150, 10)) for _ in range(150)] max_size = 1 - @contextmanager - def size_ctx(new_size): - old_size = config.traj_info_max_size - config.traj_info_max_size = new_size - yield - config.traj_info_max_size = old_size - files = [] config.show_progress_bars=False - with TemporaryDirectory() as td, size_ctx(max_size): + with TemporaryDirectory() as td, settings(traj_info_max_size=max_size): for i, arr in enumerate(data): f = os.path.join(td, "%s.txt" % i) # save as txt to enforce creation of offsets From 6159b7aef415100b211b1cc57124945aa91b1ce3 Mon Sep 17 00:00:00 2001 From: "Martin K. Scherer" Date: Thu, 21 Jul 2016 15:19:53 +0200 Subject: [PATCH 4/4] fix test --- pyemma/_base/tests/test_progress.py | 55 +++++++---------------------- 1 file changed, 12 insertions(+), 43 deletions(-) diff --git a/pyemma/_base/tests/test_progress.py b/pyemma/_base/tests/test_progress.py index bb7c77e32..f56625432 100644 --- a/pyemma/_base/tests/test_progress.py +++ b/pyemma/_base/tests/test_progress.py @@ -23,58 +23,27 @@ ''' from __future__ import absolute_import + import unittest + +from six.moves import range + from pyemma._base.progress import ProgressReporter from pyemma._base.progress.bar import ProgressBar -from six.moves import range +from pyemma import config class TestProgress(unittest.TestCase): - # FIXME: does not work with nose (because nose already captures stdout) - """ - def test_silenced(self): - reporter = ProgressReporter() - reporter._register(1) - reporter.silence_progress = True - - from StringIO import StringIO - - saved_stdout = sys.stdout - try: - out = StringIO() - sys.stdout = out - # call the update method to potentially create output - reporter._update(1) - output = out.getvalue().strip() - # in silence mode we do not want any output! - assert output == '' - finally: - sys.stdout = saved_stdout - - def test_not_silenced(self): - reporter = ProgressReporter() - reporter._register(1) - reporter.silence_progress = False - - from StringIO import StringIO - - saved_stdout = sys.stdout - try: - out = StringIO() - sys.stdout = out - # call the update method to potentially create output - reporter._update(1) - output = out.getvalue().strip() - # in silence mode we do not want any output! - print output - assert output is not '' - finally: - sys.stdout = saved_stdout - """ + @classmethod + def setUpClass(cls): + config.show_progress_bars = True - def test_callback(self): + @classmethod + def tearDownClass(cls): + config.show_progress_bars = False + def test_callback(self): self.has_been_called = 0 def call_back(stage, progressbar, *args, **kw):