Пример #1
0
class TestCase(unittest.TestCase):
    """Base class for test cases for kgb."""

    ws_re = re.compile(r'\s+')

    def setUp(self):
        self.agency = SpyAgency()
        self.orig_class_do_math = MathClass.class_do_math

    def tearDown(self):
        MathClass.class_do_math = self.orig_class_do_math
        self.agency.unspy_all()

    def shortDescription(self):
        """Return the description of the current test.

        This changes the default behavior to replace all newlines with spaces,
        allowing a test description to span lines. It should still be kept
        short, though.

        Returns:
            bytes:
            The description of the test.
        """
        doc = self._testMethodDoc

        if doc is not None:
            doc = doc.split('\n\n', 1)[0]
            doc = self.ws_re.sub(' ', doc).strip()

        return doc
Пример #2
0
class TestCase(unittest.TestCase):
    """Base class for test cases for kgb."""

    ws_re = re.compile(r'\s+')

    def setUp(self):
        self.agency = SpyAgency()
        self.orig_class_do_math = MathClass.class_do_math

    def tearDown(self):
        MathClass.class_do_math = self.orig_class_do_math
        self.agency.unspy_all()

    def shortDescription(self):
        """Return the description of the current test.

        This changes the default behavior to replace all newlines with spaces,
        allowing a test description to span lines. It should still be kept
        short, though.

        Returns:
            bytes:
            The description of the test.
        """
        doc = self._testMethodDoc

        if doc is not None:
            doc = doc.split('\n\n', 1)[0]
            doc = self.ws_re.sub(' ', doc).strip()

        return doc

    def make_func(self, code_str, func_name='func'):
        """Return a new function, created by the supplied Python code.

        This is used to create functions with signatures that depend on a
        specific version of Python, and would generate syntax errors on
        earlier versions.

        Args:
            code_str (unicode):
                The Python code used to create the function.

            func_name (unicode, optional):
                The expected name of the function.

        Returns:
            callable:
            The resulting function.

        Raises:
            Exception:
                There was an error with the supplied code.
        """
        scope = {}
        exec(textwrap.dedent(code_str), scope)

        return scope[func_name]
Пример #3
0
class BaseTestCase(unittest.TestCase):
    """Base class for test cases for kgb."""
    def setUp(self):
        self.agency = SpyAgency()
        self.orig_class_do_math = MathClass.class_do_math

    def tearDown(self):
        MathClass.class_do_math = self.orig_class_do_math
        self.agency.unspy_all()
Пример #4
0
class BaseTestCase(unittest.TestCase):
    """Base class for test cases for kgb."""

    def setUp(self):
        self.agency = SpyAgency()
        self.orig_class_do_math = MathClass.class_do_math

    def tearDown(self):
        MathClass.class_do_math = self.orig_class_do_math
        self.agency.unspy_all()
Пример #5
0
def spy_on(*args, **kwargs):
    """Spies on a function.

    By default, the spy will allow the call to go through to the original
    function. This can be disabled by passing call_original=False when
    initiating the spy. If disabled, the original function will never be
    called.

    This can also be passed a call_fake parameter pointing to another
    function to call instead of the original. If passed, this will take
    precedence over call_original.

    The spy will only remain throughout the duration of the context.
    """
    agency = SpyAgency()
    spy = agency.spy_on(*args, **kwargs)

    yield spy

    spy.unspy()
Пример #6
0
def spy_on(*args, **kwargs):
    """Spy on a function.

    By default, the spy will allow the call to go through to the original
    function. This can be disabled by passing ``call_original=False`` when
    initiating the spy. If disabled, the original function will never be
    called.

    This can also be passed a ``call_fake`` parameter pointing to another
    function to call instead of the original. If passed, this will take
    precedence over ``call_original``.

    The spy will only remain throughout the duration of the context.

    See :py:class:`~kgb.spies.FunctionSpy` for more details on arguments.

    Args:
        *args (tuple):
            Positional arguments to pass to
            :py:class:`~kgb.spies.FunctionSpy`.

        **kwargs (dict):
            Keyword arguments to pass to
            :py:class:`~kgb.spies.FunctionSpy`.

    Context:
        kgb.spies.FunctionSpy:
        The newly-created spy.
    """
    agency = SpyAgency()
    spy = agency.spy_on(*args, **kwargs)

    try:
        yield spy
    finally:
        spy.unspy()
Пример #7
0
 def setUp(self):
     self.agency = SpyAgency()
     self.orig_class_do_math = MathClass.class_do_math
Пример #8
0
 def setUp(self):
     self.agency = SpyAgency()
     self.orig_class_do_math = MathClass.class_do_math
Пример #9
0
"""Independent assertion functions.

These assertion functions can be used in pytest or in other places where
:py:class:`kgb.SpyAgency` can't be mixed.

Version Added:
    7.0
"""

from __future__ import unicode_literals

from kgb.agency import SpyAgency

_agency = SpyAgency()

__all__ = []

for name in vars(SpyAgency):
    if name.startswith('assert_'):
        globals()[name] = getattr(_agency, name)
        __all__.append(name)