Skip to content
This repository has been archived by the owner on Sep 11, 2023. It is now read-only.

Commit

Permalink
Merge pull request #877 from marscher/cleanup_testing_fixture
Browse files Browse the repository at this point in the history
Cleanup testing fixture
  • Loading branch information
marscher authored Jul 21, 2016
2 parents 502e948 + 6159b7a commit e1bd9b2
Show file tree
Hide file tree
Showing 6 changed files with 55 additions and 77 deletions.
20 changes: 15 additions & 5 deletions pyemma/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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

55 changes: 12 additions & 43 deletions pyemma/_base/tests/test_progress.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down
6 changes: 0 additions & 6 deletions pyemma/coordinates/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 0 additions & 8 deletions pyemma/coordinates/tests/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,4 @@
# You should have received a copy of the GNU Lesser General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.


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
21 changes: 6 additions & 15 deletions pyemma/coordinates/tests/test_traj_info_cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@

from __future__ import absolute_import

from contextlib import contextmanager
from tempfile import NamedTemporaryFile


Expand All @@ -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
Expand All @@ -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")
Expand All @@ -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
Expand Down Expand Up @@ -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:
Expand Down Expand Up @@ -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
Expand Down
22 changes: 22 additions & 0 deletions pyemma/util/contexts.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

0 comments on commit e1bd9b2

Please sign in to comment.