Пример #1
0
 def test_no_capture(self):
     class FooTC(TestCase):
         def test_stderr(self):
             print >> sys.stderr, "foo"
             print "bar"
             self.assert_(False)
     test = FooTC('test_stderr')
     # this runner should not capture stdout / stderr
     runner = SkipAwareTextTestRunner(stream=StringIO(), exitfirst=True)
     result = runner.run(test)
     captured_out, captured_err = test.captured_output()
     self.assertEqual(captured_out.strip(), "")
     self.assertEqual(captured_err.strip(), "")
Пример #2
0
 def test_no_capture(self):
     class FooTC(TestCase):
         def test_stderr(self):
             print >> sys.stderr, "foo"
             print "bar"
             self.assert_(False)
     test = FooTC('test_stderr')
     # this runner should not capture stdout / stderr
     runner = SkipAwareTextTestRunner(stream=StringIO(), exitfirst=True)
     result = runner.run(test)
     captured_out, captured_err = test.captured_output()
     self.assertEqual(captured_out.strip(), "")
     self.assertEqual(captured_err.strip(), "")
Пример #3
0
class ExitFirstTC(TestCase):
    def setUp(self):
        output = StringIO()
        self.runner = SkipAwareTextTestRunner(stream=output, exitfirst=True)

    def test_failure_exit_first(self):
        class FooTC(TestCase):
            def test_1(self):
                pass

            def test_2(self):
                assert False

            def test_3(self):
                pass

        tests = [FooTC('test_1'), FooTC('test_2')]
        result = self.runner.run(TestSuite(tests))
        self.assertEquals(result.testsRun, 2)
        self.assertEquals(len(result.failures), 1)
        self.assertEquals(len(result.errors), 0)

    def test_error_exit_first(self):
        class FooTC(TestCase):
            def test_1(self):
                pass

            def test_2(self):
                raise ValueError()

            def test_3(self):
                pass

        tests = [FooTC('test_1'), FooTC('test_2'), FooTC('test_3')]
        result = self.runner.run(TestSuite(tests))
        self.assertEquals(result.testsRun, 2)
        self.assertEquals(len(result.failures), 0)
        self.assertEquals(len(result.errors), 1)

    def test_generative_exit_first(self):
        class FooTC(TestCase):
            def test_generative(self):
                for i in xrange(10):
                    yield self.assert_, False

        result = self.runner.run(FooTC('test_generative'))
        self.assertEquals(result.testsRun, 1)
        self.assertEquals(len(result.failures), 1)
        self.assertEquals(len(result.errors), 0)
Пример #4
0
class ExitFirstTC(TestCase):
    def setUp(self):
        output = StringIO()
        self.runner = SkipAwareTextTestRunner(stream=output, exitfirst=True)

    def test_failure_exit_first(self):
        class FooTC(TestCase):
            def test_1(self): pass
            def test_2(self): assert False
            def test_3(self): pass
        tests = [FooTC('test_1'), FooTC('test_2')]
        result = self.runner.run(TestSuite(tests))
        self.assertEqual(result.testsRun, 2)
        self.assertEqual(len(result.failures), 1)
        self.assertEqual(len(result.errors), 0)


    def test_error_exit_first(self):
        class FooTC(TestCase):
            def test_1(self): pass
            def test_2(self): raise ValueError()
            def test_3(self): pass
        tests = [FooTC('test_1'), FooTC('test_2'), FooTC('test_3')]
        result = self.runner.run(TestSuite(tests))
        self.assertEqual(result.testsRun, 2)
        self.assertEqual(len(result.failures), 0)
        self.assertEqual(len(result.errors), 1)

    def test_generative_exit_first(self):
        class FooTC(TestCase):
            def test_generative(self):
                for i in xrange(10):
                    yield self.assert_, False
        result = self.runner.run(FooTC('test_generative'))
        self.assertEqual(result.testsRun, 1)
        self.assertEqual(len(result.failures), 1)
        self.assertEqual(len(result.errors), 0)
Пример #5
0
 def setUp(self):
     sys.stdout = sys.stderr = StringIO()
     self.runner = SkipAwareTextTestRunner(stream=StringIO(), exitfirst=True, capture=True)
Пример #6
0
class OutErrCaptureTC(TestCase):

    def setUp(self):
        sys.stdout = sys.stderr = StringIO()
        self.runner = SkipAwareTextTestRunner(stream=StringIO(), exitfirst=True, capture=True)

    def tearDown(self):
        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__

    def test_stdout_capture(self):
        class FooTC(TestCase):
            def test_stdout(self):
                print "foo"
                self.assert_(False)
        test = FooTC('test_stdout')
        result = self.runner.run(test)
        captured_out, captured_err = test.captured_output()
        self.assertEqual(captured_out.strip(), "foo")
        self.assertEqual(captured_err.strip(), "")

    def test_stderr_capture(self):
        class FooTC(TestCase):
            def test_stderr(self):
                print >> sys.stderr, "foo"
                self.assert_(False)
        test = FooTC('test_stderr')
        result = self.runner.run(test)
        captured_out, captured_err = test.captured_output()
        self.assertEqual(captured_out.strip(), "")
        self.assertEqual(captured_err.strip(), "foo")


    def test_both_capture(self):
        class FooTC(TestCase):
            def test_stderr(self):
                print >> sys.stderr, "foo"
                print "bar"
                self.assert_(False)
        test = FooTC('test_stderr')
        result = self.runner.run(test)
        captured_out, captured_err = test.captured_output()
        self.assertEqual(captured_out.strip(), "bar")
        self.assertEqual(captured_err.strip(), "foo")

    def test_no_capture(self):
        class FooTC(TestCase):
            def test_stderr(self):
                print >> sys.stderr, "foo"
                print "bar"
                self.assert_(False)
        test = FooTC('test_stderr')
        # this runner should not capture stdout / stderr
        runner = SkipAwareTextTestRunner(stream=StringIO(), exitfirst=True)
        result = runner.run(test)
        captured_out, captured_err = test.captured_output()
        self.assertEqual(captured_out.strip(), "")
        self.assertEqual(captured_err.strip(), "")


    def test_capture_core(self):
        # output = capture_stdout()
        # bootstrap_print("hello", output=sys.stdout)
        # self.assertEquals(output.restore(), "hello")
        output = capture_stdout()
        bootstrap_print("hello")
        self.assertEquals(output.restore(), "hello")

    def test_unicode_non_ascii_messages(self):
        class FooTC(TestCase):
            def test_xxx(self):
                raise Exception(u'\xe9')
        test = FooTC('test_xxx')
        # run the test and make sure testlib doesn't raise an exception
        result = self.runner.run(test)

    def test_encoded_non_ascii_messages(self):
        class FooTC(TestCase):
            def test_xxx(self):
                raise Exception('\xe9')
        test = FooTC('test_xxx')
        # run the test and make sure testlib doesn't raise an exception
        result = self.runner.run(test)
Пример #7
0
 def setUp(self):
     self.loader = NonStrictTestLoader()
     self.module = TestLoaderTC # mock_object(FooTC=TestLoaderTC.FooTC, BarTC=TestLoaderTC.BarTC)
     self.output = StringIO()
     self.runner = SkipAwareTextTestRunner(stream=self.output)
Пример #8
0
class TestLoaderTC(TestCase):
    ## internal classes for test purposes ########
    class FooTC(TestCase):
        def test_foo1(self): pass
        def test_foo2(self): pass
        def test_bar1(self): pass

    class BarTC(TestCase):
        def test_bar2(self): pass
    ##############################################

    def setUp(self):
        self.loader = NonStrictTestLoader()
        self.module = TestLoaderTC # mock_object(FooTC=TestLoaderTC.FooTC, BarTC=TestLoaderTC.BarTC)
        self.output = StringIO()
        self.runner = SkipAwareTextTestRunner(stream=self.output)

    def assertRunCount(self, pattern, module, expected_count, skipped=()):
        if pattern:
            suite = self.loader.loadTestsFromNames([pattern], module)
        else:
            suite = self.loader.loadTestsFromModule(module)
        self.runner.test_pattern = pattern
        self.runner.skipped_patterns = skipped
        result = self.runner.run(suite)
        self.runner.test_pattern = None
        self.runner.skipped_patterns = ()
        self.assertEquals(result.testsRun, expected_count)

    def test_collect_everything(self):
        """make sure we don't change the default behaviour
        for loadTestsFromModule() and loadTestsFromTestCase
        """
        testsuite = self.loader.loadTestsFromModule(self.module)
        self.assertEquals(len(testsuite._tests), 2)
        suite1, suite2 = testsuite._tests
        self.assertEquals(len(suite1._tests) + len(suite2._tests), 4)

    def test_collect_with_classname(self):
        self.assertRunCount('FooTC', self.module, 3)
        self.assertRunCount('BarTC', self.module, 1)

    def test_collect_with_classname_and_pattern(self):
        data = [('FooTC.test_foo1', 1), ('FooTC.test_foo', 2), ('FooTC.test_fo', 2),
                ('FooTC.foo1', 1), ('FooTC.foo', 2), ('FooTC.whatever', 0)
                ]
        for pattern, expected_count in data:
            yield self.assertRunCount, pattern, self.module, expected_count

    def test_collect_with_pattern(self):
        data = [('test_foo1', 1), ('test_foo', 2), ('test_bar', 2),
                ('foo1', 1), ('foo', 2), ('bar', 2), ('ba', 2),
                ('test', 4), ('ab', 0),
                ]
        for pattern, expected_count in data:
            yield self.assertRunCount, pattern, self.module, expected_count

    def test_tescase_with_custom_metaclass(self):
        class mymetaclass(type): pass
        class MyMod:
            class MyTestCase(TestCase):
                __metaclass__ = mymetaclass
                def test_foo1(self): pass
                def test_foo2(self): pass
                def test_bar(self): pass
        data = [('test_foo1', 1), ('test_foo', 2), ('test_bar', 1),
                ('foo1', 1), ('foo', 2), ('bar', 1), ('ba', 1),
                ('test', 3), ('ab', 0),
                ('MyTestCase.test_foo1', 1), ('MyTestCase.test_foo', 2),
                ('MyTestCase.test_fo', 2), ('MyTestCase.foo1', 1),
                ('MyTestCase.foo', 2), ('MyTestCase.whatever', 0)
                ]
        for pattern, expected_count in data:
            yield self.assertRunCount, pattern, MyMod, expected_count


    def test_collect_everything_and_skipped_patterns(self):
        testdata = [ (['foo1'], 3), (['foo'], 2),
                     (['foo', 'bar'], 0),
                     ]
        for skipped, expected_count in testdata:
            yield self.assertRunCount, None, self.module, expected_count, skipped


    def test_collect_specific_pattern_and_skip_some(self):
        testdata = [ ('bar', ['foo1'], 2), ('bar', [], 2),
                     ('bar', ['bar'], 0), ]

        for runpattern, skipped, expected_count in testdata:
            yield self.assertRunCount, runpattern, self.module, expected_count, skipped

    def test_skip_classname(self):
        testdata = [ (['BarTC'], 3), (['FooTC'], 1), ]
        for skipped, expected_count in testdata:
            yield self.assertRunCount, None, self.module, expected_count, skipped

    def test_skip_classname_and_specific_collect(self):
        testdata = [ ('bar', ['BarTC'], 1), ('foo', ['FooTC'], 0), ]
        for runpattern, skipped, expected_count in testdata:
            yield self.assertRunCount, runpattern, self.module, expected_count, skipped


    def test_nonregr_dotted_path(self):
        self.assertRunCount('FooTC.test_foo', self.module, 2)


    def test_inner_tests_selection(self):
        class MyMod:
            class MyTestCase(TestCase):
                def test_foo(self): pass
                def test_foobar(self):
                    for i in xrange(5):
                        if i%2 == 0:
                            yield InnerTest('even', lambda: None)
                        else:
                            yield InnerTest('odd', lambda: None)
                    yield lambda: None

        data = [('foo', 7), ('test_foobar', 6), ('even', 3), ('odd', 2),
                ]
        for pattern, expected_count in data:
            yield self.assertRunCount, pattern, MyMod, expected_count

    def test_nonregr_class_skipped_option(self):
        class MyMod:
            class MyTestCase(TestCase):
                def test_foo(self): pass
                def test_bar(self): pass
            class FooTC(TestCase):
                def test_foo(self): pass
        self.assertRunCount('foo', MyMod, 2)
        self.assertRunCount(None, MyMod, 3)
        self.loader.skipped_patterns = self.runner.skipped_patterns = ['FooTC']
        self.assertRunCount('foo', MyMod, 1)
        self.assertRunCount(None, MyMod, 2)

    def test__classes_are_ignored(self):
        class MyMod:
            class _Base(TestCase):
                def test_1(self): pass
            class MyTestCase(_Base):
                def test_2(self): pass
        self.assertRunCount(None, MyMod, 2)
Пример #9
0
 def setUp(self):
     output = StringIO()
     self.runner = SkipAwareTextTestRunner(stream=output, exitfirst=True)
Пример #10
0
class GenerativeTestsTC(TestCase):

    def setUp(self):
        output = StringIO()
        self.runner = SkipAwareTextTestRunner(stream=output)

    def test_generative_ok(self):
        class FooTC(TestCase):
            def test_generative(self):
                for i in xrange(10):
                    yield self.assertEquals, i, i
        result = self.runner.run(FooTC('test_generative'))
        self.assertEquals(result.testsRun, 10)
        self.assertEquals(len(result.failures), 0)
        self.assertEquals(len(result.errors), 0)


    def test_generative_half_bad(self):
        class FooTC(TestCase):
            def test_generative(self):
                for i in xrange(10):
                    yield self.assertEquals, i%2, 0
        result = self.runner.run(FooTC('test_generative'))
        self.assertEquals(result.testsRun, 10)
        self.assertEquals(len(result.failures), 5)
        self.assertEquals(len(result.errors), 0)


    def test_generative_error(self):
        class FooTC(TestCase):
            def test_generative(self):
                for i in xrange(10):
                    if i == 5:
                        raise ValueError('STOP !')
                    yield self.assertEquals, i, i

        result = self.runner.run(FooTC('test_generative'))
        self.assertEquals(result.testsRun, 5)
        self.assertEquals(len(result.failures), 0)
        self.assertEquals(len(result.errors), 1)

    def test_generative_error2(self):
        class FooTC(TestCase):
            def test_generative(self):
                for i in xrange(10):
                    if i == 5:
                        yield self.ouch
                    yield self.assertEquals, i, i
            def ouch(self): raise ValueError('stop !')
        result = self.runner.run(FooTC('test_generative'))
        self.assertEquals(result.testsRun, 6)
        self.assertEquals(len(result.failures), 0)
        self.assertEquals(len(result.errors), 1)


    def test_generative_setup(self):
        class FooTC(TestCase):
            def setUp(self):
                raise ValueError('STOP !')
            def test_generative(self):
                for i in xrange(10):
                    yield self.assertEquals, i, i

        result = self.runner.run(FooTC('test_generative'))
        self.assertEquals(result.testsRun, 1)
        self.assertEquals(len(result.failures), 0)
        self.assertEquals(len(result.errors), 1)

    def test_generative_inner_skip(self):
        class FooTC(TestCase):
            def check(self, val):
                if val == 5:
                    self.innerSkip("no 5")
                else:
                    self.assertEquals(val, val)

            def test_generative(self):
                for i in xrange(10):
                    yield InnerTest("check_%s"%i, self.check, i)

        result = self.runner.run(FooTC('test_generative'))
        self.assertEquals(result.testsRun, 10)
        self.assertEquals(len(result.failures), 0)
        self.assertEquals(len(result.errors), 0)
        self.assertEquals(len(result.skipped), 1)

    def test_generative_skip(self):
        class FooTC(TestCase):
            def check(self, val):
                if val == 5:
                    self.skip("no 5")
                else:
                    self.assertEquals(val, val)

            def test_generative(self):
                for i in xrange(10):
                    yield InnerTest("check_%s"%i, self.check, i)

        result = self.runner.run(FooTC('test_generative'))
        self.assertEquals(result.testsRun, 6)
        self.assertEquals(len(result.failures), 0)
        self.assertEquals(len(result.errors), 0)
        self.assertEquals(len(result.skipped), 1)

    def test_generative_inner_error(self):
        class FooTC(TestCase):
            def check(self, val):
                if val == 5:
                    raise ValueError("no 5")
                else:
                    self.assertEquals(val, val)

            def test_generative(self):
                for i in xrange(10):
                    yield InnerTest("check_%s"%i, self.check, i)

        result = self.runner.run(FooTC('test_generative'))
        self.assertEquals(result.testsRun, 6)
        self.assertEquals(len(result.failures), 0)
        self.assertEquals(len(result.errors), 1)
        self.assertEquals(len(result.skipped), 0)

    def test_generative_inner_failure(self):
        class FooTC(TestCase):
            def check(self, val):
                if val == 5:
                    self.assertEquals(val, val+1)
                else:
                    self.assertEquals(val, val)

            def test_generative(self):
                for i in xrange(10):
                    yield InnerTest("check_%s"%i, self.check, i)

        result = self.runner.run(FooTC('test_generative'))
        self.assertEquals(result.testsRun, 10)
        self.assertEquals(len(result.failures), 1)
        self.assertEquals(len(result.errors), 0)
        self.assertEquals(len(result.skipped), 0)
Пример #11
0
    def test_tagged_class(self):
        def options(tags):
            class Options(object):
                tags_pattern = tags
            return Options()

        cls = self.cls

        runner = SkipAwareTextTestRunner()
        self.assertTrue(runner.does_match_tags(cls.test_one))
        self.assertTrue(runner.does_match_tags(cls.test_two))
        self.assertTrue(runner.does_match_tags(cls.test_three))

        runner = SkipAwareTextTestRunner(options=options('one'))
        self.assertTrue(runner.does_match_tags(cls.test_one))
        self.assertTrue(runner.does_match_tags(cls.test_two))
        self.assertFalse(runner.does_match_tags(cls.test_three))

        runner = SkipAwareTextTestRunner(options=options('two'))
        self.assertTrue(runner.does_match_tags(cls.test_one))
        self.assertTrue(runner.does_match_tags(cls.test_two))
        self.assertFalse(runner.does_match_tags(cls.test_three))

        runner = SkipAwareTextTestRunner(options=options('three'))
        self.assertFalse(runner.does_match_tags(cls.test_one))
        self.assertTrue(runner.does_match_tags(cls.test_two))
        self.assertTrue(runner.does_match_tags(cls.test_three))

        runner = SkipAwareTextTestRunner(options=options('two or three'))
        self.assertTrue(runner.does_match_tags(cls.test_one))
        self.assertTrue(runner.does_match_tags(cls.test_two))
        self.assertTrue(runner.does_match_tags(cls.test_three))

        runner = SkipAwareTextTestRunner(options=options('two and three'))
        self.assertFalse(runner.does_match_tags(cls.test_one))
        self.assertTrue(runner.does_match_tags(cls.test_two))
        self.assertFalse(runner.does_match_tags(cls.test_three))
Пример #12
0
 def setUp(self):
     sys.stdout = sys.stderr = StringIO()
     self.runner = SkipAwareTextTestRunner(stream=StringIO(), exitfirst=True, capture=True)
Пример #13
0
class OutErrCaptureTC(TestCase):

    def setUp(self):
        sys.stdout = sys.stderr = StringIO()
        self.runner = SkipAwareTextTestRunner(stream=StringIO(), exitfirst=True, capture=True)

    def tearDown(self):
        sys.stdout = sys.__stdout__
        sys.stderr = sys.__stderr__

    @unittest.skipIf(not sys.stdout.isatty(), "need stdout")
    def test_stdout_capture(self):
        class FooTC(TestCase):
            def test_stdout(self):
                print "foo"
                self.assert_(False)
        test = FooTC('test_stdout')
        result = self.runner.run(test)
        captured_out, captured_err = test.captured_output()
        self.assertEqual(captured_out.strip(), "foo")
        self.assertEqual(captured_err.strip(), "")

    @unittest.skipIf(not sys.stderr.isatty(), "need stderr")
    def test_stderr_capture(self):
        class FooTC(TestCase):
            def test_stderr(self):
                print >> sys.stderr, "foo"
                self.assert_(False)
        test = FooTC('test_stderr')
        result = self.runner.run(test)
        captured_out, captured_err = test.captured_output()
        self.assertEqual(captured_out.strip(), "")
        self.assertEqual(captured_err.strip(), "foo")

    @unittest.skipIf(not sys.stderr.isatty(), "need stderr")
    @unittest.skipIf(not sys.stdout.isatty(), "need stdout")
    def test_both_capture(self):
        class FooTC(TestCase):
            def test_stderr(self):
                print >> sys.stderr, "foo"
                print "bar"
                self.assert_(False)
        test = FooTC('test_stderr')
        result = self.runner.run(test)
        captured_out, captured_err = test.captured_output()
        self.assertEqual(captured_out.strip(), "bar")
        self.assertEqual(captured_err.strip(), "foo")

    @unittest.skipIf(not sys.stderr.isatty(), "need stderr")
    @unittest.skipIf(not sys.stdout.isatty(), "need stdout")
    def test_no_capture(self):
        class FooTC(TestCase):
            def test_stderr(self):
                print >> sys.stderr, "foo"
                print "bar"
                self.assert_(False)
        test = FooTC('test_stderr')
        # this runner should not capture stdout / stderr
        runner = SkipAwareTextTestRunner(stream=StringIO(), exitfirst=True)
        result = runner.run(test)
        captured_out, captured_err = test.captured_output()
        self.assertEqual(captured_out.strip(), "")
        self.assertEqual(captured_err.strip(), "")

    @unittest.skipIf(not sys.stdout.isatty(), "need stdout")
    def test_capture_core(self):
        # output = capture_stdout()
        # bootstrap_print("hello", output=sys.stdout)
        # self.assertEqual(output.restore(), "hello")
        output = capture_stdout()
        bootstrap_print("hello")
        self.assertEqual(output.restore(), "hello")

    def test_unicode_non_ascii_messages(self):
        class FooTC(TestCase):
            def test_xxx(self):
                raise Exception(u'\xe9')
        test = FooTC('test_xxx')
        # run the test and make sure testlib doesn't raise an exception
        result = self.runner.run(test)

    def test_encoded_non_ascii_messages(self):
        class FooTC(TestCase):
            def test_xxx(self):
                raise Exception('\xe9')
        test = FooTC('test_xxx')
        # run the test and make sure testlib doesn't raise an exception
        result = self.runner.run(test)
Пример #14
0
 def setUp(self):
     self.loader = NonStrictTestLoader()
     self.module = TestLoaderTC # mock_object(FooTC=TestLoaderTC.FooTC, BarTC=TestLoaderTC.BarTC)
     self.output = StringIO()
     self.runner = SkipAwareTextTestRunner(stream=self.output)
Пример #15
0
class TestLoaderTC(TestCase):
    ## internal classes for test purposes ########
    class FooTC(TestCase):
        def test_foo1(self): pass
        def test_foo2(self): pass
        def test_bar1(self): pass

    class BarTC(TestCase):
        def test_bar2(self): pass
    ##############################################

    def setUp(self):
        self.loader = NonStrictTestLoader()
        self.module = TestLoaderTC # mock_object(FooTC=TestLoaderTC.FooTC, BarTC=TestLoaderTC.BarTC)
        self.output = StringIO()
        self.runner = SkipAwareTextTestRunner(stream=self.output)

    def assertRunCount(self, pattern, module, expected_count, skipped=()):
        if pattern:
            suite = self.loader.loadTestsFromNames([pattern], module)
        else:
            suite = self.loader.loadTestsFromModule(module)
        self.runner.test_pattern = pattern
        self.runner.skipped_patterns = skipped
        result = self.runner.run(suite)
        self.runner.test_pattern = None
        self.runner.skipped_patterns = ()
        self.assertEqual(result.testsRun, expected_count)

    def test_collect_everything(self):
        """make sure we don't change the default behaviour
        for loadTestsFromModule() and loadTestsFromTestCase
        """
        testsuite = self.loader.loadTestsFromModule(self.module)
        self.assertEqual(len(testsuite._tests), 2)
        suite1, suite2 = testsuite._tests
        self.assertEqual(len(suite1._tests) + len(suite2._tests), 4)

    def test_collect_with_classname(self):
        self.assertRunCount('FooTC', self.module, 3)
        self.assertRunCount('BarTC', self.module, 1)

    def test_collect_with_classname_and_pattern(self):
        data = [('FooTC.test_foo1', 1), ('FooTC.test_foo', 2), ('FooTC.test_fo', 2),
                ('FooTC.foo1', 1), ('FooTC.foo', 2), ('FooTC.whatever', 0)
                ]
        for pattern, expected_count in data:
            yield self.assertRunCount, pattern, self.module, expected_count

    def test_collect_with_pattern(self):
        data = [('test_foo1', 1), ('test_foo', 2), ('test_bar', 2),
                ('foo1', 1), ('foo', 2), ('bar', 2), ('ba', 2),
                ('test', 4), ('ab', 0),
                ]
        for pattern, expected_count in data:
            yield self.assertRunCount, pattern, self.module, expected_count

    def test_tescase_with_custom_metaclass(self):
        class mymetaclass(type): pass
        class MyMod:
            class MyTestCase(TestCase):
                __metaclass__ = mymetaclass
                def test_foo1(self): pass
                def test_foo2(self): pass
                def test_bar(self): pass
        data = [('test_foo1', 1), ('test_foo', 2), ('test_bar', 1),
                ('foo1', 1), ('foo', 2), ('bar', 1), ('ba', 1),
                ('test', 3), ('ab', 0),
                ('MyTestCase.test_foo1', 1), ('MyTestCase.test_foo', 2),
                ('MyTestCase.test_fo', 2), ('MyTestCase.foo1', 1),
                ('MyTestCase.foo', 2), ('MyTestCase.whatever', 0)
                ]
        for pattern, expected_count in data:
            yield self.assertRunCount, pattern, MyMod, expected_count


    def test_collect_everything_and_skipped_patterns(self):
        testdata = [ (['foo1'], 3), (['foo'], 2),
                     (['foo', 'bar'], 0),
                     ]
        for skipped, expected_count in testdata:
            yield self.assertRunCount, None, self.module, expected_count, skipped


    def test_collect_specific_pattern_and_skip_some(self):
        testdata = [ ('bar', ['foo1'], 2), ('bar', [], 2),
                     ('bar', ['bar'], 0), ]

        for runpattern, skipped, expected_count in testdata:
            yield self.assertRunCount, runpattern, self.module, expected_count, skipped

    def test_skip_classname(self):
        testdata = [ (['BarTC'], 3), (['FooTC'], 1), ]
        for skipped, expected_count in testdata:
            yield self.assertRunCount, None, self.module, expected_count, skipped

    def test_skip_classname_and_specific_collect(self):
        testdata = [ ('bar', ['BarTC'], 1), ('foo', ['FooTC'], 0), ]
        for runpattern, skipped, expected_count in testdata:
            yield self.assertRunCount, runpattern, self.module, expected_count, skipped


    def test_nonregr_dotted_path(self):
        self.assertRunCount('FooTC.test_foo', self.module, 2)


    def test_inner_tests_selection(self):
        class MyMod:
            class MyTestCase(TestCase):
                def test_foo(self): pass
                def test_foobar(self):
                    for i in xrange(5):
                        if i%2 == 0:
                            yield InnerTest('even', lambda: None)
                        else:
                            yield InnerTest('odd', lambda: None)
                    yield lambda: None

        data = [('foo', 7), ('test_foobar', 6), ('even', 3), ('odd', 2),
                ]
        for pattern, expected_count in data:
            yield self.assertRunCount, pattern, MyMod, expected_count

    def test_nonregr_class_skipped_option(self):
        class MyMod:
            class MyTestCase(TestCase):
                def test_foo(self): pass
                def test_bar(self): pass
            class FooTC(TestCase):
                def test_foo(self): pass
        self.assertRunCount('foo', MyMod, 2)
        self.assertRunCount(None, MyMod, 3)
        self.loader.skipped_patterns = self.runner.skipped_patterns = ['FooTC']
        self.assertRunCount('foo', MyMod, 1)
        self.assertRunCount(None, MyMod, 2)

    def test__classes_are_ignored(self):
        class MyMod:
            class _Base(TestCase):
                def test_1(self): pass
            class MyTestCase(_Base):
                def test_2(self): pass
        self.assertRunCount(None, MyMod, 2)
Пример #16
0
 def setUp(self):
     output = StringIO()
     self.runner = SkipAwareTextTestRunner(stream=output, exitfirst=True)
Пример #17
0
    def test_tagged_class(self):
        def options(tags):
            class Options(object):
                tags_pattern = tags
            return Options()

        cls = self.cls

        runner = SkipAwareTextTestRunner()
        self.assertTrue(runner.does_match_tags(cls.test_one))
        self.assertTrue(runner.does_match_tags(cls.test_two))
        self.assertTrue(runner.does_match_tags(cls.test_three))

        runner = SkipAwareTextTestRunner(options=options('one'))
        self.assertTrue(runner.does_match_tags(cls.test_one))
        self.assertFalse(runner.does_match_tags(cls.test_two))
        self.assertFalse(runner.does_match_tags(cls.test_three))

        runner = SkipAwareTextTestRunner(options=options('two'))
        self.assertTrue(runner.does_match_tags(cls.test_one))
        self.assertTrue(runner.does_match_tags(cls.test_two))
        self.assertFalse(runner.does_match_tags(cls.test_three))

        runner = SkipAwareTextTestRunner(options=options('three'))
        self.assertFalse(runner.does_match_tags(cls.test_one))
        self.assertTrue(runner.does_match_tags(cls.test_two))
        self.assertTrue(runner.does_match_tags(cls.test_three))

        runner = SkipAwareTextTestRunner(options=options('two or three'))
        self.assertTrue(runner.does_match_tags(cls.test_one))
        self.assertTrue(runner.does_match_tags(cls.test_two))
        self.assertTrue(runner.does_match_tags(cls.test_three))

        runner = SkipAwareTextTestRunner(options=options('two and three'))
        self.assertFalse(runner.does_match_tags(cls.test_one))
        self.assertTrue(runner.does_match_tags(cls.test_two))
        self.assertFalse(runner.does_match_tags(cls.test_three))
Пример #18
0
class GenerativeTestsTC(TestCase):

    def setUp(self):
        output = StringIO()
        self.runner = SkipAwareTextTestRunner(stream=output)

    def test_generative_ok(self):
        class FooTC(TestCase):
            def test_generative(self):
                for i in xrange(10):
                    yield self.assertEqual, i, i
        result = self.runner.run(FooTC('test_generative'))
        self.assertEqual(result.testsRun, 10)
        self.assertEqual(len(result.failures), 0)
        self.assertEqual(len(result.errors), 0)


    def test_generative_half_bad(self):
        class FooTC(TestCase):
            def test_generative(self):
                for i in xrange(10):
                    yield self.assertEqual, i%2, 0
        result = self.runner.run(FooTC('test_generative'))
        self.assertEqual(result.testsRun, 10)
        self.assertEqual(len(result.failures), 5)
        self.assertEqual(len(result.errors), 0)


    def test_generative_error(self):
        class FooTC(TestCase):
            def test_generative(self):
                for i in xrange(10):
                    if i == 5:
                        raise ValueError('STOP !')
                    yield self.assertEqual, i, i

        result = self.runner.run(FooTC('test_generative'))
        self.assertEqual(result.testsRun, 5)
        self.assertEqual(len(result.failures), 0)
        self.assertEqual(len(result.errors), 1)

    def test_generative_error2(self):
        class FooTC(TestCase):
            def test_generative(self):
                for i in xrange(10):
                    if i == 5:
                        yield self.ouch
                    yield self.assertEqual, i, i
            def ouch(self): raise ValueError('stop !')
        result = self.runner.run(FooTC('test_generative'))
        self.assertEqual(result.testsRun, 6)
        self.assertEqual(len(result.failures), 0)
        self.assertEqual(len(result.errors), 1)


    def test_generative_setup(self):
        class FooTC(TestCase):
            def setUp(self):
                raise ValueError('STOP !')
            def test_generative(self):
                for i in xrange(10):
                    yield self.assertEqual, i, i

        result = self.runner.run(FooTC('test_generative'))
        self.assertEqual(result.testsRun, 1)
        self.assertEqual(len(result.failures), 0)
        self.assertEqual(len(result.errors), 1)

    def test_generative_inner_skip(self):
        class FooTC(TestCase):
            def check(self, val):
                if val == 5:
                    self.innerSkip("no 5")
                else:
                    self.assertEqual(val, val)

            def test_generative(self):
                for i in xrange(10):
                    yield InnerTest("check_%s"%i, self.check, i)

        result = self.runner.run(FooTC('test_generative'))
        self.assertEqual(result.testsRun, 10)
        self.assertEqual(len(result.failures), 0)
        self.assertEqual(len(result.errors), 0)
        self.assertEqual(len(result.skipped), 1)

    def test_generative_skip(self):
        class FooTC(TestCase):
            def check(self, val):
                if val == 5:
                    self.skipTest("no 5")
                else:
                    self.assertEqual(val, val)

            def test_generative(self):
                for i in xrange(10):
                    yield InnerTest("check_%s"%i, self.check, i)

        result = self.runner.run(FooTC('test_generative'))
        self.assertEqual(result.testsRun, 6)
        self.assertEqual(len(result.failures), 0)
        self.assertEqual(len(result.errors), 0)
        self.assertEqual(len(result.skipped), 1)

    def test_generative_inner_error(self):
        class FooTC(TestCase):
            def check(self, val):
                if val == 5:
                    raise ValueError("no 5")
                else:
                    self.assertEqual(val, val)

            def test_generative(self):
                for i in xrange(10):
                    yield InnerTest("check_%s"%i, self.check, i)

        result = self.runner.run(FooTC('test_generative'))
        self.assertEqual(result.testsRun, 6)
        self.assertEqual(len(result.failures), 0)
        self.assertEqual(len(result.errors), 1)
        self.assertEqual(len(result.skipped), 0)

    def test_generative_inner_failure(self):
        class FooTC(TestCase):
            def check(self, val):
                if val == 5:
                    self.assertEqual(val, val+1)
                else:
                    self.assertEqual(val, val)

            def test_generative(self):
                for i in xrange(10):
                    yield InnerTest("check_%s"%i, self.check, i)

        result = self.runner.run(FooTC('test_generative'))
        self.assertEqual(result.testsRun, 10)
        self.assertEqual(len(result.failures), 1)
        self.assertEqual(len(result.errors), 0)
        self.assertEqual(len(result.skipped), 0)