Ejemplo 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(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(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'))
Ejemplo n.º 2
0
def testSchemes(modules=None, skip=None):
    '''
    Searches the given modules for methods named 'main'
    returns a test suite with one test for each main method
    If modules==None, all modules are searched
    '''
    
    suite = unittest.TestSuite()
    
    if modules==None:
        #Exclude unit tests
        modules = [mod for mod in all_unittests.find_modules() if not re.match(".*_test$", mod)]
        
    if skip==None:
        skip = []
    else:
        modules = list(set(modules).difference(set(skip)))
        modules.sort() 
    
    for name in modules:
        mod = all_unittests.load_module(name)
        if hasattr(mod, 'main'):
            try:
               case = unittest.FunctionTestCase(mod.main, None, None, mod.__name__)
               suite.addTest(case)
            except Exception as e:
               print("Stack Trace =>", e)
Ejemplo n.º 3
0
    def test_loadTestsFromNames__callable__TestSuite(self):
        m = types.ModuleType('m')
        testcase_1 = unittest.FunctionTestCase(lambda: None)
        testcase_2 = unittest.FunctionTestCase(lambda: None)

        def return_TestSuite():
            return unittest.TestSuite([testcase_1, testcase_2])

        m.return_TestSuite = return_TestSuite

        loader = unittest.TestLoader()
        suite = loader.loadTestsFromNames(['return_TestSuite'], m)
        self.assertIsInstance(suite, loader.suiteClass)

        expected = unittest.TestSuite([testcase_1, testcase_2])
        self.assertEqual(list(suite), [expected])
Ejemplo n.º 4
0
def _factory(filename, dirname):

    pwd = os.getcwd()
    short_path = os.path.relpath(filename, os.path.dirname(__file__))

    def setUp():
        os.chdir(dirname)

    def tearDown():
        os.chdir(pwd)

    def runTest():
        buf = StringIO()
        basename = os.path.basename(filename)
        if basename.startswith('test_sa'):
            argv = ["-Q", "-a", filename]
        elif basename.startswith('test_metal'):
            argv = ["-Q", "-m", filename]
        else:
            argv = ["-Q", filename]
        try:
            failed = zope.tal.runtest.main(argv, buf)
        finally:
            captured_stdout = buf.getvalue()
        if failed:
            raise AssertionError("output for %s didn't match:\n%s" %
                                 (filename, captured_stdout))

    return unittest.FunctionTestCase(runTest, setUp, tearDown, short_path)
Ejemplo n.º 5
0
 def assertEqualReservations(r1, r2):
     t = unittest.FunctionTestCase(TestReservation)
     t.assertEqual(r1.user_id, r2.user_id)
     t.assertEqual(r1.table_id, r2.table_id)
     t.assertEqual(r1.restaurant_id, r2.restaurant_id)
     t.assertEqual(r1.people_number, r2.people_number)
     t.assertEqual(r1.start_time, r2.start_time)
Ejemplo n.º 6
0
    def getFuncTest(self):
        print '-------------- Unit Test for %s ---------------' % self.testname
        """Wrap a script using unittest"""
        testscript = self.searchscript(self.testname, self.scriptdir)

        # avoid creating a _work directory
        if (testscript == ""):
            return

        # copy test to local directory
        self.workdir = os.getcwd()
        self.getTest(testscript, self.testname, self.scriptdir, self.workdir)

        # import the test
        mytest = __import__(self.testname)
        reload(mytest)

        #get the data
        try:
            self.dataFiles = mytest.data()
        except:
            print 'No data needed or found'

        # Wrap the test, funcSetup and funcTeardown in a FunctionTestCase and return it
        testcase = (unittest.FunctionTestCase(mytest.run,
                                              setUp=self.funcSetup,
                                              tearDown=self.funcTeardown,
                                              description=self.funcdesc()))

        return testcase
Ejemplo n.º 7
0
def suite():
    # Loop over all .py files here, except me :)
    try:
        me = __file__
    except NameError:
        me = sys.argv[0]
    me = os.path.abspath(me)
    files = os.listdir(os.path.dirname(me))
    suite = unittest.TestSuite()
    suite.addTest(unittest.FunctionTestCase(import_all))
    for file in files:
        base, ext = os.path.splitext(file)
        if ext=='.py' and os.path.basename(me) != file:
            try:
                mod = __import__(base)
            except:
                print("FAILED to import test module %r" % base)
                traceback.print_exc()
                continue
            if hasattr(mod, "suite"):
                test = mod.suite()
            else:
                test = unittest.defaultTestLoader.loadTestsFromModule(mod)
            suite.addTest(test)
    for test in get_demo_tests():
        suite.addTest(test)
    return suite
Ejemplo n.º 8
0
 def assertRatingEquals(r1, r2):
     t = unittest.FunctionTestCase(TestRestaurantRating)
     t.assertEqual(r1.customer_id, r2.customer_id)
     t.assertEqual(r1.restaurant_id, r2.restaurant_id)
     t.assertEqual(r1.customer_name, r2.customer_name)
     t.assertEqual(r1.value, r2.value)
     t.assertEqual(r1.review, r2.review)
Ejemplo n.º 9
0
def _generate_tests_from_file(tax_benefit_system, path_to_file, options):
    filename = os.path.splitext(os.path.basename(path_to_file))[0]
    name_filter = options.get('name_filter')
    if isinstance(name_filter, str):
        name_filter = to_unicode(name_filter)
    verbose = options.get('verbose')
    only_variables = options.get('only_variables')
    ignore_variables = options.get('ignore_variables')

    tests = _parse_test_file(tax_benefit_system, path_to_file)

    for test_index, (path_to_file, name, period_str,
                     test) in enumerate(tests, 1):
        if name_filter is not None and name_filter not in filename \
                and name_filter not in (test.get('name', '')) \
                and name_filter not in (test.get('keywords', [])):
            continue

        keywords = test.get('keywords', [])
        title = "{}: {}{} - {}".format(
            os.path.basename(path_to_file),
            '[{}] '.format(', '.join(keywords)) if keywords else '',
            name,
            period_str,
        )

        def check():
            try:
                _run_test(period_str, test, verbose, only_variables,
                          ignore_variables, options)
            except Exception:
                log.error(title)
                raise

        yield unittest.FunctionTestCase(check)
Ejemplo n.º 10
0
def create_examples_testsuite():
    # gather information on examples
    # all functions inside the examples starting with 'ex_' or 'recipe_'
    # are considered as tests
    # find example files in examples directory
    root_dir = 'examples/'
    files = []
    skip = ['__init__.py']
    for root, _, filenames in os.walk(root_dir):
        for filename in filenames:
            if filename in skip or filename[-3:] != '.py':
                continue
            if 'examples/data' in root:
                continue
            f = os.path.join(root, filename)
            f = f.replace('/', '.')
            f = f[:-3]
            files.append(f)
    # create empty testsuite
    suite = unittest.TestSuite()
    # find matching functions in
    for idx, module in enumerate(files):
        module1, func = module.split('.')
        module = __import__(module)
        func = getattr(module, func)
        funcs = inspect.getmembers(func, inspect.isfunction)
        [
            suite.addTest(unittest.FunctionTestCase(v)) for k, v in funcs
            if k.startswith(("ex_", "recipe_"))
        ]

    return suite
Ejemplo n.º 11
0
def make_test(example_dir):
    def test_example():
        temp_dir = tempfile.mkdtemp(prefix='switch_test_')
        try:
            # Custom python modules may be in the example's working directory
            sys.path.append(example_dir)
            args = switch_model.solve.get_option_file_args(dir=example_dir,
                extra_args=[
                    '--inputs-dir', os.path.join(example_dir, 'inputs'),
                    '--outputs-dir', temp_dir])
            switch_model.solve.main(args)
            total_cost = read_file(os.path.join(temp_dir, 'total_cost.txt'))
        finally:
            sys.path.remove(example_dir)
            _remove_temp_dir(temp_dir)
        expectation_file = get_expectation_path(example_dir)
        if UPDATE_EXPECTATIONS:
            write_file(expectation_file, total_cost)
        else:
            expected = float(read_file(expectation_file))
            actual = float(total_cost)
            if not switch_model.utilities.approx_equal(expected, actual,
                                                     tolerance=0.0001):
                raise AssertionError(
                    'Mismatch for total_cost (the objective function value):\n'
                    'Expected value:  {}\n'
                    'Actual value:    {}\n'
                    'Run "python -m tests.examples_test --update" to '
                    'update the expectations if this change is expected.'
                    .format(expected, actual))

    name = os.path.relpath(example_dir, TOP_DIR)
    return unittest.FunctionTestCase(
        test_example, description='Example: %s' % name)
Ejemplo n.º 12
0
def generate_test_suite_from_traces():
    def generate_test_from_file(core, tracefile):
        tracepath, cmd = get_trace_path_cmd_tuple(core, tracefile)

        def test():
            print("Running test %s " % (tracefile[:-6]))
            trace_test(core, tracepath, cmd)

        # Rename the function to the tracefile name without .trace suffix
        test.__name__ = tracefile[:-6]
        if cmd:
            return test
        else:
            return unittest.skip(
                "No command specified in trace {}".format(tracepath))(test)

    suite = unittest.TestSuite()
    for core in os.listdir(tracedir):
        if os.path.isdir(os.path.join(tracedir, core)):
            core_suite = unittest.TestSuite()
            for tracefile in os.listdir(os.path.join(tracedir, core)):
                if tracefile.endswith(".trace"):
                    core_suite.addTest(
                        unittest.FunctionTestCase(generate_test_from_file(
                            core, tracefile),
                                                  description=tracefile))
            suite.addTest(core_suite)

    return suite
Ejemplo n.º 13
0
    def __init__(self, result=None, test=None):
        """
        """

        self.result = result

        if result is None:
            self.reset()

        # names of methods that record failure or success in TestCase
        # instances.  We wrap them all with our results recording decorator.
        recmethods = [
            'assertAlmostEqual', 'assertAlmostEquals', 'assertEqual',
            'assertEquals', 'assertFalse', 'assertNotAlmostEqual',
            'assertNotAlmostEquals', 'assertNotEqual', 'assertNotEquals',
            'assertRaises', 'assertTrue', 'assert_', 'fail', 'failIf',
            'failIfAlmostEqual', 'failIfEqual', 'failUnless',
            'failUnlessAlmostEqual', 'failUnlessEqual', 'failUnlessRaises',
            'failureException'
        ]

        if test is None:
            # standalone 'script' mode: there is no outside test runner
            mode = 'script'
            test = unittest.FunctionTestCase(lambda *x, **k: None)
            self._rec_wrap(test, recmethods)
        else:
            # unittest mode, when run from inside an existing test runner
            mode = 'unittest'
            self._expose_from(test, recmethods)

        self.test = test
        self.mode = mode
 def assertOperatorsEquals(value, expected):
     t = unittest.FunctionTestCase(TestOperator)
     t.assertEqual(value.email, expected.email)
     t.assertEqual(value.password, expected.password)
     t.assertEqual(value.is_active, expected.is_active)
     t.assertEqual(value.authenticated, False)
     t.assertEqual(value.is_anonymous, expected.is_anonymous)
Ejemplo n.º 15
0
    def run(self, suite=None, module_name="Mufat"):
        if not suite:
            suite = unittest.TestSuite()
            module = __import__(module_name, fromlist="dummy")
            for test in make_tests(module):
                suite.addTest(test)

        if type(suite) is unittest.TestSuite:
            if not "Init" in (t.id() for t in suite._tests[:1]):
                from . import Init
                suite._tests.insert(0, unittest.FunctionTestCase(Init))
            if not "Release" in (t.id() for t in suite._tests[-1:]):
                from . import Release
                suite._tests.append(unittest.FunctionTestCase(Release))

        # execute tests
        return super(MufatTestRunner, self).run(suite)
Ejemplo n.º 16
0
def make_tests(module):
    """Helper function to generate a list of `unittest.FunctionTestCase` from a module"""

    from . import Init, Release
    for func in module.__dict__.itervalues():
        if type(func) == FunctionType and getattr(func, "is_a_testcase",
                                                  False):
            yield unittest.FunctionTestCase(func, setUp=Init, tearDown=Release)
 def assertEqualCustomers(c1, c2):
     t = unittest.FunctionTestCase(TestCustomer)
     t.assertEqual(c1.firstname, c2.firstname)
     t.assertEqual(c1.lastname, c2.lastname)
     t.assertEqual(c1.birthdate, c2.birthdate)
     t.assertEqual(c1.social_number, c2.social_number)
     t.assertEqual(c1.health_status, c2.health_status)
     t.assertEqual(c1.phone, c2.phone)
Ejemplo n.º 18
0
def make_test(config_file, description):
    def test_function():
        root, handlers = ZConfig.loadConfig(schema, config_file)
    # Hack the config file name into test_function's __name__ so that the test
    # -vv output is more informative. Unfortunately, FunctionTestCase's
    # description argument doesn't do what we want.
    test_function.__name__ = description
    return unittest.FunctionTestCase(test_function)
Ejemplo n.º 19
0
    def test_countTestCases_nested(self):
        class Test1(unittest.TestCase):
            def test1(self):
                pass

            def test2(self):
                pass

        test2 = unittest.FunctionTestCase(lambda: None)
        test3 = unittest.FunctionTestCase(lambda: None)
        child = unittest.TestSuite((Test1('test2'), test2))
        parent = unittest.TestSuite((test3, child, Test1('test1')))

        self.assertEqual(parent.countTestCases(), 4)
        # countTestCases() still works after tests are run
        parent.run(unittest.TestResult())
        self.assertEqual(parent.countTestCases(), 4)
        self.assertEqual(child.countTestCases(), 2)
Ejemplo n.º 20
0
def make_suite():
    suite = unittest.TestSuite()
    # Note 2
    suite.addTest(unittest.makeSuite(UnitTests01, sortUsing=compare_names))
    # Note 3
    suite.addTest(unittest.makeSuite(UnitTests02, prefix="check"))
    # Note 4
    suite.addTest(unittest.FunctionTestCase(function_test_1))
    return suite
Ejemplo n.º 21
0
def mark_test(f, argtuples=None, suite=None):
    """Mark a function-based set as a test"""
    suite = suite or unittest.TestSuite()
    for argstuple in argtuples:
        def partial_f(argstuple=argstuple):
            return f(*argstuple)
        partial_f.__name__ = '%s[%s]' % (f.__name__, argstuple)
        suite.addTest(unittest.FunctionTestCase(partial_f))
    return suite
Ejemplo n.º 22
0
def load_tests(loader, tests, pattern):
    # python3 unittest interface
    suite = unittest.TestSuite()
    for key, pairs in load_tests_dict().items():
        test = unittest.FunctionTestCase(
            (lambda key, pairs: lambda: run_test(key, pairs))(key, pairs),
            description=key)
        suite.addTests([test])
    return suite
Ejemplo n.º 23
0
 def test_all_colors(self):
     suite = unittest.TestSuite()
     for h, c in [("0xff0000", "red"),
                  ("0x00ff00", "green"),
                  ("0x0000ff", "blue")]:
         suite.addTest(
             unittest.FunctionTestCase(
                 lambda _h=h, _c=c: self.assertEqual(_h, colorToHex(_c))))
     self.assertTrue(unittest.TextTestRunner().run(suite).wasSuccessful())
Ejemplo n.º 24
0
 def test_hex_to_rgb(self):
     suite = unittest.TestSuite()
     for h, rgb in [("0xff0000", RGB(255, 0, 0)),
                    ("0x00ff00", RGB(0, 255, 0)),
                    ("0x0000ff", RGB(0, 0, 255))]:
         suite.addTest(
             unittest.FunctionTestCase(
                 lambda _h=h, _rgb=rgb: self.assertEqual(_rgb, hexToRGB(_h))))
     self.assertTrue(unittest.TextTestRunner().run(suite).wasSuccessful())
 def assertEqualRestaurants(r1, r2):
     t = unittest.FunctionTestCase(TestRestaurant)
     t.assertEqual(r1.name, r2.name)
     t.assertEqual(r1.address, r2.address)
     t.assertEqual(r1.city, r2.city)
     t.assertEqual(r1.lat, r2.lat)
     t.assertEqual(r1.lon, r2.lon)
     t.assertEqual(r1.phone, r2.phone)
     t.assertEqual(r1.menu_type, r2.menu_type)
Ejemplo n.º 26
0
        def wrapper(*args, **kwargs):
            assert len(args) > 0
            instance = args[0]
            assert isinstance(instance, unittest.TestCase)
            success_counter = 0
            failure_counter = 0
            results = []

            def fail():
                msg = '\nFail: {0}, Success: {1}'.format(
                    failure_counter, success_counter)
                if results:
                    first = results[0]
                    errs = first.failures + first.errors
                    if errs:
                        err_msg = '\n'.join(fail[1] for fail in errs)
                        msg += '\n\nThe first error message:\n' + err_msg
                instance.fail(msg)

            # Wrapper to convert pytest.skip() to unittest.SkipTest
            def f_wrap(ins, args, kwargs):
                try:
                    f(ins, *args[1:], **kwargs)
                except _pytest.outcomes.Skipped as e:
                    ins.skipTest(e.msg)

            for _ in six.moves.range(times):
                suite = unittest.TestSuite()
                # Create new instance to call the setup and the teardown only
                # once.
                ins = type(instance)(instance._testMethodName)
                suite.addTest(
                    unittest.FunctionTestCase(
                        lambda: f_wrap(ins, args, kwargs),
                        setUp=ins.setUp,
                        tearDown=ins.tearDown))

                result = QuietTestRunner().run(suite)
                if len(result.skipped) == 1:
                    # "Skipped" is a special case of "Successful".
                    # When the test has been skipped, immediately quit the
                    # test regardless of `times` and `min_success` by raising
                    # SkipTest exception using the original reason.
                    instance.skipTest(result.skipped[0][1])
                elif result.wasSuccessful():
                    success_counter += 1
                else:
                    results.append(result)
                    failure_counter += 1
                if success_counter >= min_success:
                    instance.assertTrue(True)
                    return
                if failure_counter > times - min_success:
                    fail()
                    return
            fail()
Ejemplo n.º 27
0
def test_suite():
    """
    Collect all of the tests together in a single suite.
    """
    addParentToPath()
    combined_suite = unittest.TestSuite()
    combined_suite.addTests(specialCaseTests())
    combined_suite.addTests(pyUnitTests())
    combined_suite.addTest(unittest.FunctionTestCase(djangoExampleTests))
    return combined_suite
def discover_tests_module(module):
    """Generate testcases from the module."""
    for name, obj in [(name, getattr(module, name)) for name in dir(module)]:
        if inspect.isclass(obj) and (issubclass(obj, unittest.TestCase)
                                     or re.match('test.*', name)):
            yield from discover_tests_class(obj)
        elif callable(obj) and re.match('test_.*', name):
            if inspect.isgeneratorfunction(obj):
                for name, *test in obj():
                    f = functools.partial(*test)
                    testcase = unittest.FunctionTestCase(f)
                    testcase._testcase_name = "{}.{}:{}".format(
                        obj.__module__, obj.__name__, name)
                    yield testcase
            else:
                testcase = unittest.FunctionTestCase(obj)
                testcase._testcase_name = "{}.{}".format(
                    obj.__module__, obj.__name__)
                yield testcase
Ejemplo n.º 29
0
def create_test(module, description):
    error_format = "\nExpected:  %s\nGot:       %s"

    def test_module():
        value = module.buildFibonacciSequence(15)
        if value != fibonacciSequence:
            raise AssertionError(error_format % (fibonacciSequence, value))

    test_module.__name__ = 'test_' + module.__name__
    return unittest.FunctionTestCase(test_module, description=description)
def run_tests_without_pytest():
    """Runs the tests in this file without using pytest.
    """
    main_module = sys.modules['__main__']
    test_functions = [getattr(main_module, name) for name in dir(main_module)
                      if name.startswith('test_')]
    test_cases = [unittest.FunctionTestCase(fn) for fn in test_functions]
    suite = unittest.TestSuite()
    suite.addTests(test_cases)
    runner = unittest.TextTestRunner()
    runner.run(suite)