base.testing

A Somewhat Underbaked Unit Test Framework

There are probably a hundred utility libraries to run unit tests, this is ours.

At the highest level, you define test cases like this:

class MyTest(base.TestCase):
  def Run(self):
    # ... do stuff ...
    self.LogResult(passed, message)

This adds a test case to the registry. Then from some module that you call as your entrypoint, you do this:

base.TestRunner().RunFromCommandLine()

This will run every registered test case.

If you run this example as it’s shown, you’ll see our own test cases run for various parts of Base.

A couple of command-line options can help restrict the set of which test cases are actually run. Specifically, --only and --exclude are parsed from the command line, each taking a comma-separated list of test case object names, with wildcards allowed.

Multiple Results

A test case may call LogResult() more than once – all results are accumulated, and the test is considered passed only if all the results have passed.

Exceptions

If a test case raises an exception it’s considered failed.

Contexts

Each test case can exist within a Context, which specifies setup and cleanup behavior around the test.

class MyTestContext(base.TestContext):

  def SetUp(self):
    # ... do stuff ...

  def CleanUp(self):
    # ... do stuff ...

Then your test case attaches to the context like this:

class MyTest(base.TestCase):
  TEST_CONTEXT = MyTestContext

Alternately, you can define a context for an entire module:

class MyTestContext(base.TestModuleContext):
    # ... SetUp() and CleanUp() ...

Every test case within the same module will automatically run within this context.


Back to “OctoBase”