Esempio n. 1
0
    def test_test_address(self):
        # test addresses are specified as
        #     package.module:class.method
        #     /path/to/file.py:class.method
        # converted into 3-tuples (file, module, callable)
        # all terms optional
        test_address = util.test_address
        absfile = util.absfile
        class Foo:
            def bar(self):
                pass
        def baz():
            pass

        f = Foo()

        class FooTC(unittest.TestCase):
            def test_one(self):
                pass
            def test_two(self):
                pass

        class CustomTestType(type):
            pass
        class CustomTC(unittest.TestCase):
            __metaclass__ = CustomTestType
            def test_one(self):
                pass
            def test_two(self):
                pass

        foo_funct = case.FunctionTestCase(baz)
        foo_functu = unittest.FunctionTestCase(baz)

        foo_mtc = case.MethodTestCase(unbound_method(Foo, Foo.bar))

        me = util.src(absfile(__file__))
        self.assertEqual(test_address(baz),
                         (me, __name__, 'baz'))
        assert test_address(Foo) == (me, __name__, 'Foo')
        assert test_address(unbound_method(Foo, Foo.bar)) == (me, __name__,
                                                              'Foo.bar')
        assert test_address(f) == (me, __name__, 'Foo')
        assert test_address(f.bar) == (me, __name__, 'Foo.bar')
        assert test_address(nose) == (
            util.src(absfile(nose.__file__)), 'nose', None)

        # test passing the actual test callable, as the
        # missed test plugin must do
        self.assertEqual(test_address(FooTC('test_one')),
                         (me, __name__, 'FooTC.test_one'))
        self.assertEqual(test_address(CustomTC('test_one')),
                         (me, __name__, 'CustomTC.test_one'))
        self.assertEqual(test_address(foo_funct),
                         (me, __name__, 'baz'))
        self.assertEqual(test_address(foo_functu),
                         (me, __name__, 'baz'))
        self.assertEqual(test_address(foo_mtc),
                         (me, __name__, 'Foo.bar'))
    def test_test_address(self):
        # test addresses are specified as
        #     package.module:class.method
        #     /path/to/file.py:class.method
        # converted into 3-tuples (file, module, callable)
        # all terms optional
        test_address = util.test_address
        absfile = util.absfile
        class Foo:
            def bar(self):
                pass
        def baz():
            pass

        f = Foo()

        class FooTC(unittest.TestCase):
            def test_one(self):
                pass
            def test_two(self):
                pass

        class CustomTestType(type):
            pass
        class CustomTC(unittest.TestCase, metaclass=CustomTestType):
            def test_one(self):
                pass
            def test_two(self):
                pass

        foo_funct = case.FunctionTestCase(baz)
        foo_functu = unittest.FunctionTestCase(baz)

        foo_mtc = case.MethodTestCase(unbound_method(Foo, Foo.bar))

        me = util.src(absfile(__file__))
        self.assertEqual(test_address(baz),
                         (me, __name__, 'baz'))
        assert test_address(Foo) == (me, __name__, 'Foo')
        assert test_address(unbound_method(Foo, Foo.bar)) == (me, __name__,
                                                              'Foo.bar')
        assert test_address(f) == (me, __name__, 'Foo')
        assert test_address(f.bar) == (me, __name__, 'Foo.bar')
        assert test_address(nose) == (
            util.src(absfile(nose.__file__)), 'nose', None)

        # test passing the actual test callable, as the
        # missed test plugin must do
        self.assertEqual(test_address(FooTC('test_one')),
                         (me, __name__, 'FooTC.test_one'))
        self.assertEqual(test_address(CustomTC('test_one')),
                         (me, __name__, 'CustomTC.test_one'))
        self.assertEqual(test_address(foo_funct),
                         (me, __name__, 'baz'))
        self.assertEqual(test_address(foo_functu),
                         (me, __name__, 'baz'))
        self.assertEqual(test_address(foo_mtc),
                         (me, __name__, 'Foo.bar'))
    def test_address(self):
        from nose.util import absfile, src

        class TC(unittest.TestCase):
            def runTest(self):
                raise Exception("error")

        def dummy(i):
            pass

        def test():
            pass

        class Test:
            def test(self):
                pass

            def test_gen(self):
                def tryit(i):
                    pass

                for i in range(0, 2):
                    yield tryit, i

            def try_something(self, a, b):
                pass

        fl = src(absfile(__file__))
        case = nose.case.Test(TC())
        self.assertEqual(case.address(), (fl, __name__, 'TC.runTest'))

        case = nose.case.Test(nose.case.FunctionTestCase(test))
        self.assertEqual(case.address(), (fl, __name__, 'test'))

        case = nose.case.Test(
            nose.case.FunctionTestCase(dummy, arg=(1, ), descriptor=test))
        self.assertEqual(case.address(), (fl, __name__, 'test'))

        case = nose.case.Test(
            nose.case.MethodTestCase(unbound_method(Test, Test.test)))
        self.assertEqual(case.address(), (fl, __name__, 'Test.test'))

        case = nose.case.Test(
            nose.case.MethodTestCase(unbound_method(Test, Test.try_something),
                                     arg=(
                                         1,
                                         2,
                                     ),
                                     descriptor=unbound_method(
                                         Test, Test.test_gen)))
        self.assertEqual(case.address(), (fl, __name__, 'Test.test_gen'))

        case = nose.case.Test(
            nose.case.MethodTestCase(unbound_method(Test, Test.test_gen),
                                     test=dummy,
                                     arg=(1, )))
        self.assertEqual(case.address(), (fl, __name__, 'Test.test_gen'))
Esempio n. 4
0
    def test_address(self):
        from nose.util import absfile, src
        class TC(unittest.TestCase):
            def runTest(self):
                raise Exception("error")

        def dummy(i):
            pass

        def test():
            pass

        class Test:
            def test(self):
                pass

            def test_gen(self):
                def tryit(i):
                    pass
                for i in range (0, 2):
                    yield tryit, i

            def try_something(self, a, b):
                pass

        fl = src(absfile(__file__))
        case = nose.case.Test(TC())
        self.assertEqual(case.address(), (fl, __name__, 'TC.runTest'))

        case = nose.case.Test(nose.case.FunctionTestCase(test))
        self.assertEqual(case.address(), (fl, __name__, 'test'))

        case = nose.case.Test(nose.case.FunctionTestCase(
            dummy, arg=(1,), descriptor=test))
        self.assertEqual(case.address(), (fl, __name__, 'test'))

        case = nose.case.Test(nose.case.MethodTestCase(
                                  unbound_method(Test, Test.test)))
        self.assertEqual(case.address(), (fl, __name__, 'Test.test'))

        case = nose.case.Test(
            nose.case.MethodTestCase(unbound_method(Test, Test.try_something),
                                     arg=(1,2,),
                                     descriptor=unbound_method(Test,
                                                               Test.test_gen)))
        self.assertEqual(case.address(),
                         (fl, __name__, 'Test.test_gen'))

        case = nose.case.Test(
            nose.case.MethodTestCase(unbound_method(Test, Test.test_gen),
                                     test=dummy, arg=(1,)))
        self.assertEqual(case.address(),
                         (fl, __name__, 'Test.test_gen'))
Esempio n. 5
0
 def generate(g=generator, c=cls):
     try:
         for test in g():
             test_func, arg = self.parseGeneratedTest(test)
             if not callable(test_func):
                 test_func = unbound_method(c, getattr(c, test_func))
             if ismethod(test_func):
                 yield MethodTestCase(test_func, arg=arg, descriptor=g)
             elif callable(test_func):
                 # In this case we're forcing the 'MethodTestCase'
                 # to run the inline function as its test call,
                 # but using the generator method as the 'method of
                 # record' (so no need to pass it as the descriptor)
                 yield MethodTestCase(g, test=test_func, arg=arg)
             else:
                 yield Failure(
                     TypeError,
                     "%s is not a callable or method" % test_func)
     except KeyboardInterrupt:
         raise
     except:
         exc = sys.exc_info()
         yield Failure(exc[0],
                       exc[1],
                       exc[2],
                       address=test_address(generator))
Esempio n. 6
0
 def wanted(attr, cls=cls, sel=self.selector):
     item = getattr(cls, attr, None)
     if isfunction(item):
         item = unbound_method(cls, item)
     elif not ismethod(item):
         return False
     return sel.wantMethod(item)
Esempio n. 7
0
 def wanted(attr, cls=cls, sel=self.selector):
     item = getattr(cls, attr, None)
     if isfunction(item):
         item = unbound_method(cls, item)
     elif not ismethod(item):
         return False
     return sel.wantMethod(item)
Esempio n. 8
0
    def _makeTest(self, obj, parent=None):
        """Given a test object and its parent, return a test case
        or test suite.
        """
        plug_tests = []
        try:
            addr = test_address(obj)
        except KeyboardInterrupt:
            raise
        except:
            addr = None
        for test in self.config.plugins.makeTest(obj, parent):
            plug_tests.append(test)
        # TODO: is this try/except needed?
        try:
            if plug_tests:
                return self.suiteClass(plug_tests)
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            exc = sys.exc_info()
            return Failure(exc[0], exc[1], exc[2], address=addr)
        
        if isfunction(obj) and parent and not isinstance(parent, types.ModuleType):
	    # This is a Python 3.x 'unbound method'.  Wrap it with its
	    # associated class..
            obj = unbound_method(parent, obj)

        if isinstance(obj, unittest.TestCase):
            return obj
        elif isclass(obj):
            if parent and obj.__module__ != parent.__name__:
                obj = transplant_class(obj, parent.__name__)
            if issubclass(obj, unittest.TestCase):
                return self.loadTestsFromTestCase(obj)
            else:
                return self.loadTestsFromTestClass(obj)
        elif ismethod(obj):
            if parent is None:
                parent = obj.__class__
            if issubclass(parent, unittest.TestCase):
                return parent(obj.__name__)
            else:
                if isgenerator(obj):
                    return self.loadTestsFromGeneratorMethod(obj, parent)
                else:
                    return MethodTestCase(obj)
        elif isfunction(obj):
            isgen = isgenerator(obj)
            if parent and obj.__module__ != parent.__name__:
                obj = transplant_func(obj, parent.__name__)
            if isgen:
                return self.loadTestsFromGenerator(obj, parent)
            else:
                return FunctionTestCase(obj)
        else:
            return Failure(TypeError,
                           "Can't make a test from %s" % obj,
                           address=addr)
Esempio n. 9
0
    def _makeTest(self, obj, parent=None):
        """Given a test object and its parent, return a test case
        or test suite.
        """
        plug_tests = []
        try:
            addr = test_address(obj)
        except KeyboardInterrupt:
            raise
        except:
            addr = None
        for test in self.config.plugins.makeTest(obj, parent):
            plug_tests.append(test)
        # TODO: is this try/except needed?
        try:
            if plug_tests:
                return self.suiteClass(plug_tests)
        except (KeyboardInterrupt, SystemExit):
            raise
        except:
            exc = sys.exc_info()
            return Failure(exc[0], exc[1], exc[2], address=addr)

        if isfunction(obj) and parent and not isinstance(
                parent, types.ModuleType):
            # This is a Python 3.x 'unbound method'.  Wrap it with its
            # associated class..
            obj = unbound_method(parent, obj)

        if isinstance(obj, unittest.TestCase):
            return obj
        elif isclass(obj):
            if parent and obj.__module__ != parent.__name__:
                obj = transplant_class(obj, parent.__name__)
            if issubclass(obj, unittest.TestCase):
                return self.loadTestsFromTestCase(obj)
            else:
                return self.loadTestsFromTestClass(obj)
        elif ismethod(obj):
            if parent is None:
                parent = obj.__class__
            if issubclass(parent, unittest.TestCase):
                return parent(obj.__name__)
            else:
                if isgenerator(obj):
                    return self.loadTestsFromGeneratorMethod(obj, parent)
                else:
                    return MethodTestCase(obj)
        elif isfunction(obj):
            if parent and obj.__module__ != parent.__name__:
                obj = transplant_func(obj, parent.__name__)
            if isgenerator(obj):
                return self.loadTestsFromGenerator(obj, parent)
            else:
                return FunctionTestCase(obj)
        else:
            return Failure(TypeError,
                           "Can't make a test from %s" % obj,
                           address=addr)
Esempio n. 10
0
    def test_method_raw(self):
        class TC(object):
            def test(self):
                pass

        case = nose.case.MethodTestCase(unbound_method(TC, TC.test))
        eq_(nice_test_address(case),
            'nosenicedots/tests/test_units.py:TC.test')
Esempio n. 11
0
    def test_method_gen(self):
        def fn(x):
            pass
        class TC(object):
            def test_gen(self):
                yield fn, (1,)

        case = nose.case.MethodTestCase(unbound_method(TC, TC.test_gen))
        eq_(nice_test_address(case),
            'nosenicedots/tests/test_units.py:TC.test_gen')
    def test_method_test_case_fixtures(self):
        res = unittest.TestResult()
        called = []

        class TestClass(object):
            def setup(self):
                called.append('setup')

            def teardown(self):
                called.append('teardown')

            def test_func(self):
                called.append('test')

        case = nose.case.MethodTestCase(
            unbound_method(TestClass, TestClass.test_func))
        case(res)
        self.assertEqual(called, ['setup', 'test', 'teardown'])

        class TestClassFailingSetup(TestClass):
            def setup(self):
                called.append('setup')
                raise Exception("failed")

        called[:] = []
        case = nose.case.MethodTestCase(
            unbound_method(TestClassFailingSetup,
                           TestClassFailingSetup.test_func))
        case(res)
        self.assertEqual(called, ['setup'])

        class TestClassFailingTest(TestClass):
            def test_func(self):
                called.append('test')
                raise Exception("failed")

        called[:] = []
        case = nose.case.MethodTestCase(
            unbound_method(TestClassFailingTest,
                           TestClassFailingTest.test_func))
        case(res)
        self.assertEqual(called, ['setup', 'test', 'teardown'])
Esempio n. 13
0
    def test_class_and_method_str_attr(self):
        class TestP(object):
            foo = 'a'

            def h(self):
                pass
            h.foo = 'b'

        def i():
            pass
        i.foo = 'a'

        plug = AttributeSelector()
        plug.attribs = [[('foo', 'a'), ('foo', 'b')]]
        assert plug.wantMethod(unbound_method(TestP, TestP.h)) is not False
        assert plug.wantFunction(i) is False

        plug.attribs = [[('foo', 'b')]]
        assert plug.wantMethod(unbound_method(TestP, TestP.h)) is not False
        assert plug.wantFunction(i) is False
Esempio n. 14
0
    def test_method_test_case(self):
        res = unittest.TestResult()

        a = []
        class TestClass(object):
            def test_func(self, a=a):
                a.append(1)

        case = nose.case.MethodTestCase(unbound_method(TestClass,
                                                       TestClass.test_func))
        case(res)
        assert a[0] == 1
    def test_method_test_case(self):
        res = unittest.TestResult()

        a = []
        class TestClass(object):
            def test_func(self, a=a):
                a.append(1)

        case = nose.case.MethodTestCase(unbound_method(TestClass,
                                                       TestClass.test_func))
        case(res)
        assert a[0] == 1
Esempio n. 16
0
    def test_class_attr(self):
        class TestP:
            foo = True
            def h():
                pass

        def i():
            pass

        plug = AttributeSelector()
        plug.attribs = [[('foo', True)]]
        assert plug.wantMethod(unbound_method(TestP, TestP.h)) is not False
        assert plug.wantFunction(i) is False
    def test_class_attr(self):
        class TestP:
            foo = True

            def h():
                pass

        def i():
            pass

        plug = AttributeSelector()
        plug.attribs = [[('foo', True)]]
        assert plug.wantMethod(unbound_method(TestP, TestP.h)) is not False
        assert plug.wantFunction(i) is False
Esempio n. 18
0
    def test_method_test_case_fixtures(self):        
        res = unittest.TestResult()
        called = []
        class TestClass(object):
            def setup(self):
                called.append('setup')
            def teardown(self):
                called.append('teardown')
            def test_func(self):
                called.append('test')

        case = nose.case.MethodTestCase(unbound_method(TestClass,
                                                       TestClass.test_func))
        case(res)
        self.assertEqual(called, ['setup', 'test', 'teardown'])

        class TestClassFailingSetup(TestClass):
            def setup(self):
                called.append('setup')
                raise Exception("failed")
        called[:] = []
        case = nose.case.MethodTestCase(unbound_method(TestClassFailingSetup,
                                            TestClassFailingSetup.test_func))
        case(res)
        self.assertEqual(called, ['setup'])        

        class TestClassFailingTest(TestClass):
            def test_func(self):
                called.append('test')
                raise Exception("failed")
            
        called[:] = []
        case = nose.case.MethodTestCase(unbound_method(TestClassFailingTest,
                                            TestClassFailingTest.test_func))
        case(res)
        self.assertEqual(called, ['setup', 'test', 'teardown'])     
    def test_method_test_case_with_metaclass(self):
        res = unittest.TestResult()
        
        class TestType(type):
            def __new__(cls, name, bases, dct):
                return type.__new__(cls, name, bases, dct)
        a = []
        class TestClass(object, metaclass=TestType):
            def test_func(self, a=a):
                a.append(1)

        case = nose.case.MethodTestCase(unbound_method(TestClass,
                                                       TestClass.test_func))
        case(res)
        assert a[0] == 1
Esempio n. 20
0
    def _expand_tests(self, obj, parent):
        if inspect.isfunction(obj) and parent and not isinstance(parent, types.ModuleType):
            # This is a Python 3.x 'unbound method'.  Wrap it with its
            #  associated class..
            obj = unbound_method(parent, obj)

        tests = []
        if isinstance(obj, list):
            tests = obj
        elif ismethod(obj):
            tests = [obj.__name__]

        _tests = []
        for test in tests:
            _tests += self._make_dataprovided_tests(parent, test)

        return _tests
Esempio n. 21
0
def _expand_tests(obj, parent):
    if inspect.isfunction(obj) and parent and not isinstance(parent, types.ModuleType):
        # This is a Python 3.x 'unbound method'.  Wrap it with its
        #  associated class..
        obj = unbound_method(parent, obj)

    tests = []
    if isinstance(obj, list):
        tests = obj
    elif ismethod(obj):
        tests = [obj.__name__]

    _tests = []
    for test in tests:
        _tests += _make_dataprovided_tests(parent, test)

    return _tests
Esempio n. 22
0
    def test_MethodTestCase_repr_is_consistent_with_mutable_args(self):
        class Foo(object):
            def __init__(self):
                self.bar = 'unmodified'
            def __repr__(self):
                return "Foo(%s)" % self.bar

        class FooTester(object):
            def test_foo(self, foo):
                pass

        foo = Foo()
        case = nose.case.FunctionTestCase(
            unbound_method(FooTester, FooTester.test_foo), arg=(foo,))
        case_repr_before = case.__repr__()
        foo.bar = "snafu'd!"
        case_repr_after = case.__repr__()
        assert case_repr_before == case_repr_after, (
            "Modifying a mutable object arg during test case changed the test "
            "case's __repr__")
    def test_context(self):
        class TC(unittest.TestCase):
            def runTest(self):
                pass
        def test():
            pass

        class Test:
            def test(self):
                pass

        case = nose.case.Test(TC())
        self.assertEqual(case.context, TC)

        case = nose.case.Test(nose.case.FunctionTestCase(test))
        self.assertEqual(case.context, sys.modules[__name__])

        case = nose.case.Test(nose.case.MethodTestCase(unbound_method(Test,
                                                           Test.test)))
        self.assertEqual(case.context, Test)
Esempio n. 24
0
    def test_context(self):
        class TC(unittest.TestCase):
            def runTest(self):
                pass
        def test():
            pass

        class Test:
            def test(self):
                pass

        case = nose.case.Test(TC())
        self.assertEqual(case.context, TC)

        case = nose.case.Test(nose.case.FunctionTestCase(test))
        self.assertEqual(case.context, sys.modules[__name__])

        case = nose.case.Test(nose.case.MethodTestCase(unbound_method(Test,
                                                           Test.test)))
        self.assertEqual(case.context, Test)
Esempio n. 25
0
 def generate(g=generator, c=cls):
     try:
         for test in g():
             test_func, arg = self.parseGeneratedTest(test)
             if not callable(test_func):
                 test_func = unbound_method(c, getattr(c, test_func))
             if ismethod(test_func):
                 yield MethodTestCase(test_func, arg=arg, descriptor=g)
             elif isfunction(test_func):
                 # In this case we're forcing the 'MethodTestCase'
                 # to run the inline function as its test call,
                 # but using the generator method as the 'method of
                 # record' (so no need to pass it as the descriptor)
                 yield MethodTestCase(g, test=test_func, arg=arg)
             else:
                 yield Failure(TypeError, "%s is not a function or method" % test_func)
     except KeyboardInterrupt:
         raise
     except:
         exc = sys.exc_info()
         yield Failure(exc[0], exc[1], exc[2], address=test_address(generator))
Esempio n. 26
0
    def test_MethodTestCase_repr_is_consistent_with_mutable_args(self):
        class Foo(object):
            def __init__(self):
                self.bar = 'unmodified'

            def __repr__(self):
                return "Foo(%s)" % self.bar

        class FooTester(object):
            def test_foo(self, foo):
                pass

        foo = Foo()
        case = nose.case.FunctionTestCase(unbound_method(
            FooTester, FooTester.test_foo),
                                          arg=(foo, ))
        case_repr_before = case.__repr__()
        foo.bar = "snafu'd!"
        case_repr_after = case.__repr__()
        assert case_repr_before == case_repr_after, (
            "Modifying a mutable object arg during test case changed the test "
            "case's __repr__")
Esempio n. 27
0
 def test_is_selected_selects_method_unbound(self):
     plugin = NoseSelectPlugin()
     plugin.add_criterion('DummyTest.test_foo')
     self.assertTrue(plugin._is_selected(unbound_method(DummyTest, DummyTest.test_foo)))