Add TestCase.enter_context to make it easier to use context managers in tests.

When writing tests, re-using the same context manager logic is hard because
of the separatation of setUp() from the test being executed. Re-using the
logic require decorating the test function (which usually adds an argument
to the signature), or copy/pasting the same `with` block (which loses a
level of indent and can create a lot of redundant code).

This can be particularly annoying with mock.patch, since you must either
decorate every function, or add very boilerplate, awkward, and somewhat subtle
cleanup logic in setUp to properly patch, start, store, and cleanup the
patching and its created mock object.

To alleviate this, contextlib.ExitStack can be used to more manually manage
the lifecycle of context managers.

NOTE: This is a Python 3-only feature because contextlib.ExitStack is Py3-only.
While contextlib2 could be used for Py2 support, that involves pulling in another
dependency, so can be done in a future change.
PiperOrigin-RevId: 277084054
Change-Id: Ic6baee7908a96600ff577217568fec7e5276d48d
3 files changed
tree: 08929ccff4674cd55c2eb4b475fa2ae8bd7b9d30
  1. absl/
  2. smoke_tests/
  3. third_party/
  4. AUTHORS
  5. CONTRIBUTING.md
  6. LICENSE
  7. MANIFEST.in
  8. README.md
  9. setup.py
  10. WORKSPACE
README.md

Abseil Python Common Libraries

This repository is a collection of Python library code for building Python applications. The code is collected from Google's own Python code base, and has been extensively tested and used in production.

Features

  • Simple application startup
  • Distributed commandline flags system
  • Custom logging module with additional features
  • Testing utilities

Getting Started

Installation

To install the package, simply run:

pip install absl-py

Or install from source:

python setup.py install

Running Tests

To run Abseil tests, you can clone the git repo and run bazel:

git clone https://github.com/abseil/abseil-py.git
cd abseil-py
bazel test absl/...

Example Code

Please refer to smoke_tests/sample_app.py as an example to get started.

Documentation

See the Abseil Python Developer Guide.

Future Releases

The current repository includes an initial set of libraries for early adoption. More components and interoperability with Abseil C++ Common Libraries will come in future releases.

License

The Abseil Python library is licensed under the terms of the Apache license. See LICENSE for more information.