officescripts-unit-testing-framework
    Preparing search index...

    Class TestRunner

    A utility class for managing and running test cases with controlled console output. TestRunner supports configurable verbosity levels and allows structured logging with indentation for better test output organization. It is primarily designed for test cases using assertion methods (e.g., Assert.equals, Assert.throws). Verbosity can be set via the TestRunner.VERBOSITY in the constructor: constant object (enum pattern)

    • OFF (0): No output
    • HEADER (1): High-level section headers only
    • SECTION (2): First level nested title
    • SUBSECTION (3): Second level nested title

    Verbosity level is incremental, i.e. allows all logging events with indentation level that is lower or equal than TestRunner.VERBOSITY.

    const runner = new TestRunner(TestRunner.VERBOSITY.SECTION)
    title("Start Testing") // default indentation is 1 (HEADER)
    runner.exec("Simple Length Test", () => { // default indentation is 2 (SECTION)
    Assert.equals("test".length, 4) // test passed
    })
    function sum(a: number, b: number): number {return a + b}
    const a = 1, b = 2
    runner.exec("Sum Test", () => {
    Assert.equals(sum(a, b), 3) // test passed
    })
    title("End Testing")

    // Output will look like:
    // * Start Testing *
    // ** START: Simple Length Test **
    // ** END: Simple Length Test **
    // ** START: Sum Test **
    // ** END: Sum Test **
    // * End Testing *
    • Test functions are expected to use Assert methods internally. If an assertion fails, the error will be caught and reported with context.
    • For a better organization, consider to put all test cases withing a single TestCase class where each test case scenario is defined with a static method.
    Index

    Constructors

    • Constructs a TestRunner with the specified verbosity level.

      Parameters

      • verbosity: 0 | 1 | 2 | 3 = TestRunner.DEFAULT_VERBOSITY

        One of the values from TestRunner.VERBOSITY (default: HEADER(1))

      Returns TestRunner

    Properties

    VERBOSITY: { HEADER: 1; OFF: 0; SECTION: 2; SUBSECTION: 3 } = ...

    Allowed verbosity levels, to control the output of TestRunner.title method.

    Methods

    • Executes a test case with a title and handles assertions. This method logs the start and end of the test case with titles (using TestRunner.title method) if the verbosity level allows it. It executes the provided function which should contain assertions. If an assertion fails, it will throw an AssertionError. See detailed JSDoc in class documentation

      Parameters

      • name: string

        The name of the test case.

      • fn: () => void

        The function containing the test logic. It should contain assertions using Assert methods.

      • indent: number = TestRunner.VERBOSITY.SECTION

        Indentation level for the title (default: 2(SECTION)). The indentation level is indicated with the number of suffix *.

      Returns void

      • This method does not return a value.

      AssertionError - If an assertion fails within the test function fn. - If the provided fn is not a function.

      Always pass a function reference using () => .... If you pass a direct function call, the code will execute before and not at indicated place it should be executed inside this method and it may produce unexpected results.

      const runner = new TestRunner()
      runner.exec("My Test", () => {
      Assert.equals(1 + 1, 2)
      })
    • Returns the current verbosity level.

      Returns 0 | 1 | 2 | 3

      • The current verbosity level as defined in TestRunner.VERBOSITY.
      const runner = new TestRunner(TestRunner.VERBOSITY.HEADER)
      console.log(runner.getVerbosity()) // Outputs: 1
    • Returns the corresonding string label for the verbosity level. This is useful for logging or debugging purposes.

      Returns string

      • The label corresponding to the current verbosity level.
      const runner = new TestRunner(TestRunner.VERBOSITY.HEADER)
      console.log(runner.getVerbosityLabel()) // Outputs: "HEADER"
    • Conditionally prints a title message based on the configured verbosity. The title is prefixed and suffixed with * characters for visual structure. The number of * will depend on the indentation level, for 2 it shows ** as prefix and suffix.

      Parameters

      • msg: string

        The message to display

      • indent: number = 1

        Indentation level (default: 1(HEADER)). The indentation level is indicated with the number of * characters used as prefix and suffix to the title.

      Returns void

      • This method does not return a value.
      const runner = new TestRunner(TestRunner.VERBOSITY.HEADER)
      runner.title("My Test Section", 2) // Outputs: "** My Test Section **"

      This method is used to create structured output for test sections or titles. It helps in organizing test output visually, especially when running multiple tests. The indentation level allows for hierarchical organization of test cases.