示例#1
0
    def collect(self) -> Iterable[Union[Item, Collector]]:
        from unittest import TestLoader

        cls = self.obj
        if not getattr(cls, "__test__", True):
            return

        skipped = _is_skipped(cls)
        if not skipped:
            self._inject_setup_teardown_fixtures(cls)
            self._inject_setup_class_fixture()

        self.session._fixturemanager.parsefactories(self, unittest=True)
        loader = TestLoader()
        foundsomething = False
        for name in loader.getTestCaseNames(self.obj):
            x = getattr(self.obj, name)
            if not getattr(x, "__test__", True):
                continue
            funcobj = getimfunc(x)
            yield TestCaseFunction.from_parent(self,
                                               name=name,
                                               callobj=funcobj)
            foundsomething = True

        if not foundsomething:
            runtest = getattr(self.obj, "runTest", None)
            if runtest is not None:
                ut = sys.modules.get("twisted.trial.unittest", None)
                # Type ignored because `ut` is an opaque module.
                if ut is None or runtest != ut.TestCase.runTest:  # type: ignore
                    yield TestCaseFunction.from_parent(self, name="runTest")
示例#2
0
文件: unittest.py 项目: diop/kebetu
    def collect(self):
        from unittest import TestLoader

        cls = self.obj
        if not getattr(cls, "__test__", True):
            return

        skipped = getattr(cls, "__unittest_skip__", False)
        if not skipped:
            self._inject_setup_teardown_fixtures(cls)
            self._inject_setup_class_fixture()

        self.session._fixturemanager.parsefactories(self, unittest=True)
        loader = TestLoader()
        foundsomething = False
        for name in loader.getTestCaseNames(self.obj):
            x = getattr(self.obj, name)
            if not getattr(x, "__test__", True):
                continue
            funcobj = getimfunc(x)
            yield TestCaseFunction(name, parent=self, callobj=funcobj)
            foundsomething = True

        if not foundsomething:
            runtest = getattr(self.obj, "runTest", None)
            if runtest is not None:
                ut = sys.modules.get("twisted.trial.unittest", None)
                if ut is None or runtest != ut.TestCase.runTest:
                    yield TestCaseFunction("runTest", parent=self)
示例#3
0
    def collect(self):
        from unittest import TestLoader

        cls = self.obj
        if not getattr(cls, "__test__", True):
            return
        self.session._fixturemanager.parsefactories(self, unittest=True)
        loader = TestLoader()
        module = self.getparent(Module).obj
        foundsomething = False
        for name in loader.getTestCaseNames(self.obj):
            x = getattr(self.obj, name)
            if not getattr(x, "__test__", True):
                continue
            funcobj = getimfunc(x)
            transfer_markers(funcobj, cls, module)
            yield TestCaseFunction(name, parent=self, callobj=funcobj)
            foundsomething = True

        if not foundsomething:
            runtest = getattr(self.obj, "runTest", None)
            if runtest is not None:
                ut = sys.modules.get("twisted.trial.unittest", None)
                if ut is None or runtest != ut.TestCase.runTest:
                    yield TestCaseFunction("runTest", parent=self)
    def build_suite(self, test_labels, extra_tests=None, **kwargs):
        '''
        Override the base class method to return a suite consisting of all
        TestCase subclasses throughought the whole project.
        '''
        if test_labels:
            suite = TestSuite()
        else:
            suite = DjangoTestSuiteRunner.build_suite(
                self, test_labels, extra_tests, **kwargs
            )
        added_test_classes = set(t.__class__ for t in suite)

        loader = TestLoader()
        for fname in _get_module_names(os.getcwd()):
            module = _import(_to_importable_name(fname))
            for test_class in _get_testcases(module):

                if test_class in added_test_classes:
                    continue

                for method_name in loader.getTestCaseNames(test_class):
                    testname = '.'.join([
                        module.__name__, test_class.__name__, method_name
                    ])
                    if self._test_matches(testname, test_labels):
                        suite.addTest(loader.loadTestsFromName(testname))
                        added_test_classes.add(test_class)

        return reorder_suite(suite, (TestCase,))
示例#5
0
    def collect(self):
        from unittest import TestLoader

        cls = self.obj
        if not getattr(cls, "__test__", True):
            return
        self.session._fixturemanager.parsefactories(self, unittest=True)
        loader = TestLoader()
        module = self.getparent(Module).obj
        foundsomething = False
        for name in loader.getTestCaseNames(self.obj):
            x = getattr(self.obj, name)
            if not getattr(x, "__test__", True):
                continue
            funcobj = getattr(x, "im_func", x)
            transfer_markers(funcobj, cls, module)
            yield TestCaseFunction(name, parent=self, callobj=funcobj)
            foundsomething = True

        if not foundsomething:
            runtest = getattr(self.obj, "runTest", None)
            if runtest is not None:
                ut = sys.modules.get("twisted.trial.unittest", None)
                if ut is None or runtest != ut.TestCase.runTest:
                    yield TestCaseFunction("runTest", parent=self)
示例#6
0
    def collect(self):
        from unittest import TestLoader

        cls = self.obj
        if not getattr(cls, "__test__", True):
            return

        skipped = getattr(cls, "__unittest_skip__", False)
        if not skipped:
            self._inject_setup_teardown_fixtures(cls)
            self._inject_setup_class_fixture()

        self.session._fixturemanager.parsefactories(self, unittest=True)
        loader = TestLoader()
        foundsomething = False
        for name in loader.getTestCaseNames(self.obj):
            x = getattr(self.obj, name)
            if not getattr(x, "__test__", True):
                continue
            funcobj = getimfunc(x)
            yield TestCaseFunction(name, parent=self, callobj=funcobj)
            foundsomething = True

        if not foundsomething:
            runtest = getattr(self.obj, "runTest", None)
            if runtest is not None:
                ut = sys.modules.get("twisted.trial.unittest", None)
                if ut is None or runtest != ut.TestCase.runTest:
                    yield TestCaseFunction("runTest", parent=self)
示例#7
0
def load_tests(loader: unittest.TestLoader, tests, pattern):
    suite = unittest.TestSuite()
    test_names = loader.getTestCaseNames(ShutdownTest)
    if os.environ.get('YCM_VALGRIND_RUN'):

        def allowed_tests(name: str):
            return 'WithoutSubserver' in name
    else:

        def allowed_tests(name: str):
            return True

    tests = loader.loadTestsFromNames(filter(allowed_tests, test_names),
                                      ShutdownTest)
    suite.addTests(tests)
    return suite
示例#8
0
 def parametrize(testcase_class, users):
     """ Create a suite containing all tests taken from the given
         subclass, passing them the parameter 'client'.
     """
     
     testloader = TestLoader()
     testnames = testloader.getTestCaseNames(testcase_class)
     tests = []
     for name in testnames:
         # Not logged in
         client = Client()
         tests.append(testcase_class(name, client=client))
         
         # Log in users
         for user in users:
             client = Client()
             tests.append(testcase_class(name, client=client, username=user['username'], password=user['password']))
     return tests
示例#9
0
    def parametrize(testcase_class, users):
        """ Create a suite containing all tests taken from the given
            subclass, passing them the parameter 'client'.
        """

        testloader = TestLoader()
        testnames = testloader.getTestCaseNames(testcase_class)
        tests = []
        for name in testnames:
            # Not logged in
            client = Client()
            tests.append(testcase_class(name, client=client))

            # Log in users
            for user in users:
                client = Client()
                tests.append(
                    testcase_class(name,
                                   client=client,
                                   username=user['username'],
                                   password=user['password']))
        return tests
示例#10
0
    def find_tests_and_apps(self, label):
        """Construct a test suite of all test methods with the specified name.
        Returns an instantiated test suite corresponding to the label provided.
        """

        tests = []
        from unittest import TestLoader

        loader = TestLoader()

        from django.db.models import get_app, get_apps

        for app_models_module in get_apps():
            app_name = app_models_module.__name__.rpartition(".")[0]
            if app_name == label:
                from django.test.simple import build_suite

                tests.append(build_suite(app_models_module))

            from django.test.simple import get_tests

            app_tests_module = get_tests(app_models_module)

            for sub_module in [m for m in app_models_module, app_tests_module if m is not None]:

                # print "Checking for %s in %s" % (label, sub_module)

                for name in dir(sub_module):
                    obj = getattr(sub_module, name)
                    import types

                    if isinstance(obj, (type, types.ClassType)) and issubclass(obj, unittest.TestCase):

                        test_names = loader.getTestCaseNames(obj)
                        # print "Checking for %s in %s.%s" % (label, obj, test_names)
                        if label in test_names:
                            tests.append(loader.loadTestsFromName(label, obj))

                try:
                    module = sub_module
                    from django.test import _doctest as doctest
                    from django.test.testcases import DocTestRunner

                    doctests = doctest.DocTestSuite(module, checker=self.doctestOutputChecker, runner=DocTestRunner)
                    # Now iterate over the suite, looking for doctests whose name
                    # matches the pattern that was given
                    for test in doctests:
                        if test._dt_test.name in (
                            "%s.%s" % (module.__name__, ".".join(parts[1:])),
                            "%s.__test__.%s" % (module.__name__, ".".join(parts[1:])),
                        ):
                            tests.append(test)
                except TypeError as e:
                    raise Exception("%s appears not to be a module: %s" % (module, e))
                except ValueError:
                    # No doctests found.
                    pass

        # If no tests were found, then we were given a bad test label.
        if not tests:
            raise ValueError(("Test label '%s' does not refer to a " + "test method or app") % label)

        # Construct a suite out of the tests that matched.
        return unittest.TestSuite(tests)
示例#11
0
def initTest(klass, gn2_url, es_url):
    loader = TestLoader()
    methodNames = loader.getTestCaseNames(klass)
    return [klass(mname, gn2_url, es_url) for mname in methodNames]
示例#12
0
         # Dynamically import test module
         module = __import__(_UNIT_TEST_PKG.format(test_name))
         test_module = getattr(module, test_name)
         # Iterate over the list of attributes for test module to find valid
         # TestCase objects.
         for name in dir(test_module):
             # Process object if subclass of TestCase.
             obj = getattr(test_module, name)
             if isinstance(obj, type) and issubclass(obj, TestCase):
                 # Check if object is subclass tests.utils.TestCase to pass
                 # required arguments.
                 use_args = True \
                     if issubclass(obj, unit_tests.utils.GadgetsTestCase) \
                     else False
                 # Get test names (methods).
                 testnames = testloader.getTestCaseNames(obj)
                 for testname in testnames:
                     if use_args:
                         # Add test with specific arguments.
                         test_instance = obj(testname, options=test_options)
                         suite.addTest(test_instance)
                         # Store max number of servers required by test.
                         num_servers = test_instance.num_servers_required
                         if num_servers > num_srv_needed:
                             num_srv_needed = num_servers
                     else:
                         # Add test without arguments (default).
                         suite.addTest(obj(testname))
 except Exception as err:  # pylint: disable=W0703
     sys.stderr.write("ERROR: Could not load tests to run: {0}"
                      "\n".format(err))
示例#13
0
def initTest(klass, gn2_url, es_url):
    loader = TestLoader()
    methodNames = loader.getTestCaseNames(klass)
    return [klass(mname, gn2_url, es_url) for mname in methodNames]
示例#14
0
         # Dynamically import test module
         module = __import__(_UNIT_TEST_PKG.format(test_name))
         test_module = getattr(module, test_name)
         # Iterate over the list of attributes for test module to find valid
         # TestCase objects.
         for name in dir(test_module):
             # Process object if subclass of TestCase.
             obj = getattr(test_module, name)
             if isinstance(obj, type) and issubclass(obj, TestCase):
                 # Check if object is subclass tests.utils.TestCase to pass
                 # required arguments.
                 use_args = True \
                     if issubclass(obj, unit_tests.utils.GadgetsTestCase) \
                     else False
                 # Get test names (methods).
                 testnames = testloader.getTestCaseNames(obj)
                 for testname in testnames:
                     if use_args:
                         # Add test with specific arguments.
                         test_instance = obj(testname, options=test_options)
                         suite.addTest(test_instance)
                         # Store max number of servers required by test.
                         num_servers = test_instance.num_servers_required
                         if num_servers > num_srv_needed:
                             num_srv_needed = num_servers
                     else:
                         # Add test without arguments (default).
                         suite.addTest(obj(testname))
 except Exception as err:  # pylint: disable=W0703
     sys.stderr.write("ERROR: Could not load tests to run: {0}"
                      "\n".format(err))
示例#15
0
	def __init__(self, test_case_class):
		my_load = TestLoader()
		self.methods = my_load.getTestCaseNames(test_case_class)