Exemplo n.º 1
0
    def test_buffering(self):
        m = TestModule(
            "from __future__ import print_function",
            "from unittest import TestCase",
            "import logging",
            "import sys",
            "",
            "logging.basicConfig()",
            "logger = logging.getLogger(__name__)",
            "logger.setLevel(logging.DEBUG)",
            "log_handler = logging.StreamHandler(stream=sys.stderr)",
            "logger.addHandler(log_handler)",
            "",
            "class BaseTResultTestCase(TestCase):",
            "   def test_success(self):",
            "       logger.info('foo')",
            "",
            "   def test_failure(self):",
            "       logger.info('foo')",
            "       print('bar')",
            "       self.assertTrue(False)",
            "",
        )

        search_str = "{}.BaseTResultTestCase.failure".format(m.name)
        t = tester.run_test(search_str, m.cwd, buffer=True)

        search_str = "{}.BaseTResultTestCase.success".format(m.name)
        t = tester.run_test(search_str, m.cwd, buffer=True)

        search_str = "{}.BaseTResultTestCase.success".format(m.name)
        t = tester.run_test(search_str, m.cwd)
Exemplo n.º 2
0
    def test_multi(self):
        # if there is an error in one of the tests but another test is found, don't
        # bubble up the error
        cwd = testdata.create_dir()
        testdata.create_modules(
            {
                "multi.bar_test": "\n".join(
                    ["from unittest import TestCase", "", "class BarTest(TestCase):", "    def test_bar(self): pass"]
                ),
                "multi.foo_test": "\n".join(
                    [
                        "from unittest import TestCase",
                        "",
                        "class FooTest(TestCase):",
                        "    def test_foo(self): pass",
                        "",
                        "class CheTest(TestCase):",
                        "    def test_che(self): pass",
                    ]
                ),
            },
            tmpdir=cwd,
        )

        ret_code = tester.run_test("multi.", cwd)
        self.assertEqual(0, ret_code)
Exemplo n.º 3
0
    def test_prefix_search2(self):
        m = TestModule(
            "from unittest import TestCase",
            "",
            "class FooBarTest(TestCase):",
            "    def test_blah(self):",
            "        pass",
            name="ps2.foobar.chebaz_test",
        )

        s = Client(m.cwd)
        ret_code = tester.run_test("foobar.", m.cwd)
        self.assertEqual(0, ret_code)

        ret_code = tester.run_test("ps2.", m.cwd)
        self.assertEqual(0, ret_code)
Exemplo n.º 4
0
    def test_names(self):
        m = TestModule(
            "from unittest import TestCase",
            "",
            "class BaseTCase(TestCase):",
            "   def test_foo(self):",
            "       pass",
            "",
            "class BarTest(BaseTCase):",
            "   pass",
        )

        ret_code = tester.run_test("pmod", m.cwd)
        self.assertEqual(0, ret_code)

        ret_code = tester.run_test("", m.cwd)
        self.assertEqual(0, ret_code)
Exemplo n.º 5
0
    def test_relative_import(self):
        cwd = testdata.create_dir()
        testdata.create_modules(
            {
                "ritests": "from unittest import TestCase",
                "ritests.foo_test": "\n".join(
                    ["from . import TestCase", "", "class FooTest(TestCase):", "    def test_bar(self): pass"]
                ),
            },
            tmpdir=cwd,
        )

        ret_code = tester.run_test("Foo.bar", cwd)
Exemplo n.º 6
0
    def test_cli_errors(self):
        # if there is an error in one of the tests but another test is found, don't
        # bubble up the error
        cwd = testdata.create_dir()
        testdata.create_modules(
            {
                "cli_2": "from unittest import TestCase",
                "cli_2.clibar_test": "\n".join(["from . import TestCase", "", "raise ValueError('foo')"]),
                "cli_2.clifoo_test": "\n".join(
                    ["from . import TestCase", "", "class CliFooTest(TestCase):", "    def test_bar(self): pass"]
                ),
            },
            tmpdir=cwd,
        )

        ret_code = tester.run_test("cli_2.", cwd)
        self.assertEqual(0, ret_code)

        # if there is an error and no other test is found, bubble up the error
        m = TestModule("from unittest import TestCase", "", "raise ValueError('foo')")

        with self.assertRaises(ValueError):
            ret_code = tester.run_test(m.name_prefix, m.cwd)
Exemplo n.º 7
0
def console():
    '''
    cli hook

    return -- integer -- the exit code
    '''
    parser = argparse.ArgumentParser(description='Easy Python Testing')
    parser.add_argument('names', metavar='NAME', nargs='*', default=[], help='the test(s) you want to run')
    parser.add_argument('--basedir', dest='basedir', default=os.curdir, help='run from this directory')
    parser.add_argument('--debug', "-d", dest='debug', action='store_true', help='print debugging info')
    parser.add_argument("--version", "-V", action='version', version="%(prog)s {}".format(__version__))
    parser.add_argument('--all', "-a", dest='run_all', action='store_true', help='run all tests with buffer')
    #parser.add_argument('--fad', dest='daf', action='store_true', help='run with --all --no-faifast --debug')

    # https://docs.python.org/2/library/unittest.html#command-line-options
    #parser.add_argument('--no-failfast', dest='no_failfast', action='store_true', help='turns off fail fast')
    #parser.add_argument('--no-buffer', dest='no_buffer', action='store_true', help='turns off buffer when more than one test is ran')
    parser.add_argument('--buffer', "-b", dest='buffer', action='store_true', help='Buffer stdout and stderr during test runs')

    args, test_args = parser.parse_known_args()

    test_args.insert(0, sys.argv[0])
    ret_code = 0

    if args.run_all:
        args.names = ['']
        args.buffer = True

    # create the singleton
    environ = tester.TestEnviron.get_instance(args)

    if not args.names:
        args.names.append('')

    if args.names:
        for name in args.names:
            ret_code |= tester.run_test(
                name,
                args.basedir,
                argv=test_args,
            )

    else:
        environ.unbuffer()
        # http://unix.stackexchange.com/a/8815/118750
        parser.print_help()
        ret_code = 1

    sys.exit(ret_code)
Exemplo n.º 8
0
    def test_no_tests_found(self):
        # if there is an error in one of the tests but another test is found, don't
        # bubble up the error
        cwd = testdata.create_dir()
        testdata.create_modules(
            {
                "nofound.nofo_test": "\n".join(
                    ["from unittest import TestCase", "", "class NofoTest(TestCase):", "    def test_nofo(self): pass"]
                )
            },
            tmpdir=cwd,
        )

        ret_code = tester.run_test("nofound_does_not_exist.", cwd)
        self.assertEqual(1, ret_code)
Exemplo n.º 9
0
    def test_names(self):
        return
        m = TestModule(
            "from unittest import TestCase",
            "",
            "class BaseTCase(TestCase):",
            "   def test_foo(self):",
            "       pass",
            "",
            "class BarTest(BaseTCase):",
            "   pass",
        )

        # this should call load from name(s)
        # tl = tester.TestLoader('pmod', m.cwd)
        ret_code = tester.run_test("pmod", m.cwd)
Exemplo n.º 10
0
    def test_setup(self):
        m = TestModule(
            "from __future__ import print_function",
            "from unittest import TestCase",
            "",
            "def setUpModule():",
            "    print('here')",
            "",
            "class BaseTCase(TestCase):",
            "    def test_foo(self):",
            "        pass",
            "",
            "class BarTest(BaseTCase):",
            "    pass",
        )

        ret_code = tester.run_test("pmod", m.cwd)
        self.assertEqual(0, ret_code)
Exemplo n.º 11
0
    def test_parse_error(self):
        cwd = testdata.create_dir()
        testdata.create_modules(
            {
                "tests_parse_error": "from unittest import TestCase",
                "tests_parse_error.pefoo_test": "\n".join(
                    [
                        "from . import TestCase",
                        "",
                        "class PEFooTest(TestCase):",
                        "    def test_bar(self):",
                        '        foo = "this is a parse error',
                    ]
                ),
            },
            tmpdir=cwd,
        )

        with self.assertRaises(SyntaxError):
            ret_code = tester.run_test("PEFoo.bar", cwd)
Exemplo n.º 12
0
def console():
    '''
    cli hook

    return -- integer -- the exit code
    '''
    parser = argparse.ArgumentParser(description='Easy Python Testing')
    parser.add_argument('names',
                        metavar='NAME',
                        nargs='*',
                        default=[],
                        help='the test(s) you want to run')
    parser.add_argument('--basedir',
                        dest='basedir',
                        default=os.curdir,
                        help='run from this directory')
    parser.add_argument('--debug',
                        "-d",
                        dest='debug',
                        action='store_true',
                        help='print debugging info')
    parser.add_argument("--version",
                        "-V",
                        action='version',
                        version="%(prog)s {}".format(__version__))
    parser.add_argument('--all',
                        "-a",
                        dest='run_all',
                        action='store_true',
                        help='run all tests with buffer')
    #parser.add_argument('--fad', dest='daf', action='store_true', help='run with --all --no-faifast --debug')

    # https://docs.python.org/2/library/unittest.html#command-line-options
    #parser.add_argument('--no-failfast', dest='no_failfast', action='store_true', help='turns off fail fast')
    #parser.add_argument('--no-buffer', dest='no_buffer', action='store_true', help='turns off buffer when more than one test is ran')
    parser.add_argument('--buffer',
                        "-b",
                        dest='buffer',
                        action='store_true',
                        help='Buffer stdout and stderr during test runs')

    args, test_args = parser.parse_known_args()

    test_args.insert(0, sys.argv[0])
    ret_code = 0

    if args.run_all:
        args.names = ['']
        args.buffer = True

    # create the singleton
    environ = tester.TestEnviron.get_instance(args)

    if not args.names:
        args.names.append('')

    if args.names:
        for name in args.names:
            ret_code |= tester.run_test(
                name,
                args.basedir,
                argv=test_args,
            )

    else:
        environ.unbuffer()
        # http://unix.stackexchange.com/a/8815/118750
        parser.print_help()
        ret_code = 1

    sys.exit(ret_code)