def test_object_from_module(self):
        # try_import('thing.object') imports 'thing' and returns
        # 'thing.object' if 'thing' is a module and 'object' is not.
        result = try_import("os.path.join")
        import os

        self.assertThat(result, Is(os.path.join))
    def test_existing_module(self):
        # try_import('thing', foo) imports 'thing' and returns it if it's a
        # module that exists.
        result = try_import("os", object())
        import os

        self.assertThat(result, Is(os))
    def test_existing_submodule(self):
        # try_import('thing.another', foo) imports 'thing' and returns it if
        # it's a module that exists.
        result = try_import("os.path", object())
        import os

        self.assertThat(result, Is(os.path))
Exemplo n.º 4
0
def hide_testtools_stack(should_hide=True):
    modules = [
        'testtools.matchers',
        'testtools.runtest',
        'testtools.testcase',
        ]
    result = is_stack_hidden()
    for module_name in modules:
        module = try_import(module_name)
        if should_hide:
            setattr(module, '__unittest', True)
        else:
            try:
                delattr(module, '__unittest')
            except AttributeError:
                # Attribute already doesn't exist. Our work here is done.
                pass
    return result
Exemplo n.º 5
0
    KeysEqual,
    MatchesDict,
    MatchesException,
    MatchesListwise,
    Not,
    Raises,
)
from testtools.runtest import RunTest
from testtools.testresult.doubles import ExtendedTestResult
from testtools.tests.helpers import (
    AsText,
    MatchesEvents,
)
from ._helpers import NeedsTwistedTestCase

DebugTwisted = try_import(
    'testtools.twistedsupport._deferreddebug.DebugTwisted')

assert_fails_with = try_import('testtools.twistedsupport.assert_fails_with')
AsynchronousDeferredRunTest = try_import(
    'testtools.twistedsupport.AsynchronousDeferredRunTest')
flush_logged_errors = try_import(
    'testtools.twistedsupport.flush_logged_errors')
SynchronousDeferredRunTest = try_import(
    'testtools.twistedsupport.SynchronousDeferredRunTest')

defer = try_import('twisted.internet.defer')
failure = try_import('twisted.python.failure')
log = try_import('twisted.python.log')
DelayedCall = try_import('twisted.internet.base.DelayedCall')
_get_global_publisher_and_observers = try_import(
    'testtools.twistedsupport._runtest._get_global_publisher_and_observers')
Exemplo n.º 6
0
 def test_object_from_module(self):
     # try_import('thing.object') imports 'thing' and returns
     # 'thing.object' if 'thing' is a module and 'object' is not.
     result = try_import('os.path.join')
     import os
     self.assertThat(result, Is(os.path.join))
Exemplo n.º 7
0
import sys
from textwrap import dedent
import unittest
from unittest import TestSuite

import testtools
from testtools import TestCase, run, skipUnless
from testtools.compat import _b
from testtools.helpers import try_import
from testtools.matchers import (
    Contains,
    DocTestMatches,
    MatchesRegex,
)

fixtures = try_import('fixtures')
testresources = try_import('testresources')

if fixtures:

    class SampleTestFixture(fixtures.Fixture):
        """Creates testtools.runexample temporarily."""
        def __init__(self, broken=False):
            """Create a SampleTestFixture.

            :param broken: If True, the sample file will not be importable.
            """
            if not broken:
                init_contents = _b("""\
from testtools import TestCase
Exemplo n.º 8
0
# Copyright (c) 2010-2011 testtools developers. See LICENSE for details.

import unittest

from testtools import TestCase, content, content_type
from testtools.compat import _b, _u
from testtools.helpers import try_import
from testtools.testresult.doubles import ExtendedTestResult

fixtures = try_import("fixtures")
LoggingFixture = try_import("fixtures.tests.helpers.LoggingFixture")


class TestFixtureSupport(TestCase):
    def setUp(self):
        super(TestFixtureSupport, self).setUp()
        if fixtures is None or LoggingFixture is None:
            self.skipTest("Need fixtures")

    def test_useFixture(self):
        fixture = LoggingFixture()

        class SimpleTest(TestCase):
            def test_foo(self):
                self.useFixture(fixture)

        result = unittest.TestResult()
        SimpleTest("test_foo").run(result)
        self.assertTrue(result.wasSuccessful())
        self.assertEqual(["setUp", "cleanUp"], fixture.calls)
Exemplo n.º 9
0
# Copyright (c) 2010, 2016 testtools developers. See LICENSE for details.

__all__ = [
    'NeedsTwistedTestCase',
]

from testtools.helpers import try_import
from testtools import TestCase

defer = try_import('twisted.internet.defer')


class NeedsTwistedTestCase(TestCase):
    def setUp(self):
        super().setUp()
        if defer is None:
            self.skipTest("Need Twisted to run")
Exemplo n.º 10
0
# Copyright (c) 2010 testtools developers. See LICENSE for details.

"""Tests for the DeferredRunTest single test execution logic."""

import os
import signal

from testtools import skipIf, TestCase
from testtools.content import text_content
from testtools.helpers import try_import
from testtools.tests.helpers import ExtendedTestResult
from testtools.matchers import Equals, KeysEqual, MatchesException, Raises
from testtools.runtest import RunTest
from testtools.tests.test_spinner import NeedsTwistedTestCase

assert_fails_with = try_import("testtools.deferredruntest.assert_fails_with")
AsynchronousDeferredRunTest = try_import("testtools.deferredruntest.AsynchronousDeferredRunTest")
flush_logged_errors = try_import("testtools.deferredruntest.flush_logged_errors")
SynchronousDeferredRunTest = try_import("testtools.deferredruntest.SynchronousDeferredRunTest")

defer = try_import("twisted.internet.defer")
failure = try_import("twisted.python.failure")
log = try_import("twisted.python.log")
DelayedCall = try_import("twisted.internet.base.DelayedCall")


class X(object):
    """Tests that we run as part of our tests, nested to avoid discovery."""

    class Base(TestCase):
        def setUp(self):
    )
from testtools.content import (
    text_content,
    )
from testtools.helpers import try_import
from testtools.matchers import (
    Equals,
    KeysEqual,
    MatchesException,
    Raises,
    )
from testtools.runtest import RunTest
from testtools.testresult.doubles import ExtendedTestResult
from testtools.tests.test_spinner import NeedsTwistedTestCase

assert_fails_with = try_import('testtools.deferredruntest.assert_fails_with')
AsynchronousDeferredRunTest = try_import(
    'testtools.deferredruntest.AsynchronousDeferredRunTest')
flush_logged_errors = try_import(
    'testtools.deferredruntest.flush_logged_errors')
SynchronousDeferredRunTest = try_import(
    'testtools.deferredruntest.SynchronousDeferredRunTest')

defer = try_import('twisted.internet.defer')
failure = try_import('twisted.python.failure')
log = try_import('twisted.python.log')
DelayedCall = try_import('twisted.internet.base.DelayedCall')


class X(object):
    """Tests that we run as part of our tests, nested to avoid discovery."""
Exemplo n.º 12
0
 def test_nonexistent_submodule(self):
     # try_import('thing.another', foo) imports 'thing' and returns foo if
     # 'another' doesn't exist.
     marker = object()
     result = try_import('os.doesntexist', marker)
     self.assertThat(result, Is(marker))
Exemplo n.º 13
0
 def test_None_is_default_alternative(self):
     # try_import('thing') returns None if 'thing' doesn't exist.
     result = try_import('doesntexist')
     self.assertThat(result, Is(None))
Exemplo n.º 14
0
# Copyright (c) 2010-2011 testtools developers. See LICENSE for details.

import unittest

from testtools import (
    TestCase,
    content,
    content_type,
)
from testtools.compat import _b, _u
from testtools.helpers import try_import
from testtools.testresult.doubles import (
    ExtendedTestResult, )

fixtures = try_import('fixtures')
LoggingFixture = try_import('fixtures.tests.helpers.LoggingFixture')


class TestFixtureSupport(TestCase):
    def setUp(self):
        super(TestFixtureSupport, self).setUp()
        if fixtures is None or LoggingFixture is None:
            self.skipTest("Need fixtures")

    def test_useFixture(self):
        fixture = LoggingFixture()

        class SimpleTest(TestCase):
            def test_foo(self):
                self.useFixture(fixture)
Exemplo n.º 15
0
from testtools import (
    ConcurrentTestSuite,
    ConcurrentStreamTestSuite,
    iterate_tests,
    PlaceHolder,
    TestByTestResult,
    TestCase,
    )
from testtools.helpers import try_import
from testtools.matchers import DocTestMatches, Equals
from testtools.testresult.doubles import StreamResult as LoggingStream
from testtools.testsuite import FixtureSuite, sorted_tests
from testtools.tests.helpers import LoggingResult

FunctionFixture = try_import('fixtures.FunctionFixture')


class Sample(TestCase):
    def __hash__(self):
        return id(self)
    def test_method1(self):
        pass
    def test_method2(self):
        pass


class TestConcurrentTestSuiteRun(TestCase):

    def test_broken_test(self):
        log = []
# Copyright (c) 2010 testtools developers. See LICENSE for details.

import unittest

from testtools import (
    TestCase,
    content,
    content_type,
    )
from testtools.helpers import try_import
from testtools.tests.helpers import (
    ExtendedTestResult,
    )

fixtures = try_import('fixtures')
LoggingFixture = try_import('fixtures.tests.helpers.LoggingFixture')


class TestFixtureSupport(TestCase):

    def setUp(self):
        super(TestFixtureSupport, self).setUp()
        if fixtures is None or LoggingFixture is None:
            self.skipTest("Need fixtures")

    def test_useFixture(self):
        fixture = LoggingFixture()
        class SimpleTest(TestCase):
            def test_foo(self):
                self.useFixture(fixture)
        result = unittest.TestResult()
Exemplo n.º 17
0
# Copyright (c) 2010 testtools developers. See LICENSE for details.
"""Tests for the test runner logic."""

from testtools.compat import (
    _b,
    StringIO,
)
from testtools.helpers import try_import
fixtures = try_import('fixtures')

import testtools
from testtools import TestCase, run

if fixtures:

    class SampleTestFixture(fixtures.Fixture):
        """Creates testtools.runexample temporarily."""
        def __init__(self):
            self.package = fixtures.PythonPackage('runexample',
                                                  [('__init__.py',
                                                    _b("""
from testtools import TestCase

class TestFoo(TestCase):
    def test_bar(self):
        pass
    def test_quux(self):
        pass
def test_suite():
    from unittest import TestLoader
    return TestLoader().loadTestsFromName(__name__)
Exemplo n.º 18
0
# Copyright (c) testtools developers. See LICENSE for details.
"""Tests for testtools._deferred."""

from testtools.helpers import try_import
from testtools.matchers import (
    Equals,
    MatchesException,
    Raises,
)
from ._helpers import NeedsTwistedTestCase

DeferredNotFired = try_import(
    'testtools.twistedsupport._deferred.DeferredNotFired')
extract_result = try_import(
    'testtools.twistedsupport._deferred.extract_result')

defer = try_import('twisted.internet.defer')
Failure = try_import('twisted.python.failure.Failure')


class TestExtractResult(NeedsTwistedTestCase):
    """Tests for ``extract_result``."""
    def test_not_fired(self):
        # _spinner.extract_result raises _spinner.DeferredNotFired if it's
        # given a Deferred that has not fired.
        self.assertThat(lambda: extract_result(defer.Deferred()),
                        Raises(MatchesException(DeferredNotFired)))

    def test_success(self):
        # _spinner.extract_result returns the value of the Deferred if it has
        # fired successfully.
Exemplo n.º 19
0
import sys

import six
from testtools.compat import (
    advance_iterator,
    reraise,
    )
from testtools.helpers import try_import

from fixtures.callmany import (
    CallMany,
    # Deprecated, imported for compatibility.
    MultipleExceptions,
    )

gather_details = try_import("testtools.testcase.gather_details")

# This would be better in testtools (or a common library)
def combine_details(source_details, target_details):
    """Add every value from source to target deduping common keys."""
    for name, content_object in source_details.items():
        new_name = name
        disambiguator = itertools.count(1)
        while new_name in target_details:
            new_name = '%s-%d' % (name, advance_iterator(disambiguator))
        name = new_name
        target_details[name] = content_object


class SetupError(Exception):
    """Setup failed.
Exemplo n.º 20
0
    TestResult,
)
from testtools.content import (
    text_content, )
from testtools.helpers import try_import
from testtools.matchers import (
    Equals,
    KeysEqual,
    MatchesException,
    Raises,
)
from testtools.runtest import RunTest
from testtools.testresult.doubles import ExtendedTestResult
from testtools.tests.test_spinner import NeedsTwistedTestCase

assert_fails_with = try_import('testtools.deferredruntest.assert_fails_with')
AsynchronousDeferredRunTest = try_import(
    'testtools.deferredruntest.AsynchronousDeferredRunTest')
flush_logged_errors = try_import(
    'testtools.deferredruntest.flush_logged_errors')
SynchronousDeferredRunTest = try_import(
    'testtools.deferredruntest.SynchronousDeferredRunTest')

defer = try_import('twisted.internet.defer')
failure = try_import('twisted.python.failure')
log = try_import('twisted.python.log')
DelayedCall = try_import('twisted.internet.base.DelayedCall')


class X(object):
    """Tests that we run as part of our tests, nested to avoid discovery."""
Exemplo n.º 21
0
import os
import signal

from testtools import (
    skipIf,
    TestCase,
    )
from testtools.helpers import try_import
from testtools.matchers import (
    Equals,
    Is,
    MatchesException,
    Raises,
    )

_spinner = try_import('testtools._spinner')

defer = try_import('twisted.internet.defer')
Failure = try_import('twisted.python.failure.Failure')


class NeedsTwistedTestCase(TestCase):

    def setUp(self):
        super(NeedsTwistedTestCase, self).setUp()
        if defer is None or Failure is None:
            self.skipTest("Need Twisted to run")


class TestNotReentrant(NeedsTwistedTestCase):
Exemplo n.º 22
0
import os
import signal

from testtools import (
    skipIf,
    TestCase,
)
from testtools.helpers import try_import
from testtools.matchers import (
    Equals,
    Is,
    MatchesException,
    Raises,
)

_spinner = try_import('testtools._spinner')

defer = try_import('twisted.internet.defer')
Failure = try_import('twisted.python.failure.Failure')


class NeedsTwistedTestCase(TestCase):
    def setUp(self):
        super(NeedsTwistedTestCase, self).setUp()
        if defer is None or Failure is None:
            self.skipTest("Need Twisted to run")


class TestNotReentrant(NeedsTwistedTestCase):
    def test_not_reentrant(self):
        # A function decorated as not being re-entrant will raise a
Exemplo n.º 23
0
 def test_doesnt_exist(self):
     # try_import('thing', foo) returns foo if 'thing' doesn't exist.
     marker = object()
     result = try_import('doesntexist', marker)
     self.assertThat(result, Is(marker))
Exemplo n.º 24
0
 def test_doesnt_exist(self):
     # try_import('thing', foo) returns foo if 'thing' doesn't exist.
     marker = object()
     result = try_import('doesntexist', marker)
     self.assertThat(result, Is(marker))
Exemplo n.º 25
0
import itertools
import sys

from testtools.compat import (
    advance_iterator,
    reraise,
    )
from testtools.helpers import try_import

from fixtures.callmany import (
    CallMany,
    # Deprecated, imported for compatibility.
    MultipleExceptions,
    )

gather_details = try_import("testtools.testcase.gather_details")

# This would be better in testtools (or a common library)
def combine_details(source_details, target_details):
    """Add every value from source to target deduping common keys."""
    for name, content_object in source_details.items():
        new_name = name
        disambiguator = itertools.count(1)
        while new_name in target_details:
            new_name = '%s-%d' % (name, advance_iterator(disambiguator))
        name = new_name
        target_details[name] = content_object


class Fixture(object):
    """A Fixture representing some state or resource.
Exemplo n.º 26
0
 def test_None_is_default_alternative(self):
     # try_import('thing') returns None if 'thing' doesn't exist.
     result = try_import('doesntexist')
     self.assertThat(result, Is(None))
Exemplo n.º 27
0
__all__ = [
    'CallMany',
    ]

import sys

from testtools.compat import (
    reraise,
    )
from testtools.helpers import try_import


class MultipleExceptions(Exception):
    """Report multiple exc_info tuples in self.args."""

MultipleExceptions = try_import(
    "testtools.MultipleExceptions", MultipleExceptions)


class CallMany(object):
    """A stack of functions which will all be called on __call__.

    CallMany also acts as a context manager for convenience.

    Functions are called in last pushed first executed order.

    This is used by Fixture to manage its addCleanup feature.
    """

    def __init__(self):
        self._cleanups = []
Exemplo n.º 28
0
 def test_existing_module(self):
     # try_import('thing', foo) imports 'thing' and returns it if it's a
     # module that exists.
     result = try_import('os', object())
     import os
     self.assertThat(result, Is(os))
Exemplo n.º 29
0
__metaclass__ = type

import unittest

from testtools import (
    ConcurrentTestSuite,
    iterate_tests,
    PlaceHolder,
    TestCase,
    )
from testtools.helpers import try_import
from testtools.testsuite import FixtureSuite, iterate_tests, sorted_tests
from testtools.tests.helpers import LoggingResult

FunctionFixture = try_import('fixtures.FunctionFixture')

class Sample(TestCase):
    def __hash__(self):
        return id(self)
    def test_method1(self):
        pass
    def test_method2(self):
        pass

class TestConcurrentTestSuiteRun(TestCase):

    def test_trivial(self):
        log = []
        result = LoggingResult(log)
        test1 = Sample('test_method1')
Exemplo n.º 30
0
 def test_existing_submodule(self):
     # try_import('thing.another', foo) imports 'thing' and returns it if
     # it's a module that exists.
     result = try_import('os.path', object())
     import os
     self.assertThat(result, Is(os.path))
# Copyright (c) 2010-2011 Testtools authors. See LICENSE for details.

"""Tests for the distutils test command logic."""

from distutils.dist import Distribution

from testtools.helpers import try_import, try_imports
fixtures = try_import('fixtures')
StringIO = try_imports(['StringIO.StringIO', 'io.StringIO'])

import testtools
from testtools import TestCase
from testtools.distutilscmd import TestCommand


if fixtures:
    class SampleTestFixture(fixtures.Fixture):
        """Creates testtools.runexample temporarily."""

        def __init__(self):
            self.package = fixtures.PythonPackage(
            'runexample', [('__init__.py', """
from testtools import TestCase

class TestFoo(TestCase):
    def test_bar(self):
        pass
    def test_quux(self):
        pass
def test_suite():
    from unittest import TestLoader
Exemplo n.º 32
0
 def test_nonexistent_submodule(self):
     # try_import('thing.another', foo) imports 'thing' and returns foo if
     # 'another' doesn't exist.
     marker = object()
     result = try_import('os.doesntexist', marker)
     self.assertThat(result, Is(marker))