Example #1
0
    def test_ancestry(self):
        top = imp.new_module('top')
        top.bot = imp.new_module('top.bot')
        top.bot.end = imp.new_module('top.bot.end')
        
        sys.modules['top'] = top
        sys.modules['top.bot'] = top.bot
        sys.modules['top.bot.end'] = top.bot.end
        
        class P:
            pass
        top.bot.P = P
        P.__module__ = 'top.bot'

        csf = ContextSuiteFactory()
        P_ancestors = list([a for a in csf.ancestry(P)])
        self.assertEqual(P_ancestors, [top.bot, top])

        end_ancestors = list([a for a in csf.ancestry(top.bot.end)])
        self.assertEqual(end_ancestors, [top.bot, top])

        bot_ancestors = list([a for a in csf.ancestry(top.bot)])
        self.assertEqual(bot_ancestors, [top])

        top_ancestors = list([a for a in csf.ancestry(top)])
        self.assertEqual(top_ancestors, [])
Example #2
0
    def test_ancestry(self):
        from test_pak.test_sub.test_mod import TestMaths
        from test_pak.test_sub import test_mod
        from test_pak import test_sub
        import test_pak

        factory = ContextSuiteFactory()
        ancestry = [l for l in factory.ancestry(TestMaths)]
        self.assertEqual(ancestry, [test_mod, test_sub, test_pak])
Example #3
0
 def test_ancestry(self):
     from test_pak.test_sub.test_mod import TestMaths
     from test_pak.test_sub import test_mod
     from test_pak import test_sub
     import test_pak
     
     factory = ContextSuiteFactory()
     ancestry = [l for l in factory.ancestry(TestMaths)]
     self.assertEqual(ancestry,
                      [test_mod, test_sub, test_pak])
Example #4
0
 def create_test_suite(self, config, loader):
     """Transforms the plan into a Nose test suite."""
     creator = TestSuiteCreator(loader)
     if dependencies.use_nose:
         from nose.suite import ContextSuiteFactory
         suite = ContextSuiteFactory(config)([])
     else:
         suite = unittest.TestSuite()
     for case in self.tests:
         if case.entry.info.enabled and case.entry.home is not None:
             tests = creator.loadTestsFromTestEntry(case)
             for test in tests:
                 suite.addTest(test)
Example #5
0
 def create_test_suite(self, config, loader):
     """Transforms the plan into a Nose test suite."""
     creator = TestSuiteCreator(loader)
     if dependencies.use_nose:
         from nose.suite import ContextSuiteFactory
         suite = ContextSuiteFactory(config)([])
     else:
         suite = unittest.TestSuite()
     for case in self.tests:
         if case.entry.info.enabled and case.entry.home is not None:
             tests = creator.loadTestsFromTestEntry(case)
             for test in tests:
                 suite.addTest(test)
     return suite
Example #6
0
def rebuild_context_suite(tests):
    "Construct a tree of ContextSuites from the tests-with-contexts passed in."
    factory = ContextSuiteFactory()
    all_ancestors = set()
    for test in tests:
        all_ancestors.update(factory.ancestry(test.context))

    root = Node(None)
    for ancestor in sorted(all_ancestors, key=full_path):
        root.add_context(get_path(ancestor), ancestor)

    for test in tests:
        node = root.add_context(get_path(test.context), test.context)
        node.add_test(test)

    return root.to_context_suite()
Example #7
0
def rebuild_context_suite(tests):
    "Construct a tree of ContextSuites from the tests-with-contexts passed in."
    factory = ContextSuiteFactory()
    all_ancestors = set()
    for test in tests:
        all_ancestors.update(factory.ancestry(test.context))

    root = Node(None)
    for ancestor in sorted(all_ancestors, key=full_path):
        root.add_context(get_path(ancestor), ancestor)

    for test in tests:
        node = root.add_context(get_path(test.context), test.context)
        node.add_test(test)

    return root.to_context_suite()
    def test_context_fixtures_for_ancestors(self):
        top = imp.new_module('top')
        top.bot = imp.new_module('top.bot')
        top.bot.end = imp.new_module('top.bot.end')

        sys.modules['top'] = top
        sys.modules['top.bot'] = top.bot
        sys.modules['top.bot.end'] = top.bot.end

        class TC(unittest.TestCase):
            def runTest(self):
                pass

        top.bot.TC = TC
        TC.__module__ = 'top.bot'

        # suite with just TC test
        # this suite should call top and top.bot setup
        csf = ContextSuiteFactory()
        suite = csf(ContextList([TC()], context=top.bot))

        suite.setUp()
        assert top in csf.was_setup, "Ancestor not set up"
        assert top.bot in csf.was_setup, "Context not set up"
        suite.has_run = True
        suite.tearDown()
        assert top in csf.was_torndown, "Ancestor not torn down"
        assert top.bot in csf.was_torndown, "Context not torn down"

        # wrapped suites
        # the outer suite sets up its context, the inner
        # its context only, without re-setting up the outer context
        csf = ContextSuiteFactory()
        inner_suite = csf(ContextList([TC()], context=top.bot))
        suite = csf(ContextList(inner_suite, context=top))

        suite.setUp()
        assert top in csf.was_setup
        assert not top.bot in csf.was_setup
        inner_suite.setUp()
        assert top in csf.was_setup
        assert top.bot in csf.was_setup
        assert csf.was_setup[top] is suite
        assert csf.was_setup[top.bot] is inner_suite
Example #9
0
    def test_find_context(self):
        from test_pak import test_mod

        factory = ContextSuiteFactory()
        tests = [
            case.FunctionTestCase(test_mod.test_add),
            case.FunctionTestCase(test_mod.test_minus)
        ]
        suite = factory(tests)
        self.assertEqual(suite.context, test_mod)
    def test_ancestry(self):
        top = imp.new_module('top')
        top.bot = imp.new_module('top.bot')
        top.bot.end = imp.new_module('top.bot.end')

        sys.modules['top'] = top
        sys.modules['top.bot'] = top.bot
        sys.modules['top.bot.end'] = top.bot.end

        class P:
            pass

        top.bot.P = P
        P.__module__ = 'top.bot'

        csf = ContextSuiteFactory()
        P_ancestors = list([a for a in csf.ancestry(P)])
        self.assertEqual(P_ancestors, [top.bot, top])

        end_ancestors = list([a for a in csf.ancestry(top.bot.end)])
        self.assertEqual(end_ancestors, [top.bot, top])

        bot_ancestors = list([a for a in csf.ancestry(top.bot)])
        self.assertEqual(bot_ancestors, [top])

        top_ancestors = list([a for a in csf.ancestry(top)])
        self.assertEqual(top_ancestors, [])
Example #11
0
    def __init__(self,
                 config=None,
                 importer=None,
                 workingDir=None,
                 selector=None):
        """Initialize a test loader.

        Parameters (all optional):

        * config: provide a `nose.config.Config`_ or other config class
          instance; if not provided a `nose.config.Config`_ with
          default values is used.
        * importer: provide an importer instance that implements
          `importFromPath`. If not provided, a
          `nose.importer.Importer`_ is used.
        * workingDir: the directory to which file and module names are
          relative. If not provided, assumed to be the current working
          directory.
        * selector: a selector class or instance. If a class is
          provided, it will be instantiated with one argument, the
          current config. If not provided, a `nose.selector.Selector`_
          is used.
        """
        if config is None:
            config = Config()
        if importer is None:
            importer = Importer(config=config)
        if workingDir is None:
            workingDir = config.workingDir
        if selector is None:
            selector = defaultSelector(config)
        elif isclass(selector):
            selector = selector(config)
        self.config = config
        self.importer = importer
        self.workingDir = op_normpath(op_abspath(workingDir))
        self.selector = selector
        if config.addPaths:
            add_path(workingDir, config)
        self.suiteClass = ContextSuiteFactory(config=config)

        self._visitedPaths = set([])

        unittest.TestLoader.__init__(self)
Example #12
0
def get_tests_from_xml_files(paths, config):
    '''Retrieve the suite of tests from an xml file produced by the xunit plugin
    
    Start cpnose like this to make the file:
    
    python cpnose.py --collect-only --with-xunit --xunit-file=<path>
    
    paths - paths to xml files
    
    returns a test suite with the paths
    '''
    factory = ContextSuiteFactory(config=config)
    hierarchy = {}
    classes = {}
    for path in paths:
        xml = parse(path)
        for test_suite in xml.getElementsByTagName("testsuite"):
            for test_case in test_suite.getElementsByTagName("testcase"):
                full_class_name = test_case.getAttribute("classname")
                name = test_case.getAttribute("name")
                parts = full_class_name.split(".")
                leaf = hierarchy
                for part in parts:
                    if part not in leaf:
                        leaf[part] = {}
                    leaf = leaf[part]
                module_name, class_name = full_class_name.rsplit(".", 1)
                try:
                    if full_class_name not in classes:
                        klass = resolve_name(full_class_name)
                        classes[full_class_name] = klass
                    else:
                        klass = classes[full_class_name]
                    if klass is None:
                        continue
                    test = klass(name)
                    leaf[name] = test
                except:
                    logger.warning("Failed to load test %s.%s" %
                                   (full_class_name, name),
                                   exc_info=True)
    return get_suite_from_dictionary(factory, hierarchy)
Example #13
0
 def __init__(self, *args, **kwargs):
     super(TestLoader, self).__init__(*args, **kwargs)
     self.suiteClass = ContextSuiteFactory(
         self.config, resultProxy=ResultProxyFactory(config=self.config))