bazel-skylib/docs/unittest_doc.md

20 KiB
Executable File

unittest_toolchain

unittest_toolchain(name, failure_templ, file_ext, join_on, success_templ)

Attributes

name Name; required

A unique name for this target.

failure_templ String; required
file_ext String; required
join_on String; required
success_templ String; required

analysistest.make

analysistest.make(impl, expect_failure, config_settings)

Creates an analysis test rule from its implementation function.

An analysis test verifies the behavior of a "real" rule target by examining and asserting on the providers given by the real target.

Each analysis test is defined in an implementation function that must then be associated with a rule so that a target can be built. This function handles the boilerplate to create and return a test rule and captures the implementation function's name so that it can be printed in test feedback.

An example of an analysis test:

def _your_test(ctx):
  env = analysistest.begin(ctx)

  # Assert statements go here

  return analysistest.end(env)

your_test = analysistest.make(_your_test)

Recall that names of test rules must end in _test.

Parameters

impl required.

The implementation function of the unit test.

expect_failure optional. default is False

If true, the analysis test will expect the target_under_test to fail. Assertions can be made on the underlying failure using asserts.expect_failure

config_settings optional. default is {}

A dictionary of configuration settings to change for the target under test and its dependencies. This may be used to essentially change 'build flags' for the target under test, and may thus be utilized to test multiple targets with different flags in a single build

analysistest.begin

analysistest.begin(ctx)

Begins a unit test.

This should be the first function called in a unit test implementation function. It initializes a "test environment" that is used to collect assertion failures so that they can be reported and logged at the end of the test.

Parameters

ctx required.

The Skylark context. Pass the implementation function's `ctx` argument in verbatim.

analysistest.end

analysistest.end(env)

Ends an analysis test and logs the results.

This must be called and returned at the end of an analysis test implementation function so that the results are reported.

Parameters

env required.

The test environment returned by `analysistest.begin`.

analysistest.fail

analysistest.fail(env, msg)

Unconditionally causes the current test to fail.

Parameters

env required.

The test environment returned by `unittest.begin`.

msg required.

The message to log describing the failure.

analysistest.target_actions

analysistest.target_actions(env)

Returns a list of actions registered by the target under test.

Parameters

env required.

The test environment returned by `analysistest.begin`.

analysistest.target_under_test

analysistest.target_under_test(env)

Returns the target under test.

Parameters

env required.

The test environment returned by `analysistest.begin`.

asserts.expect_failure

asserts.expect_failure(env, expected_failure_msg)

Asserts that the target under test has failed with a given error message.

This requires that the analysis test is created with analysistest.make() and expect_failures = True is specified.

Parameters

env required.

The test environment returned by `analysistest.begin`.

expected_failure_msg optional. default is ""

The error message to expect as a result of analysis failures.

asserts.equals

asserts.equals(env, expected, actual, msg)

Asserts that the given expected and actual values are equal.

Parameters

env required.

The test environment returned by `unittest.begin`.

expected required.

The expected value of some computation.

actual required.

The actual value returned by some computation.

msg optional. default is None

An optional message that will be printed that describes the failure. If omitted, a default will be used.

asserts.false

asserts.false(env, condition, msg)

Asserts that the given condition is false.

Parameters

env required.

The test environment returned by `unittest.begin`.

condition required.

A value that will be evaluated in a Boolean context.

msg optional. default is "Expected condition to be false, but was true."

An optional message that will be printed that describes the failure. If omitted, a default will be used.

asserts.set_equals

asserts.set_equals(env, expected, actual, msg)

Asserts that the given expected and actual sets are equal.

Parameters

env required.

The test environment returned by `unittest.begin`.

expected required.

The expected set resulting from some computation.

actual required.

The actual set returned by some computation.

msg optional. default is None

An optional message that will be printed that describes the failure. If omitted, a default will be used.

asserts.new_set_equals

asserts.new_set_equals(env, expected, actual, msg)

Asserts that the given expected and actual sets are equal.

Parameters

env required.

The test environment returned by `unittest.begin`.

expected required.

The expected set resulting from some computation.

actual required.

The actual set returned by some computation.

msg optional. default is None

An optional message that will be printed that describes the failure. If omitted, a default will be used.

asserts.true

asserts.true(env, condition, msg)

Asserts that the given condition is true.

Parameters

env required.

The test environment returned by `unittest.begin`.

condition required.

A value that will be evaluated in a Boolean context.

msg optional. default is "Expected condition to be true, but was false."

An optional message that will be printed that describes the failure. If omitted, a default will be used.

register_unittest_toolchains

register_unittest_toolchains()

Registers the toolchains for unittest users.

unittest.make

unittest.make(impl, attrs)

Creates a unit test rule from its implementation function.

Each unit test is defined in an implementation function that must then be associated with a rule so that a target can be built. This function handles the boilerplate to create and return a test rule and captures the implementation function's name so that it can be printed in test feedback.

The optional attrs argument can be used to define dependencies for this test, in order to form unit tests of rules.

An example of a unit test:

def _your_test(ctx):
  env = unittest.begin(ctx)

  # Assert statements go here

  return unittest.end(env)

your_test = unittest.make(_your_test)

Recall that names of test rules must end in _test.

Parameters

impl required.

The implementation function of the unit test.

attrs optional. default is None

An optional dictionary to supplement the attrs passed to the unit test's `rule()` constructor.

unittest.suite

unittest.suite(name, test_rules)

Defines a test_suite target that contains multiple tests.

After defining your test rules in a .bzl file, you need to create targets from those rules so that blaze test can execute them. Doing this manually in a BUILD file would consist of listing each test in your load statement and then creating each target one by one. To reduce duplication, we recommend writing a macro in your .bzl file to instantiate all targets, and calling that macro from your BUILD file so you only have to load one symbol.

For the case where your unit tests do not take any (non-default) attributes -- i.e., if your unit tests do not test rules -- you can use this function to create the targets and wrap them in a single test_suite target. In your .bzl file, write:

def your_test_suite():
  unittest.suite(
      "your_test_suite",
      your_test,
      your_other_test,
      yet_another_test,
  )

Then, in your BUILD file, simply load the macro and invoke it to have all of the targets created:

load("//path/to/your/package:tests.bzl", "your_test_suite")
your_test_suite()

If you pass N unit test rules to unittest.suite, N + 1 targets will be created: a test_suite target named ${name} (where ${name} is the name argument passed in here) and targets named ${name}_test_${i}, where ${i} is the index of the test in the test_rules list, which is used to uniquely name each target.

Parameters

name required.

The name of the `test_suite` target, and the prefix of all the test target names.

test_rules optional.

A list of test rules defines by `unittest.test`.

unittest.begin

unittest.begin(ctx)

Begins a unit test.

This should be the first function called in a unit test implementation function. It initializes a "test environment" that is used to collect assertion failures so that they can be reported and logged at the end of the test.

Parameters

ctx required.

The Skylark context. Pass the implementation function's `ctx` argument in verbatim.

unittest.end

unittest.end(env)

Ends a unit test and logs the results.

This must be called and returned at the end of a unit test implementation function so that the results are reported.

Parameters

env required.

The test environment returned by `unittest.begin`.

unittest.fail

unittest.fail(env, msg)

Unconditionally causes the current test to fail.

Parameters

env required.

The test environment returned by `unittest.begin`.

msg required.

The message to log describing the failure.