Skip to content

runner

Unit tests facility.

Attributes

mlogger = get_logger(__name__) module-attribute

DEBUG_OKAY_RESULT = 'PASSED' module-attribute

DEBUG_FAIL_RESULT = 'FAILED' module-attribute

RESULT_TEST_SUITE_START = '<div class="unittest unitteststart">Test Suite: {suite}</div>' module-attribute

RESULT_DIV_OKAY = '<div class="unittest unittestokay">:white_heavy_check_mark: PASSED {test}</div>' module-attribute

RESULT_DIV_FAIL = '<div class="unittest unittestfail">:cross_mark: FAILED {test}</div>' module-attribute

RESULT_DIV_ERROR = '<div class="unittest unittesterror">:heavy_large_circle: ERROR {test}</div>' module-attribute

Classes

OutputWriter()

Output writer for tests results.

Source code in pyrevitlib/pyrevit/unittests/runner.py
def __init__(self):
    self._output = get_output()

Functions

write(output_str)

Prints the results to the output window.

Parameters:

Name Type Description Default
output_str str

Text to output

required
Source code in pyrevitlib/pyrevit/unittests/runner.py
def write(self, output_str):
    """Prints the results to the output window.

    Args:
        output_str (str): Text to output
    """
    self._output.print_html(output_str)

PyRevitTestResult(verbosity)

Bases: TestResult

Pyrevit Test Result.

Parameters:

Name Type Description Default
verbosity int

verbosity level.

required
Source code in pyrevitlib/pyrevit/unittests/runner.py
def __init__(self, verbosity):
    super(PyRevitTestResult, self).__init__(verbosity=verbosity)
    self.writer = OutputWriter()

Attributes

writer = OutputWriter() instance-attribute

Functions

getDescription(test) staticmethod

Returns the description of the test.

Parameters:

Name Type Description Default
test TestCase

Unit test.

required

Returns:

Type Description
str

test description

Source code in pyrevitlib/pyrevit/unittests/runner.py
@staticmethod
def getDescription(test):
    """Returns the description of the test.

    Args:
        test (TestCase): Unit test.

    Returns:
        (str): test description
    """
    return test.shortDescription() or test
startTest(test)

Starts the test.

Parameters:

Name Type Description Default
test TestCase

unit test

required
Source code in pyrevitlib/pyrevit/unittests/runner.py
def startTest(self, test):
    """Starts the test.

    Args:
        test (TestCase): unit test
    """
    super(PyRevitTestResult, self).startTest(test)
    mlogger.debug('Running test: %s', self.getDescription(test))
addSuccess(test)

Adds a test success.

Parameters:

Name Type Description Default
test TestCase

unit test case

required
Source code in pyrevitlib/pyrevit/unittests/runner.py
def addSuccess(self, test):
    """Adds a test success.

    Args:
        test (TestCase): unit test case
    """
    super(PyRevitTestResult, self).addSuccess(test)
    mlogger.debug(DEBUG_OKAY_RESULT)
    self.writer.write(RESULT_DIV_OKAY
                      .format(test=self.getDescription(test)))
addError(test, err)

Adds a test error.

Parameters:

Name Type Description Default
test TestCase

unit test case

required
err OptExcInfo

test exception info

required
Source code in pyrevitlib/pyrevit/unittests/runner.py
def addError(self, test, err):
    """Adds a test error.

    Args:
        test (TestCase): unit test case
        err (OptExcInfo): test exception info
    """
    super(PyRevitTestResult, self).addError(test, err)
    mlogger.debug(DEBUG_FAIL_RESULT)
    self.writer.write(RESULT_DIV_ERROR
                      .format(test=self.getDescription(test)))
addFailure(test, err)

Adds a test failure.

Parameters:

Name Type Description Default
test TestCase

unit test case

required
err OptExcInfo

test exception info

required
Source code in pyrevitlib/pyrevit/unittests/runner.py
def addFailure(self, test, err):
    """Adds a test failure.

    Args:
        test (TestCase): unit test case
        err (OptExcInfo): test exception info
    """
    super(PyRevitTestResult, self).addFailure(test, err)
    mlogger.debug(DEBUG_FAIL_RESULT)
    self.writer.write(RESULT_DIV_FAIL
                      .format(test=self.getDescription(test)))

PyRevitTestRunner(verbosity=1, failfast=False, use_buffer=False, resultclass=None)

Bases: object

Test runner.

Parameters:

Name Type Description Default
verbosity int

level of vermosity. Defaults to 1.

1
failfast bool

if True, stops at the first failure. Defaults to False.

False
use_buffer bool

use a buffer. Defaults to False.

False
resultclass type

Class to use to hold the results. Defaults to PyRevitTestResult.

None
Source code in pyrevitlib/pyrevit/unittests/runner.py
def __init__(self, verbosity=1, failfast=False,
             use_buffer=False, resultclass=None):
    self.verbosity = verbosity
    self.failfast = failfast
    self.use_buffer = use_buffer
    if resultclass is not None:
        self.resultclass = resultclass

Attributes

resultclass = PyRevitTestResult class-attribute instance-attribute
verbosity = verbosity instance-attribute
failfast = failfast instance-attribute
use_buffer = use_buffer instance-attribute

Functions

run(test)

Runs a test suite.

Parameters:

Name Type Description Default
test TestSuite

Test suite to run

required

Returns:

Type Description
PyRevitTestResult

Test suite results.

Source code in pyrevitlib/pyrevit/unittests/runner.py
def run(self, test):
    """Runs a test suite.

    Args:
        test (TestSuite): Test suite to run

    Returns:
        (PyRevitTestResult): Test suite results.
    """
    # setup results object
    result = self._make_result()
    result.failfast = self.failfast
    result.buffer = self.use_buffer

    # start clock
    start_time = time.time()

    # find run test methods
    start_test_run = getattr(result, 'startTestRun', None)
    if start_test_run is not None:
        start_test_run()
    try:
        test(result)
    finally:
        stop_test_run = getattr(result, 'stopTestRun', None)
        if stop_test_run is not None:
            stop_test_run()

    # stop clock and calculate run time
    stop_time = time.time()
    time_taken = stop_time - start_time

    # print errots
    result.printErrors()
    test_count = result.testsRun
    mlogger.debug("Ran %d test%s in %.3fs",
                  test_count, test_count != 1 and "s" or "", time_taken)

    expected_fails = unexpected_successes = skipped = 0
    try:
        results = map(len, (result.expectedFailures,
                            result.unexpectedSuccesses,
                            result.skipped))
    except AttributeError:
        pass
    else:
        expected_fails, unexpected_successes, skipped = results

    infos = []
    if not result.wasSuccessful():
        mlogger.debug("FAILED")
        failed, errored = map(len, (result.failures, result.errors))
        if failed:
            infos.append("failures=%d" % failed)
        if errored:
            infos.append("errors=%d" % errored)
    else:
        mlogger.debug(DEBUG_OKAY_RESULT)

    if skipped:
        infos.append("skipped=%d" % skipped)
    if expected_fails:
        infos.append("expected failures=%d" % expected_fails)
    if unexpected_successes:
        infos.append("unexpected successes=%d" % unexpected_successes)
    if infos:
        mlogger.debug(" (%s)", (", ".join(infos),))

    return result

Functions

run_module_tests(test_module)

Runs the unit tests of the given module.

Parameters:

Name Type Description Default
test_module module

module with tests

required

Returns:

Type Description
PyRevitTestResult

tests results.

Source code in pyrevitlib/pyrevit/unittests/runner.py
def run_module_tests(test_module):
    """Runs the unit tests of the given module.

    Args:
        test_module (module): module with tests

    Returns:
        (PyRevitTestResult): tests results.
    """
    test_runner = PyRevitTestRunner()
    test_loader = TestLoader()
    # load all testcases from the given module into a testsuite
    test_suite = test_loader.loadTestsFromModule(test_module)
    # run the test suite
    mlogger.debug('Running test suite for module: %s', test_module)
    OutputWriter()\
        .write(RESULT_TEST_SUITE_START.format(suite=test_module.__name__))
    return test_runner.run(test_suite)

run_test_case(test_case)

Runs the unit test of the given TestCase class.

Parameters:

Name Type Description Default
test_case type[TestCase]

TestCase class with tests

required

Returns:

Type Description
PyRevitTestResult

tests results.

Source code in pyrevitlib/pyrevit/unittests/runner.py
def run_test_case(test_case):
    """Runs the unit test of the given TestCase class.

    Args:
        test_case (type[TestCase]): TestCase class with tests

    Returns:
        (PyRevitTestResult): tests results.
    """
    test_runner = PyRevitTestRunner()
    suite = TestLoader().loadTestsFromTestCase(test_case)
    OutputWriter()\
        .write(RESULT_TEST_SUITE_START.format(suite=suite.__class__.__name__))
    return test_runner.run(suite)