Skip to content

dougthor42/bazel-python-src-tests-example

Repository files navigation

Bazel for Python that uses src and tests dirs

An example of how to configure bazel (via the new bzlmod MODULE.bazel) for Python projects that use a src dir and have tests outside of the package in a separate tests dir.

Key points that make this example interesting:

  • All python code is in a src dir, as recommended by the Python Packaging User Guide.
  • Tests are outside of the package and thus not shipped with distributions.
  • The package must be installed before tests will work.
    • Typically this is done via editable installs pip install -e ..

Usage

  1. Clone the repo.
  2. Make a venv and activate it.
  3. Install the package pip install -e .[dev]
  4. Make sure non-bazel basic unit tests work python -m unittest.
  5. And make sure bazel unit tests work bazel test //....
  6. And that bazel run works: bazel run //src/mypackage:mypackage_bin.

If I've set up this example correctlly, everything should pass.

Buildifier

Buildifier is a linter and autoformatter for Starlark, the language used by Bazel.

Run with one or both of:

bazel run //:buildifier.fix
bazel run //:buildifier.check

Gazelle

Gazelle is a tool for autogenerating BUILD(.bazel) files from source code.

Run by calling all these, in order:

# If any python dependencies change:
bazel run //:requirements.update
bazel run //:gazelle_python_manifest.update
# Run gazelle and generate BUILD files and targets:
bazel run //:gazelle

About

Example of using Bazel with python `src` and `tests` dir

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published