Changeset - 60c48410996e
[Not reviewed]
beta
0 1 0
Marcin Kuzminski - 13 years ago 2012-07-06 00:12:29
marcin@python-works.com
fixed nose_parametrized for py25 compat
1 file changed with 1 insertions and 1 deletions:
0 comments (0 inline, 0 general)
rhodecode/tests/nose_parametrized.py
Show inline comments
 
@@ -113,97 +113,97 @@ def parameterized_expand(input):
 
        """
 

	
 
    def parameterized_expand_wrapper(f):
 
        stack = inspect.stack()
 
        frame = stack[1]
 
        frame_locals = frame[0].f_locals
 

	
 
        base_name = f.__name__
 
        for num, args in enumerate(input):
 
            name_suffix = "_%s" % (num,)
 
            if len(args) > 0 and isinstance(args[0], basestring):
 
                name_suffix += "_" + to_safe_name(args[0])
 
            name = base_name + name_suffix
 
            new_func = parameterized_expand_helper(name, f, args)
 
            frame_locals[name] = new_func
 
        return nottest(f)
 
    return parameterized_expand_wrapper
 

	
 
parameterized.expand = parameterized_expand
 

	
 

	
 
def assert_contains(haystack, needle):
 
    if needle not in haystack:
 
        raise AssertionError("%r not in %r" % (needle, haystack))
 

	
 

	
 
def assert_not_contains(haystack, needle):
 
    if needle in haystack:
 
        raise AssertionError("%r in %r" % (needle, haystack))
 

	
 

	
 
def imported_from_test():
 
    """ Returns true if it looks like this module is being imported by unittest
 
        or nose. """
 
    import re
 
    import inspect
 
    nose_re = re.compile(r"\bnose\b")
 
    unittest_re = re.compile(r"\bunittest2?\b")
 
    for frame in inspect.stack():
 
        file = frame[1]
 
        if nose_re.search(file) or unittest_re.search(file):
 
            return True
 
    return False
 

	
 

	
 
def assert_raises(func, exc_type, str_contains=None, repr_contains=None):
 
    try:
 
        func()
 
    except exc_type as e:
 
    except exc_type, e:
 
        if str_contains is not None and str_contains not in str(e):
 
            raise AssertionError("%s raised, but %r does not contain %r"
 
                                 % (exc_type, str(e), str_contains))
 
        if repr_contains is not None and repr_contains not in repr(e):
 
            raise AssertionError("%s raised, but %r does not contain %r"
 
                                 % (exc_type, repr(e), repr_contains))
 
        return e
 
    else:
 
        raise AssertionError("%s not raised" % (exc_type,))
 

	
 

	
 
log_handler = None
 

	
 

	
 
def setup_logging():
 
    """ Configures a log handler which will capure log messages during a test.
 
        The ``logged_messages`` and ``assert_no_errors_logged`` functions can be
 
        used to make assertions about these logged messages.
 

	
 
        For example::
 

	
 
            from ensi_common.testing import (
 
                setup_logging, teardown_logging, assert_no_errors_logged,
 
                assert_logged,
 
            )
 

	
 
            class TestWidget(object):
 
                def setup(self):
 
                    setup_logging()
 

	
 
                def teardown(self):
 
                    assert_no_errors_logged()
 
                    teardown_logging()
 

	
 
                def test_that_will_fail(self):
 
                    log.warning("this warning message will trigger a failure")
 

	
 
                def test_that_will_pass(self):
 
                    log.info("but info messages are ok")
 
                    assert_logged("info messages are ok")
 
        """
 

	
 
    global log_handler
 
    if log_handler is not None:
 
        logging.getLogger().removeHandler(log_handler)
 
    log_handler = logging.handlers.BufferingHandler(1000)
 
    formatter = logging.Formatter("%(name)s: %(levelname)s: %(message)s")
 
    log_handler.setFormatter(formatter)
0 comments (0 inline, 0 general)