예제 #1
0
    def autotest(self, line):
        """Run the unit tests inline

        Returns the TestResult object. Notable fields in the result object:

        * `result.results` is a summary dictionary of
          outcomes, with keys named TestClass.test_case
          and boolean values (True: test passed, False: test failed
          or an error occurred).
          TODO(salikh): Decide whether to remove or document ad-hoc
          entries in the outcome map:
          * 'test_file.py' (set to False if the tests has any issues,
            or set to True if all test cases passed)

        * `result.errors`, `result.failures`, `result.skipped` and other
          fields are computed as documented in unittest.TestResult.
        """

        test_case = self.shell.ev(line)
        if isinstance(test_case, type) and issubclass(test_case,
                                                      unittest.TestCase):
            suite = unittest.TestLoader().loadTestsFromTestCase(test_case)
            errors = io.StringIO()
            result = unittest.TextTestRunner(
                verbosity=4,
                stream=errors,
                resultclass=summary_test_result.SummaryTestResult).run(suite)
            return result, errors.getvalue()
        elif (isinstance(test_case, types.SimpleNamespace)
              and test_case.type == 'inlinetest'):
            errorMessage = None
            with CaptureOutput() as (out, err):
                try:
                    env = {}
                    # Assume the context has already been
                    # set up in user_ns.
                    # Exercise the code under test.
                    exec(self.shell.user_ns['submission_source'].source,
                         self.shell.user_ns, env)
                    # Exercise the inline test.
                    exec(test_case.source, self.shell.user_ns, env)
                except AssertionError as e:
                    errorMessage = e
                except Exception as e:
                    errorMessage = e
            stream = out.getvalue()
            if len(err.getvalue()) > 0:
                stream += "\nERROR:\n" + err.getvalue()
            result = summary_test_result.SummaryTestResult(
                stream=stream, descriptions=test_case.name, verbosity=1)
            if errorMessage is not None:
                result.results['error'] = errorMessage
                result.results['passed'] = False
            else:
                result.results['passed'] = True
            return result, stream
        else:
            raise Exception("Unrecognized autotest argument of class %s" %
                            (test_case.__class__))
예제 #2
0
def autotest(test_case):
    """Runs one unit test and returns the test result.

    This is a non-magic version of the %autotest.
    This function can be used as

        result, log = autotest(MyTestCase)

    Args:
    * test_case: The name of the unit test class (extends unittest.TestCase),
                 or the name of the inline test namespace, defined with
                 %%inlinetest or %%studenttest.

    Returns: A 2-tuple of a SummaryTestResult objct and a string holding verbose
    test logs.
    """
    if isinstance(test_case, type) and issubclass(test_case,
                                                  unittest.TestCase):
        suite = unittest.TestLoader().loadTestsFromTestCase(test_case)
        errors = io.StringIO()
        result = unittest.TextTestRunner(
            verbosity=4,
            stream=errors,
            resultclass=summary_test_result.SummaryTestResult).run(suite)
        return result, errors.getvalue()
    elif (isinstance(test_case, types.SimpleNamespace)
          and test_case.type == 'inlinetest'):
        errorMessage = None
        with CaptureOutput() as (out, err):
            try:
                env = {}
                # Assume the context has already been
                # set up in user_ns.
                # Exercise the code under test.
                exec(globals()['submission_source'].source, globals(), env)  # pylint: disable=W0122
                # Exercise the inline test.
                exec(test_case.source, globals(), env)  # pylint: disable=W0122
            except AssertionError as e:
                errorMessage = e
            except Exception as e:
                errorMessage = e
        stream = out.getvalue()
        if len(err.getvalue()) > 0:
            stream += "\nERROR:\n" + err.getvalue()
        result = summary_test_result.SummaryTestResult(
            stream=stream, descriptions=test_case.name, verbosity=1)
        if errorMessage is not None:
            result.results['error'] = errorMessage
            result.results['passed'] = False
        else:
            result.results['passed'] = True
        return result, stream
    else:
        raise Exception("Unrecognized autotest argument of class %s" %
                        (test_case.__class__))