Skip to main content

Mojo struct

TestSuite

struct TestSuite

A suite of tests to run.

You can automatically collect and register test functions starting with test_ by calling the discover_tests static method, and then running the entire suite by calling the run method.

Example:

from testing import assert_equal, TestSuite

def test_something():
    assert_equal(1 + 1, 2)

def test_some_other_thing():
    assert_equal(2 + 2, 4)

def main():
    TestSuite.discover_tests[__functions_in_module()]().run()

Alternatively, you can manually register tests by calling the test method.

from testing import assert_equal, TestSuite

def some_test():
    assert_equal(1 + 1, 2)

def main():
    var suite = TestSuite()
    suite.test[some_test]()
    suite^.run()

Fields

  • tests (List[_Test]): The list of tests registered in this suite.
  • location (_SourceLocation): The source location where the test suite was created.
  • skip_list (Set[StringSlice[StaticConstantOrigin]]): The list of tests to skip in this suite.
  • allow_list (Optional[Set[StringSlice[StaticConstantOrigin]]]): The list of tests to allow in this suite.
  • cli_args (List[StringSlice[StaticConstantOrigin]]): The raw command line arguments passed to the test suite.

Implemented traits

AnyType, Movable, UnknownDestructibility

Aliases

__del__is_trivial

alias __del__is_trivial = False

__moveinit__is_trivial

alias __moveinit__is_trivial = False

Methods

__init__

__init__(out self, *, location: Optional[_SourceLocation] = None, var cli_args: Optional[List[StringSlice[StaticConstantOrigin]]] = None)

Create a new test suite.

Args:

  • location (Optional): The location of the test suite (defaults to __call_location).
  • cli_args (Optional): The command line arguments to pass to the test suite (defaults to sys.argv()).

__del__

__del__(var self)

Destructor for the test suite.

discover_tests

static discover_tests[test_funcs: Tuple[element_types], /](*, location: Optional[_SourceLocation] = None, var cli_args: Optional[List[StringSlice[StaticConstantOrigin]]] = None) -> Self

Discover tests from the given list of functions, and register them.

Parameters:

  • test_funcs (Tuple): The pack of functions to discover tests from. In most cases, callers should pass __functions_in_module().

Args:

  • location (Optional): The location of the test suite (defaults to __call_location).
  • cli_args (Optional): The command line arguments to pass to the test suite (defaults to sys.argv()).

Returns:

Self: A new TestSuite with all discovered tests registered. Raises:

If test discovery fails (e.g. because of a nonconforming test function signature).

test

test[f: fn() raises -> None](mut self)

Registers a test to be run.

Parameters:

  • f (fn() raises -> None): The function to run.

skip

skip[f: fn() raises -> None](mut self)

Registers a test to be skipped.

If attempting to skip a test that is not registered in the suite (either explicitly or via automatic discovery), an error will be raised.

Parameters:

  • f (fn() raises -> None): The function to skip.

Raises:

If the test is not found in the test suite.

generate_report

generate_report(mut self, skip_all: Bool = False) -> TestSuiteReport

Runs the test suite and generates a report.

Args:

  • skip_all (Bool): Only collect tests, but don't execute them (defaults to False).

Returns:

TestSuiteReport: A report containing the results of all tests. Raises:

If an error occurs during test collection.

run

run(var self, *, quiet: Bool = False, skip_all: Bool = False)

Runs the test suite and prints the results to the console.

Args:

  • quiet (Bool): Suppresses printing the report when the suite does not fail (defaults to False).
  • skip_all (Bool): Only collect tests, but don't execute them (defaults to False).

Raises:

If a test in the test suite fails or if an error occurs during test collection.

Was this page helpful?