def test_fixture_context_name_is_test_class(self):
        res = unittest.TestResult()
        wd = os.path.join(support, 'package2')
        l = loader.TestLoader(workingDir=wd)
        suite = l.loadTestsFromName('test_pak.test_sub.test_mod:TestMaths')
        suite(res)

        assert 'test_pak' in sys.modules, \
               "Context did not load test_pak"
        m = sys.modules['test_pak']
        # print "test pak state", m.state
        expect = [
            'test_pak.setup', 'test_pak.test_sub.setup',
            'test_pak.test_sub.test_mod.setup',
            'test_pak.test_sub.test_mod.TestMaths.setup_class',
            'test_pak.test_sub.test_mod.TestMaths.setup',
            'test_pak.test_sub.test_mod.TestMaths.test_div',
            'test_pak.test_sub.test_mod.TestMaths.teardown',
            'test_pak.test_sub.test_mod.TestMaths.setup',
            'test_pak.test_sub.test_mod.TestMaths.test_two_two',
            'test_pak.test_sub.test_mod.TestMaths.teardown',
            'test_pak.test_sub.test_mod.TestMaths.teardown_class',
            'test_pak.test_sub.test_mod.teardown',
            'test_pak.test_sub.teardown', 'test_pak.teardown'
        ]
        self.assertEqual(m.state, expect, diff(expect, m.state))
 def test_fixture_context_multiple_names(self):
     res = unittest.TestResult()
     wd = os.path.join(support, 'package2')
     l = loader.TestLoader(workingDir=wd)
     suite = l.loadTestsFromNames([
         'test_pak.test_sub.test_mod:TestMaths.test_div',
         'test_pak.test_sub.test_mod:TestMaths.test_two_two',
         'test_pak.test_mod:test_add'
     ])
     print(suite)
     suite(res)
     assert not res.errors, res.errors
     assert not res.failures, res.failures
     assert 'test_pak' in sys.modules, \
            "Context not load test_pak"
     m = sys.modules['test_pak']
     print("test pak state", m.state)
     expect = [
         'test_pak.setup', 'test_pak.test_sub.setup',
         'test_pak.test_sub.test_mod.setup',
         'test_pak.test_sub.test_mod.TestMaths.setup_class',
         'test_pak.test_sub.test_mod.TestMaths.setup',
         'test_pak.test_sub.test_mod.TestMaths.test_div',
         'test_pak.test_sub.test_mod.TestMaths.teardown',
         'test_pak.test_sub.test_mod.TestMaths.setup',
         'test_pak.test_sub.test_mod.TestMaths.test_two_two',
         'test_pak.test_sub.test_mod.TestMaths.teardown',
         'test_pak.test_sub.test_mod.TestMaths.teardown_class',
         'test_pak.test_sub.test_mod.teardown',
         'test_pak.test_sub.teardown', 'test_pak.test_mod.setup',
         'test_pak.test_mod.test_add', 'test_pak.test_mod.teardown',
         'test_pak.teardown'
     ]
     self.assertEqual(m.state, expect, diff(expect, m.state))
Exemple #3
0
    def test_load_from_sub_package(self):
        res = unittest.TestResult()
        wd = os.path.join(support, 'package2')
        l = loader.TestLoader(workingDir=wd)
        suite = l.loadTestsFromName('test_pak.test_sub')
        suite(res)

        assert 'test_pak' in sys.modules, \
               "Context did not load test_pak"
        m = sys.modules['test_pak']
        print "test pak state", m.state
        expect = [
            'test_pak.setup', 'test_pak.test_sub.setup',
            'test_pak.test_sub.test_sub_init',
            'test_pak.test_sub.test_mod.setup',
            'test_pak.test_sub.test_mod.TestMaths.setup_class',
            'test_pak.test_sub.test_mod.TestMaths.setup',
            'test_pak.test_sub.test_mod.TestMaths.test_div',
            'test_pak.test_sub.test_mod.TestMaths.teardown',
            'test_pak.test_sub.test_mod.TestMaths.setup',
            'test_pak.test_sub.test_mod.TestMaths.test_two_two',
            'test_pak.test_sub.test_mod.TestMaths.teardown',
            'test_pak.test_sub.test_mod.TestMaths.teardown_class',
            'test_pak.test_sub.test_mod.test',
            'test_pak.test_sub.test_mod.teardown',
            'test_pak.test_sub.teardown', 'test_pak.teardown'
        ]
        self.assertEqual(len(m.state), len(expect))
        for item in m.state:
            self.assertEqual(item, expect.pop(0))
    def test_fixture_context_multiple_names_no_common_ancestors(self):
        stream = _WritelnDecorator(StringIO())
        res = _TextTestResult(stream, 0, 2)
        wd = os.path.join(support, 'ltfn')
        l = loader.TestLoader(workingDir=wd)
        suite = l.loadTestsFromNames(
            ['test_pak1.test_mod', 'test_pak2:test_two_two', 'test_mod'])
        print(suite)
        suite(res)
        res.printErrors()
        print(stream.getvalue())
        assert not res.errors, res.errors
        assert not res.failures, res.failures
        assert 'state' in sys.modules, \
               "Context not load state module"
        m = sys.modules['state']
        print("state", m.called)

        expect = [
            'test_pak1.setup', 'test_pak1.test_mod.setup',
            'test_pak1.test_mod.test_one_mod_one',
            'test_pak1.test_mod.teardown', 'test_pak1.teardown',
            'test_pak2.setup', 'test_pak2.test_two_two', 'test_pak2.teardown',
            'test_mod.setup', 'test_mod.test_mod', 'test_mod.teardown'
        ]
        self.assertEqual(m.called, expect, diff(expect, m.called))
Exemple #5
0
def main():

    parser = argparse.ArgumentParser()
    parser.add_argument("platform")
    parser.add_argument("type")
    parser.add_argument("experiment")

    args = parser.parse_args()

    experiment = args.experiment.replace('.', '_').replace('-', '_')

    ret = 1
    if 'test_%s' % experiment in dir(tb.TestBitReproducibility):
        l = loader.TestLoader()
        suite = l.loadTestsFromName(
            "test_bit_reproducibility.py:TestBitReproducibility.test_%s" %
            experiment)
        # nose returns False on failure - want 1.
        ret = not nose.run(suite=suite, argv=[sys.argv[0], '-s'])

    else:

        my_path = os.path.dirname(os.path.realpath(__file__))
        exp_path = os.path.join(my_path, '../', 'exp')
        os.chdir(exp_path)

        # Specific test was not found. Try to just run the experiment directly.
        ret = subprocess.check_call([
            './MOM_run.csh', '--platform', args.platform, '--type', args.type,
            '--experiment', args.experiment, '--download_input_data'
        ])

    return ret
def nose(line, cell=None, test_module=get_ipython_user_ns_as_a_module):
    if callable(test_module):
        test_module = test_module()
    config = makeNoseConfig(os.environ)
    if cell is None:
        # Called as the %nose line magic.
        # All objects in the notebook namespace should be considered for the
        # test suite.
        selector = None
    else:
        # Called as the %%nose cell magic.
        # Classes and functions defined outside the cell should be excluded from
        # the test run.
        selector = ExcludingTestSelector(config, test_module.__dict__.values())
        # Evaluate the cell and add objects it defined into the test module.
        exec(cell, test_module.__dict__)
    loader = nose_loader.TestLoader(config=config, selector=selector)
    tests = loader.loadTestsFromModule(test_module)
    extra_args = shlex.split(str(line))
    expand_tracebacks = '--expand-tracebacks' in extra_args
    if expand_tracebacks:
        extra_args.remove('--expand-tracebacks')
    argv = ['ipython-nose', '--with-ipython-html', '--no-skip'] + extra_args
    verbose = '-v' in extra_args
    plug = IPythonDisplay(verbose=verbose, expand_tracebacks=expand_tracebacks)

    nose_core.TestProgram(argv=argv,
                          suite=tests,
                          addplugins=[plug],
                          exit=False,
                          config=config)

    ## for debugging
    #get_ipython().user_ns['nose_obj'] = plug
    return plug
    def test_fixture_context(self):
        res = unittest.TestResult()
        wd = os.path.join(support, 'package2')
        l = loader.TestLoader(workingDir=wd)
        dir_suite = l.loadTestsFromName('.')
        dir_suite(res)

        m = sys.modules['test_pak']
        print("test pak state", m.state)

        assert not res.errors, res.errors
        assert not res.failures, res.failures
        self.assertEqual(res.testsRun, 5)

        # Expected order of calls
        expect = [
            'test_pak.setup', 'test_pak.test_mod.setup',
            'test_pak.test_mod.test_add', 'test_pak.test_mod.test_minus',
            'test_pak.test_mod.teardown', 'test_pak.test_sub.setup',
            'test_pak.test_sub.test_mod.setup',
            'test_pak.test_sub.test_mod.TestMaths.setup_class',
            'test_pak.test_sub.test_mod.TestMaths.setup',
            'test_pak.test_sub.test_mod.TestMaths.test_div',
            'test_pak.test_sub.test_mod.TestMaths.teardown',
            'test_pak.test_sub.test_mod.TestMaths.setup',
            'test_pak.test_sub.test_mod.TestMaths.test_two_two',
            'test_pak.test_sub.test_mod.TestMaths.teardown',
            'test_pak.test_sub.test_mod.TestMaths.teardown_class',
            'test_pak.test_sub.test_mod.test',
            'test_pak.test_sub.test_mod.teardown',
            'test_pak.test_sub.teardown', 'test_pak.teardown'
        ]
        self.assertEqual(len(m.state), len(expect))
        for item in m.state:
            self.assertEqual(item, expect.pop(0))
def get_module_test_loader(base_module):
    """Takes a base module, and returns a loader already loaded with tests, ready for a TestRunner
    e.g. "import j5test.test_NoseTests ; ld = get_package_test_loader(j5)" will get all tests under j5test.test_NoseTests"""
    workingDir = os.path.dirname(base_module.__file__)
    basename = os.path.basename(base_module.__file__)
    cfg = get_default_config(workingDir=workingDir)
    cfg.testNames = [basename]
    return loader.TestLoader(config=cfg)
Exemple #9
0
 def test_dict_wrapper_instance_not_loaded(self):
     wd = os.path.join(support, 'issue065')
     l = loader.TestLoader()  # workingDir=wd)
     tests = l.loadTestsFromDir(wd)
     tests = list(tests)
     self.assertEqual(len(tests), 1)
     tests = list(tests[0])
     assert not tests, "Tests were loaded from module with no tests"
def get_named_module_test_loader(path_name):
    """Takes a base module name, and returns a loader already loaded with tests, ready for a TestRunner
    e.g. "ld = get_package_test_loader('x.y.z')" will get all tests under x.y.z"""
    workingDir = os.path.dirname(os.path.abspath(path_name))
    basename = os.path.basename(path_name)
    cfg = get_default_config(workingDir=workingDir)
    cfg.testNames = [basename]
    return loader.TestLoader(config=cfg)
 def test_load_from_name_dot(self):
     res = unittest.TestResult()
     wd = os.path.join(support, 'package1')
     l = loader.TestLoader(workingDir=wd)
     dir_suite = l.loadTestsFromName('.')
     dir_suite(res)
     assert not res.errors, res.errors
     assert not res.failures, res.failures
    def test_load_from_name_file(self):
        res = unittest.TestResult()
        wd = os.path.join(support, 'package1')
        l = loader.TestLoader(workingDir=wd)

        file_suite = l.loadTestsFromName('tests/test_example_function.py')
        file_suite(res)
        assert not res.errors, res.errors
        assert not res.failures, res.failures
Exemple #13
0
 def Randomized_loadTestsFromTestCase(self, testCaseClass):
     l = loader.TestLoader()
     tmp = l.loadTestsFromTestCase(testCaseClass)
     randomized_tests = []
     for t in tmp._tests:
         randomized_tests.append(t)
     random.shuffle(randomized_tests)
     tmp._tests = (t for t in randomized_tests)
     return tmp
Exemple #14
0
def nose(line):
    test_module = types.ModuleType('test_module')
    test_module.__dict__.update(get_ipython().user_ns)

    loader = nose_loader.TestLoader()
    tests = loader.loadTestsFromModule(test_module)

    tester = TestProgram(argv=['dummy'], suite=tests)
    return tester.result
 def test_load_from_name_file_callable(self):
     res = unittest.TestResult()
     wd = os.path.join(support, 'package1')
     l = loader.TestLoader(workingDir=wd)
     suite = l.loadTestsFromName(
         'tests/test_example_function.py:test_times_two')
     suite(res)
     assert not res.errors, res.errors
     assert not res.failures, res.failures
     self.assertEqual(res.testsRun, 1)
    def test_mod_setup_fails_no_tests_run(self):
        ctx = os.path.join(support, 'ctx')
        l = loader.TestLoader(workingDir=ctx)
        suite = l.loadTestsFromName('mod_setup_fails.py')

        res = unittest.TestResult()
        suite(res)

        assert res.errors
        assert not res.failures, res.failures
        assert res.testsRun == 0, \
               "Expected to run 0 tests but ran %s" % res.testsRun
    def test_load_nonsense_name(self):
        ctx = os.path.join(support, 'ctx')
        l = loader.TestLoader(workingDir=ctx)
        suite = l.loadTestsFromName('fred!')

        res = _TextTestResult(stream=_WritelnDecorator(sys.stdout),
                              descriptions=0,
                              verbosity=1)
        suite(res)
        print(res.errors)
        assert res.errors, "Expected errors but got none"
        assert not res.failures, res.failures
 def test_issue_269(self):
     """Test classes that raise exceptions in __init__ do not stop test run
     """
     wdir = os.path.join(support, 'issue269')
     l = loader.TestLoader(workingDir=wdir)
     suite = l.loadTestsFromName('test_bad_class')
     res = _TextTestResult(stream=_WritelnDecorator(sys.stdout),
                           descriptions=0,
                           verbosity=1)
     suite(res)
     print(res.errors)
     self.assertEqual(len(res.errors), 1)
     assert 'raise Exception("pow")' in res.errors[0][1]
    def test_mod_import_skip_one_test_no_errors(self):
        config = Config(plugins=PluginManager(plugins=[Skip()]))
        ctx = os.path.join(support, 'ctx')
        l = loader.TestLoader(workingDir=ctx, config=config)
        suite = l.loadTestsFromName('mod_import_skip.py')

        res = unittest.TestResult()
        suite(res)

        assert not res.errors, res.errors
        assert not res.failures, res.failures
        assert res.testsRun == 1, \
               "Expected to run 1 tests but ran %s" % res.testsRun
    def loadTestsFromTestCase(self, testCaseClass):
        print("Called!")

        class Derived(testCaseClass):
            def setUp(self):
                pass

            def tearDown(self):
                pass

        # must use nose loader here because the default loader in 2.3
        # won't load tests from base classes
        l = loader.TestLoader()
        return l.loadTestsFromTestCase(Derived)
Exemple #21
0
    def makeTest(self, obj, parent=None):
        """Given a test object and its parent, return a test case
        or test suite.
        """
        ldr = loader.TestLoader()
        if isinstance(obj, unittest.TestCase):
            return obj
        elif isclass(obj):
            if not self.class_specific:
                if parent and obj.__module__ != parent.__name__:
                    obj = transplant_class(obj, parent.__name__)
                if issubclass(obj, unittest.TestCase):
                    # Randomize the order of the tests in the TestCase
                    return self.randomized_loadTestsFromTestCase(obj)
                else:
                    return self.randomized_loadTestsFromTestClass(obj)
            else:
                class_specific_seed = getattr(
                    obj, CLASS_SPECIFIC_RANDOMIZE_TESTS_FIELD_NAME, None)
                if issubclass(
                        obj,
                        unittest.TestCase) and class_specific_seed is not None:
                    random.seed(class_specific_seed)
                    print("Using %d as seed to randomize tests in %s" %
                          (class_specific_seed, '.'.join(
                              [obj.__module__, obj.__name__])))
                    return self.randomized_loadTestsFromTestCase(obj)
                else:
                    return ldr.loadTestsFromTestCase(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 ldr.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 ldr.loadTestsFromGenerator(obj, parent)
            else:
                return [FunctionTestCase(obj)]
        else:
            return Failure(TypeError, "Can't make a test from %s" % obj)
    def test_mod_setup_skip_no_tests_run_no_errors(self):
        config = Config(plugins=PluginManager(plugins=[Skip()]))
        ctx = os.path.join(support, 'ctx')
        l = loader.TestLoader(workingDir=ctx, config=config)
        suite = l.loadTestsFromName('mod_setup_skip.py')

        res = unittest.TestResult()
        suite(res)

        assert not suite.was_setup, "Suite setup did not fail"
        assert not res.errors, res.errors
        assert not res.failures, res.failures
        assert res.skipped
        assert res.testsRun == 0, \
               "Expected to run 0 tests but ran %s" % res.testsRun
    def test_generator_with_closure(self):
        """Test that a generator test can employ a closure

        Issue #3. If the generator binds early, the last value
        of the closure will be seen for each generated test and
        the tests will fail.
        """
        gen = os.path.join(support, 'gen')
        l = loader.TestLoader(workingDir=gen)
        suite = l.loadTestsFromName('test')
        res = _TextTestResult(stream=_WritelnDecorator(sys.stdout),
                              descriptions=0,
                              verbosity=1)
        suite(res)
        assert not res.errors
        self.assertEqual(res.testsRun, 5)
    def test_failed_import_module_name(self):
        ctx = os.path.join(support, 'ctx')
        l = loader.TestLoader(workingDir=ctx)
        suite = l.loadTestsFromName('no_such_module')

        res = _TextTestResult(stream=_WritelnDecorator(sys.stdout),
                              descriptions=0,
                              verbosity=1)
        suite(res)
        print(res.errors)
        res.printErrors()
        assert res.errors, "Expected errors but got none"
        assert not res.failures, res.failures
        err = res.errors[0][0].test.exc_class
        assert err is ImportError, \
            "Expected import error, got %s" % err
    def test_failed_import(self):
        ctx = os.path.join(support, 'ctx')
        l = loader.TestLoader(workingDir=ctx)
        suite = l.loadTestsFromName('no_such_module.py')

        res = _TextTestResult(stream=_WritelnDecorator(sys.stdout),
                              descriptions=0,
                              verbosity=1)
        suite(res)

        print(res.errors)
        res.printErrors()
        assert res.errors, "Expected errors but got none"
        assert not res.failures, res.failures
        assert res.testsRun == 1, \
               "Expected to run 1 tests but ran %s" % res.testsRun
def nose(line, test_module=get_ipython_user_ns_as_a_module):
    if callable(test_module):
        test_module = test_module()
    config = makeNoseConfig(os.environ)
    loader = nose_loader.TestLoader(config=config)
    tests = loader.loadTestsFromModule(test_module)
    extra_args = shlex.split(str(line))
    argv = ['ipython-nose', '--with-ipython-html', '--no-skip'] + extra_args
    verbose = '-v' in extra_args
    plug = IPythonDisplay(verbose=verbose)

    nose_core.TestProgram(argv=argv,
                          suite=tests,
                          addplugins=[plug],
                          exit=False,
                          config=config)

    return plug
    def test_fixture_context_name_is_test_function(self):
        res = unittest.TestResult()
        wd = os.path.join(support, 'package2')
        l = loader.TestLoader(workingDir=wd)
        suite = l.loadTestsFromName('test_pak.test_mod:test_add')
        suite(res)

        assert 'test_pak' in sys.modules, \
               "Context did not load test_pak"
        m = sys.modules['test_pak']
        print("test pak state", m.state)
        expect = [
            'test_pak.setup', 'test_pak.test_mod.setup',
            'test_pak.test_mod.test_add', 'test_pak.test_mod.teardown',
            'test_pak.teardown'
        ]
        self.assertEqual(len(m.state), len(expect))
        for item in m.state:
            self.assertEqual(item, expect.pop(0))
Exemple #28
0
def generate_suites(root, test_sets):
    """
    生成测试套件
    """
    suites = suite.LazySuite()

    sys.path.append(root)
    #print "sys.path :"
    #for path_i in sys.path:
    #    print path_i

    for ts_name, ts in test_sets.iteritems():
        for tc in ts:
            print "tc xxxxxxx: ", tc
            tc_complete = ts_name + "." + tc  # 用例文件完整的路径
            print "tc_complete: ", tc_complete
            pprint(sys.path)
            module = importlib.import_module(tc_complete)
            print "module: ", module
            suites.addTest(loader.TestLoader().loadTestsFromModule(module))
    return suites
Exemple #29
0
def generate_suites(root, test_sets):
    """
    生成测试套件
    """
    suites = suite.LazySuite()

    sys.path.append(root)
    print(root)
    sys.path.append(
        os.path.dirname(os.path.abspath(__file__)) + "/../testcase")
    if IS_DEBUG:
        pprint(sys.path)

    for ts_name, ts in test_sets.items():
        for tc in ts:
            print("用例文件名: ", tc)
            tc_complete = ts_name + "." + tc  # 用例文件完整的路径
            print("用例路径: ", tc_complete)
            module = importlib.import_module(tc_complete)
            print("用例module: ", module)
            print("")
            suites.addTest(loader.TestLoader().loadTestsFromModule(module))
    return suites
Exemple #30
0
    def makeTest(self, obj, parent=None):

        """Given a test object and its parent, return a test case
        or test suite.
        """
        ldr = loader.TestLoader()
        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):
                # Randomize the order of the tests in the TestCase
                return self.Randomized_loadTestsFromTestCase(obj)
            else:
                return ldr.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 ldr.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 ldr.loadTestsFromGenerator(obj, parent)
            else:
                return FunctionTestCase(obj)
        else:
            return Failure(TypeError,
                           "Can't make a test from %s" % obj)