Beispiel #1
0
def collector():
    """TestSuite replacement entry point. Use anywhere you might use a
    unittest.TestSuite. The collector will, by default, load options from
    all config files and execute loader.loadTestsFromNames() on the
    configured testNames, or '.' if no testNames are configured.
    """    
    # plugins that implement any of these methods are disabled, since
    # we don't control the test runner and won't be able to run them
    # finalize() is also not called, but plugins that use it aren't disabled,
    # because capture needs it.
    setuptools_incompat = ('report', 'prepareTest',
                           'prepareTestLoader', 'prepareTestRunner',
                           'setOutputStream')

    plugins = RestrictedPluginManager(exclude=setuptools_incompat)
    conf = Config(files=all_config_files(),
                  plugins=plugins)
    conf.configure(argv=['collector'])
    loader = defaultTestLoader(conf)

    if conf.testNames:
        suite = loader.loadTestsFromNames(conf.testNames)
    else:
        suite = loader.loadTestsFromNames(('.',))
    return FinalizingSuiteWrapper(suite, plugins.finalize)
Beispiel #2
0
def collector():
    """TestSuite replacement entry point. Use anywhere you might use a
    unittest.TestSuite. The collector will, by default, load options from
    all config files and execute loader.loadTestsFromNames() on the
    configured testNames, or '.' if no testNames are configured.
    """
    # plugins that implement any of these methods are disabled, since
    # we don't control the test runner and won't be able to run them
    # finalize() is also not called, but plugins that use it aren't disabled,
    # because capture needs it.
    setuptools_incompat = ('report', 'prepareTest',
                           'prepareTestLoader', 'prepareTestRunner',
                           'setOutputStream')

    plugins = RestrictedPluginManager(exclude=setuptools_incompat)
    conf = Config(files=all_config_files(),
                  plugins=plugins)
    conf.configure(argv=['collector'])
    loader = defaultTestLoader(conf)

    if conf.testNames:
        suite = loader.loadTestsFromNames(conf.testNames)
    else:
        suite = loader.loadTestsFromNames(('.',))
    return FinalizingSuiteWrapper(suite, plugins.finalize)
Beispiel #3
0
class TestIdTest(unittest.TestCase):
    tests_location = "tests/data/testid/testid.py"
    idfile_location = "data/testid/.noseids"

    def setUp(self):
        self.idfile = os.path.abspath(
            os.path.join(os.path.dirname(__file__), self.idfile_location))
        parser = optparse.OptionParser()
        argv = [
            # 0 is always program
            "lode_runner",
            "--failed",
            "--with-id",
            "--id-file=%s" % self.idfile
        ]
        self.x = TestId()
        self.x.add_options(parser, env={})
        (options, args) = parser.parse_args(argv)
        self.config = Config()
        self.x.configure(options, self.config)
        self.config.plugins = PluginManager()
        self.config.plugins.addPlugin(Dataprovider())
        self.config.plugins.addPlugin(TestId())
        self.config.configure(argv)

    def tearDown(self):
        try:
            os.remove(self.idfile)
        except OSError:
            pass

    def test_load_tests_path_with_no_info_in_idfile(self):
        names = self.x.loadTestsFromNames([self.tests_location])
        self.assertEqual((None, [self.tests_location]), names)

    def test_loaded_names_with_failing_tests_in_idfile(self):
        stream = StringIO()

        tests = TestLoader(config=self.config).loadTestsFromName(
            self.tests_location)
        result = LodeTestResult(stream, None, 0)
        tests.run(result)
        # generate needed idfile
        self.config.plugins.finalize(result)

        names = self.x.loadTestsFromNames([self.tests_location])
        loaded_tests = [(parse_test_name(name)[1], parse_test_name(name)[2])
                        for name in names[1]]
        self.assertEqual([
            ('DataprovidedTestCase',
             'test_with_dataprovider_failing_on_everything_except_2_with_dataset_0'
             ),
            ('DataprovidedTestCase',
             'test_with_dataprovider_failing_on_everything_except_2_with_dataset_2'
             )
        ], loaded_tests)
Beispiel #4
0
class TestIdTest(unittest.TestCase):
    tests_location = "tests/data/testid/testid.py"
    idfile_location = "data/testid/.noseids"

    def setUp(self):
        self.idfile = os.path.abspath(
            os.path.join(os.path.dirname(__file__), self.idfile_location))
        parser = optparse.OptionParser()
        argv = [
            # 0 is always program
            "lode_runner",
            "--failed",
            "--with-id",
            "--id-file=%s" % self.idfile
        ]
        self.x = TestId()
        self.x.add_options(parser, env={})
        (options, args) = parser.parse_args(argv)
        self.config = Config()
        self.x.configure(options, self.config)
        self.config.plugins = PluginManager()
        self.config.plugins.addPlugin(Dataprovider())
        self.config.plugins.addPlugin(TestId())
        self.config.configure(argv)

    def tearDown(self):
        try:
            os.remove(self.idfile)
        except OSError:
            pass

    def test_load_tests_path_with_no_info_in_idfile(self):
        names = self.x.loadTestsFromNames([self.tests_location])
        self.assertEqual((None, [self.tests_location]), names)

    def test_loaded_names_with_failing_tests_in_idfile(self):
        stream = StringIO()

        tests = TestLoader(config=self.config).loadTestsFromName(self.tests_location)
        result = LodeTestResult(stream, None, 0)
        tests.run(result)
        # generate needed idfile
        self.config.plugins.finalize(result)

        names = self.x.loadTestsFromNames([self.tests_location])
        loaded_tests = [(parse_test_name(name)[1], parse_test_name(name)[2]) for name in names[1]]
        self.assertEqual(
            [('DataprovidedTestCase','test_with_dataprovider_failing_on_everything_except_2_1'),
             ('DataprovidedTestCase','test_with_dataprovider_failing_on_everything_except_2_3')], loaded_tests)
class DiscoverTest(unittest.TestCase):
    tests_location = "tests/data/dataprovided/dataprovided.py"
    tested_test = ":TestCase.test_with_dataprovider_fixture_2"
    argv = []

    ran_1_test = "Ran 1 test"
    no_such_test = "ValueError: No such test"

    def setUp(self):
        self.config = Config()
        self.config.plugins = PluginManager()
        self.config.plugins.addPlugin(Dataprovider())
        self.config.configure(self.argv)

    def tearDown(self):
        del sys.modules["dataprovided"]
        self.argv = []
class DiscoverTest(unittest.TestCase):
    tests_location = "tests/data/dataprovided/dataprovided.py"
    tested_test = ":TestCase.test_with_dataprovider_fixture_2"
    argv = []

    ran_1_test = "Ran 1 test"
    no_such_test = "ValueError: No such test"

    def setUp(self):
        self.config = Config()
        self.config.plugins = PluginManager()
        self.config.plugins.addPlugin(Dataprovider())
        self.config.configure(self.argv)

    def tearDown(self):
        del sys.modules["dataprovided"]
        self.argv = []
Beispiel #7
0
class BaseDataproviderTest(unittest.TestCase):
    path = os.path.dirname(
        os.path.realpath(__file__)) + "/data/dataprovided/dataprovided.py"
    test = "test_raw_dataprovider_with_dataset_1"
    test_case = "DataproviderTest"
    test_full_name = "%s:%s.%s" % (path, test_case, test)
    ran_1_test = "Ran 1 test"
    no_such_test = "ValueError: No such test"

    def cleanup(self):
        del sys.modules["dataprovided"]
        self.argv = []

    def setUp(self):
        self.addCleanup(self.cleanup)
        self.config = Config()
        self.config.plugins = PluginManager()
        self.config.plugins.addPlugin(Dataprovider())
        self.argv = []
        # 0 is always should be program name
        self.argv.append("lode_runner")
        self.config.configure(self.argv)
Beispiel #8
0
 def test_config_file_set_by_arg(self):
     c = Config()
     c.configure(['test_config_file_set_by_arg',
                  '-c', self.cfg_file, '-v'])
     # 10 from file, 1 more from cmd line
     self.assertEqual(c.verbosity, 11)
Beispiel #9
0
 def test_load_config_file(self):
     c = Config(files=self.cfg_file)
     c.configure(['test_load_config_file'])
     self.assertEqual(c.verbosity, 10)
Beispiel #10
0
 def test_config_file_set_by_arg(self):
     c = Config()
     c.configure(['test_config_file_set_by_arg',
                  '-c', self.cfg_file, '-v'])
     # 10 from file, 1 more from cmd line
     self.assertEqual(c.verbosity, 11)
Beispiel #11
0
 def test_load_config_file(self):
     c = Config(files=self.cfg_file)
     c.configure(['test_load_config_file'])
     self.assertEqual(c.verbosity, 10)
Beispiel #12
0
    def run():
        import argparse

        parser = argparse.ArgumentParser(description='Run TestBenches.')
        parser.add_argument('--max_configs', type=int)
        parser.add_argument('--run_desert', action='store_true')
        parser.add_argument('model_file')
        parser.add_argument('nose_options', nargs=argparse.REMAINDER)
        command_line_args = parser.parse_args()

        project = Dispatch("Mga.MgaProject")
        mga_file = command_line_args.model_file
        if mga_file.endswith('.xme'):
            project = Dispatch("Mga.MgaProject")
            parser = Dispatch("Mga.MgaParser")
            resolver = Dispatch("Mga.MgaResolver")
            resolver.IsInteractive = False
            parser.Resolver = resolver
            mga_file = os.path.splitext(
                command_line_args.model_file)[0] + ".mga"
            project.Create("MGA=" + os.path.abspath(mga_file), "CyPhyML")
            parser.ParseProject(project, command_line_args.model_file)
        else:
            # n.b. without abspath, things break (e.g. CyPhy2CAD)
            project.OpenEx(
                "MGA=" + os.path.abspath(command_line_args.model_file),
                "CyPhyML", None)

        project.BeginTransactionInNewTerr()
        try:
            if command_line_args.run_desert:
                desert = Dispatch("MGA.Interpreter.DesignSpaceHelper")
                desert.Initialize(project)
                filter = project.CreateFilter()
                filter.Kind = "DesignContainer"
                # FIXME wont work right for TBs that point to non-root design sace
                designContainers = [
                    tb for tb in project.AllFCOs(filter)
                    if not tb.IsLibObject and tb.ParentFolder is not None
                ]
                for designContainer in designContainers:
                    desert.InvokeEx(project, designContainer,
                                    Dispatch("MGA.MgaFCOs"), 128)

            def add_fail(masterContext, configName, message):
                def fail(self):
                    raise ValueError(message)

                fail.__name__ = str('test_' + masterContext.Name + "__" +
                                    configName)
                setattr(TestBenchTest, fail.__name__, fail)

            def add_test(masterContext, mi_config, config):
                def testTestBench(self):
                    self._testTestBench(masterContext, master, mi_config)

                # testTestBench.__name__ = str('test_' + masterContext.Name + "_" + masterContext.ID + "__" + config.Name)
                testTestBench.__name__ = str('test_' + masterContext.Name +
                                             "__" + config.Name)
                setattr(TestBenchTest, testTestBench.__name__, testTestBench)

            master = Dispatch(
                "CyPhyMasterInterpreter.CyPhyMasterInterpreterAPI")
            master.Initialize(project)

            modelKinds = set(
                ("TestBench", "CADTestBench", "KinematicTestBench",
                 "BlastTestBench", "BallisticTestBench", "CarTestBench",
                 "CFDTestBench", "ParametricExploration"))
            # masterContexts = [tb for tb in itertools.chain(*(project.AllFCOs(filter) for filter in filters)) if not tb.IsLibObject]
            masterContexts = list(
                crawlForKinds(project.RootFolder,
                              ("ParametricExplorationFolder", "Testing"),
                              modelKinds))
            print repr([t.Name for t in masterContexts])
            for masterContext in masterContexts:
                configs = None
                if masterContext.Meta.Name == "ParametricExploration":
                    tbs = [
                        tb for tb in masterContext.ChildFCOs
                        if tb.MetaBase.Name == 'TestBenchRef'
                        and tb.Referred is not None
                    ]
                    if not tbs:
                        configs = [masterContext]
                    else:
                        testBench = tbs[0].Referred
                else:
                    testBench = masterContext

                if not configs:
                    suts = [
                        sut for sut in testBench.ChildFCOs
                        if sut.MetaRole.Name == 'TopLevelSystemUnderTest'
                    ]
                    if len(suts) == 0:
                        add_fail(
                            masterContext, 'invalid',
                            'Error: TestBench "{}" has no TopLevelSystemUnderTest'
                            .format(testBench.Name))
                        continue
                    if len(suts) > 1:
                        add_fail(
                            masterContext, 'invalid',
                            'Error: TestBench "{}" has more than one TopLevelSystemUnderTest'
                            .format(testBench.Name))
                        continue
                    sut = suts[0]
                    if sut.Referred.MetaBase.Name == 'ComponentAssembly':
                        configs = [sut.Referred]
                    else:
                        configurations = [
                            config for config in sut.Referred.ChildFCOs
                            if config.MetaBase.Name == 'Configurations'
                        ]
                        if not configurations:
                            add_fail(
                                masterContext, 'invalid',
                                'Error: design has no Configurations models. Try using the --run_desert option'
                            )
                            continue
                        configurations = configurations[0]
                        cwcs = [
                            cwc for cwc in configurations.ChildFCOs
                            if cwc.MetaBase.Name == 'CWC' and cwc.Name
                        ]
                        if not cwcs:
                            raise ValueError(
                                'Error: could not find CWCs for "{}"'.format(
                                    testBench.Name))
                        configs = list(cwcs)
                        # FIXME cfg2 > cfg10
                        configs.sort(key=operator.attrgetter('Name'))
                        configs = configs[slice(0,
                                                command_line_args.max_configs)]

                for config in configs:
                    mi_config = Dispatch(
                        "CyPhyMasterInterpreter.ConfigurationSelectionLight")

                    # GME id, or guid, or abs path or path to Test bench or SoT or PET
                    mi_config.ContextId = masterContext.ID

                    mi_config.SetSelectedConfigurationIds([config.ID])

                    # mi_config.KeepTemporaryModels = True
                    mi_config.PostToJobManager = False

                    add_test(masterContext, mi_config, config)
        finally:
            project.CommitTransaction()

        config = Config(files=all_config_files(),
                        plugins=BuiltinPluginManager())
        config.configure(argv=['nose'] + command_line_args.nose_options)
        loader = TestLoader(config=config)

        tests = [loader.loadTestsFromTestClass(TestBenchTest)]

        try:
            nose.core.TestProgram(suite=tests,
                                  argv=['nose'] +
                                  command_line_args.nose_options,
                                  exit=True,
                                  testLoader=loader,
                                  config=config)
        finally:
            # project.Save(project.ProjectConnStr + "_debug.mga", True)
            project.Close(True)