diff --git a/.gitattributes b/.gitattributes new file mode 100644 index 0000000..aa377d8 --- /dev/null +++ b/.gitattributes @@ -0,0 +1 @@ +docs/*.md linguist-generated diff --git a/docs/rules.md b/docs/rules.md index 4531a1a..da6ace2 100644 --- a/docs/rules.md +++ b/docs/rules.md @@ -1,38 +1,54 @@ -Public API +Use pytest to run tests, using a wrapper script to interface with Bazel. - +Example: -## py_pytest_test +```starlark +load("@caseyduquettesc_rules_python_pytest//python_pytest:defs.bzl", "py_pytest_test") -
-py_pytest_test(name, srcs, deps, args, kwargs)
-
+py_pytest_test( + name = "test_w_pytest", + size = "small", + srcs = ["test.py"], +) +``` -Use pytest to run tests, using a wrapper script to interface with Bazel. +By default, `@pip//pytest` is added to `deps`. +If sharding is used (when `shard_count > 1`) then `@pip//pytest_shard` is also added. +To instead provide explicit deps for the pytest library, set `pytest_deps`: -```py +```starlark py_pytest_test( - name = "test_w_pytest", - size = "small", - srcs = ["test.py"], - deps = [ - # TODO Add this for the user - requirement("pytest"), - ], + name = "test_w_my_pytest", + shard_count = 2, + srcs = ["test.py"], + pytest_deps = [requirement("pytest"), requirement("pytest-shard"), ...], ) ``` + + + +## py_pytest_test + +
+py_pytest_test(name, srcs, deps, args, pytest_deps, pip_repo, kwargs)
+
+ + Wrapper macro for `py_test` which supports pytest. + **PARAMETERS** | Name | Description | Default Value | | :------------- | :------------- | :------------- | -| name |

-

| none | -| srcs |

-

| none | -| deps |

-

| [] | -| args |

-

| [] | -| kwargs |

-

| none | +| name | A unique name for this target. | none | +| srcs | Python source files. | none | +| deps | Dependencies, typically py_library. | [] | +| args | Additional command-line arguments to pytest. See https://docs.pytest.org/en/latest/how-to/usage.html | [] | +| pytest_deps | Labels of the pytest tool and other packages it may import. | None | +| pip_repo | Name of the external repository where Python packages are installed. It's typically created by pip.parse. This attribute is used only when pytest_deps is unset. | "pip" | +| kwargs | Additional named parameters to py_test. | none | diff --git a/e2e/smoke/BUILD.bazel b/e2e/smoke/BUILD.bazel index 693ef3b..f609a3e 100644 --- a/e2e/smoke/BUILD.bazel +++ b/e2e/smoke/BUILD.bazel @@ -19,11 +19,11 @@ py_pytest_test( name = "test_sharded", size = "small", srcs = ["test_sharded.py"], - shard_count = 2, - deps = [ + pytest_deps = [ requirement("pytest"), requirement("pytest-shard"), ], + shard_count = 2, ) build_test( diff --git a/python_pytest/defs.bzl b/python_pytest/defs.bzl index cc6cd5a..971d272 100644 --- a/python_pytest/defs.bzl +++ b/python_pytest/defs.bzl @@ -1,25 +1,56 @@ -"""Public API""" +"""Use pytest to run tests, using a wrapper script to interface with Bazel. + +Example: + +```starlark +load("@caseyduquettesc_rules_python_pytest//python_pytest:defs.bzl", "py_pytest_test") + +py_pytest_test( + name = "test_w_pytest", + size = "small", + srcs = ["test.py"], +) +``` + +By default, `@pip//pytest` is added to `deps`. +If sharding is used (when `shard_count > 1`) then `@pip//pytest_shard` is also added. +To instead provide explicit deps for the pytest library, set `pytest_deps`: + +```starlark +py_pytest_test( + name = "test_w_my_pytest", + shard_count = 2, + srcs = ["test.py"], + pytest_deps = [requirement("pytest"), requirement("pytest-shard"), ...], +) +``` +""" load("@rules_python//python:defs.bzl", "py_test") -# load("@py_deps//:requirements.bzl", "requirement") - -def py_pytest_test(name, srcs, deps = [], args = [], **kwargs): - """Use pytest to run tests, using a wrapper script to interface with Bazel. - - ```py - py_pytest_test( - name = "test_w_pytest", - size = "small", - srcs = ["test.py"], - deps = [ - # TODO Add this for the user - requirement("pytest"), - ], - ) - ``` + +def py_pytest_test(name, srcs, deps = [], args = [], pytest_deps = None, pip_repo = "pip", **kwargs): + """ + Wrapper macro for `py_test` which supports pytest. + + Args: + name: A unique name for this target. + srcs: Python source files. + deps: Dependencies, typically `py_library`. + args: Additional command-line arguments to pytest. + See https://docs.pytest.org/en/latest/how-to/usage.html + pytest_deps: Labels of the pytest tool and other packages it may import. + pip_repo: Name of the external repository where Python packages are installed. + It's typically created by `pip.parse`. + This attribute is used only when `pytest_deps` is unset. + **kwargs: Additional named parameters to py_test. """ shim_label = Label("//python_pytest:pytest_shim.py") + if pytest_deps == None: + pytest_deps = ["@{}//pytest".format(pip_repo)] + if kwargs.get("shard_count", 1) > 1: + pytest_deps.append("@{}//pytest_shard".format(pip_repo)) + py_test( name = name, srcs = [ @@ -31,10 +62,6 @@ def py_pytest_test(name, srcs, deps = [], args = [], **kwargs): ] + args + ["$(location :%s)" % x for x in srcs], # python_version = "PY3", # srcs_version = "PY3", - # TODO It'd be nice to implicitly include pytest, but I don't know how to know the requirements repo nme - # deps = deps + [ - # requirement("pytest"), - # ], - deps = deps, + deps = deps + pytest_deps, **kwargs )