Abseil Python’s testing library is similar to Python’s standard unittest
module (sometimes referred to as PyUnit) but offers some additional useful
features on top of the standard library, such as interfacing with Abseil Flags.
To use the Abseil testing library, do the following in your unit tests:
absltest
moduleflags
module, which gives you access to the variables
FLAGS.test_srcdir
and FLAGS.test_tmpdir
.absltest.main()
instead of unittest.main()
Within a unit test class, any method name starting with ‘test’ will be run automatically as part of the unit test. Test names should describe the particular case being tested.
The pattern is simple and common to most xUnit frameworks. Basically:
main()
scans the current file for classes derived from TestCase
main()
then scans each class for methods prefixed by "test"
(e.g.
testInitialize(self):
)setUp()
method, if one existstearDown()
method, if one existsNote that setUp()
and tearDown()
are run once for each test method, so they
are a great place to put data initialization and resource cleanup.
Validation methods such as assertEqual()
raise an exception on failure,
which terminates the current test method. Unit test execution will continue with
the remaining test methods.
Additional methods, inherited from absltest
, are available to validate
results:
self.assertTrue
self.assertFalse
self.assertEqual
self.assertNotEqual
self.fail
The Abseil testing library contains many more methods than the five above; some are in Python’s built-in unittest.TestCase while others are in Abseil Python’s subclass absl.testing.absltest.
More examples are forthcoming, but for now please see absl-py’s own tests for
examples of how to use absltest
.