Пример #1
0
def dorun():
    '''
    Handle the `run` command.
    '''
    loader = load_tests()

    if config.tags:
        suites = []
        for tag in config.tags:
            suites.extend(loader.suites_with_tag(tag))
    else:
        suites = loader.suites

    # Create directory to save junit and internal results in.
    mkdir_p(config.result_path)

    with open(joinpath(config.result_path, 'pickle'), 'w') as result_file,\
         open(joinpath(config.result_path, 'junit.xml'), 'w') as junit_f:

        junit_logger = result.JUnitLogger(junit_f, result_file)
        console_logger = result.ConsoleLogger()
        loggers = (junit_logger, console_logger)

        log.display(separator())
        log.bold('Running Tests')
        log.display('')
        if config.uid:
            test_item = loader.get_uid(config.uid)
            results = Runner.run_items(test_item)
        else:
            testrunner = Runner(suites, loggers)
            results = testrunner.run()
Пример #2
0
def list_tests_with_tags(loader, tags):
    log.display('Listing tests based on tags.')
    for tag in tags:
        log.display(separator())
        log.display("Tests marked with tag '%s':" % tag)
        log.display(separator())
        for test in loader.tag_index(tag):
            log.display(test.uid)
Пример #3
0
def load_tests():
    '''
    Create a TestLoader and load tests for the directory given by the config.
    '''
    testloader = TestLoader()
    log.display(separator())
    log.bold('Loading Tests')
    log.display('')
    testloader.load_root(config.directory)
    return testloader
Пример #4
0
def list_fixtures(loader):
    log.display(separator())
    log.display('Listing all Fixtures.')
    log.display(separator())
    for fixture in loader.fixtures:
        log.display(fixture.name)
Пример #5
0
def list_tags(loader):
    log.display(separator())
    log.display('Listing all Tags.')
    log.display(separator())
    for tag in loader.tags:
        log.display(tag)
Пример #6
0
def list_suites(loader):
    log.display(separator())
    log.display('Listing all TestSuites.')
    log.display(separator())
    for suite in loader.suites:
        log.display(suite.uid)
Пример #7
0
def list_tests(loader):
    log.display(separator())
    log.display('Listing all TestCases.')
    log.display(separator())
    for test in loader.tests:
        log.display(test.uid)
Пример #8
0
 def end_testing(self):
     if self._started:
         self.timer.stop()
         log.display(self._display_summary())
         self._started = False
Пример #9
0
 def _skip_testcase(self, test_case, reason):
     log.display('{color}Skipping: {name}{reset}'.format(
         color=self.colormap[Outcome.SKIP],
         name=test_case.name,
         reset=self.reset))
     self.outcome_count[Outcome.SKIP] += 1
Пример #10
0
    def load_file(self, path, collection=None):
        '''
        Load the given path for tests collecting all created instances of
        :class:`TestSuite`, :class:`TestCase`, and :code:`Fixture` objects.

        TestSuites are placed into self._suites (a private SuiteList).
        TestCases which are not explicitly stored within a TestSuite are
        placed into a TestSuite which is generated from the filepath name.

        There are couple of things that might be unexpected to the user in the
        loading namespace.

        First, the current working directory will change to the directory path
        of the file being loaded.

        Second, in order to collect tests we wrap __new__ calls of collected
        objects before calling :code:`execfile`.  If a user wishes to prevent
        an instantiated object from being collected (possibly to create a copy
        with a modified attribute) they should use :func:`no_collect` to do
        so. (Internally a :code:`__no_collect__` method is added to objects we
        plan to collect. This method will then remove the object from those
        collected from the file.)

        .. note:: Automatically drop_caches

        .. warning:: There isn't a way to prevent reloading of test modules
            that are imported by other test modules. It's up to users to never
            import a test module from a test module, otherwise those tests
            enumerated during the importer module's load.
        '''
        path = os.path.abspath(path)
        if __debug__:
            self._loaded_a_file = True

        # Remove our cache of tagged test items since they may be modified.
        self.drop_caches()

        if collection is None:
            collection = self._suites

        # Create a custom dictionary for the loaded module.
        newdict = {
            '__builtins__': __builtins__,
            '__name__': path_as_modulename(path),
            '__file__': path,
            '__directory__': os.path.dirname(path),
        }

        self._wrap_collection(TestSuite, self._collected_test_items)
        self._wrap_collection(TestCase, self._collected_test_items)
        self._wrap_collection(Fixture, self._collected_fixtures)

        # Add the file's containing directory to the system path. So it can do
        # relative imports naturally.
        sys.path.insert(0, os.path.dirname(path))
        cwd = os.getcwd()
        os.chdir(os.path.dirname(path))

        def cleanup():
            # Undo all the wrapping of class methods used to gather test
            # items. (Placed here to compare against the above stuff it
            # undoes.)
            self._unwrap_collection(TestSuite)
            self._unwrap_collection(TestCase)
            self._unwrap_collection(Fixture)
            self._collected_fixtures = OrderedSet()
            self._collected_test_items = OrderedSet()
            del sys.path[0]
            os.chdir(cwd)

        try:
            execfile(path, newdict, newdict)
        except Exception as e:
            log.warn('Tried to load tests from %s but failed with an'
                     ' exception.' % path)
            log.debug(traceback.format_exc())
            cleanup()
            return

        # Separate the instances so we can manipulate them more easily.
        # We also keep them together so we maintain ordering.
        test_items = self._collected_test_items
        testcases = OrderedSet()
        testsuites = []

        for item in test_items:
            if isinstance(item, TestCase):
                testcases.add(item)
            elif isinstance(item, TestSuite):
                testsuites.append(item)

        self._index(*self._collected_test_items)
        self._fixtures.extend(self._collected_fixtures)

        if testcases:
            log.display('Discovered %d tests and %d testsuites in %s'
                        '' % (len(testcases), len(testsuites), path))

            # Remove all tests already contained in a TestSuite.
            if testsuites:
                testcases = OrderedSet(testcases)
                for testsuite in testsuites:
                    testcases -= OrderedSet(testsuite.testcases)

            # If there are any remaining tests, create a TestSuite for the
            # module and place those tests into it.
            if testcases:
                module_testsuite = path_as_testsuite(path)
                testsuites.append(module_testsuite)
                for test_item in test_items:
                    if isinstance(test_item, TestCase):
                        module_testsuite.append(test_item)

                # Add our new testsuite into the index as well
                self._index(module_testsuite)

            collection.extend(testsuites)

        elif testsuites:
            log.warn('No tests discovered in %s, but found %d '
                     ' TestSuites' % (path, len(testsuites)))
        else:
            log.warn('No tests discovered in %s' % path)

        cleanup()