Defining and Running Tests

570 views
Skip to first unread message

Charlie White

unread,
Mar 1, 2018, 12:09:19 PM3/1/18
to bazel-discuss
After much trial and error and community support, I have been able to build a Docker image with Python source that includes multiple packages.  I am now trying to figure out tests.  I have defined a set of tests that I have previously executed with pytest.  I have created a unit_test target using the py_test rule and it generates an entire standalone python script app that executes that single unit test file as defined by "main" in the rule.  What I would like to do is run all *_test.py files that are in my unit_tests directory.  I typically break my tests into small pieces that test only one segment of the application in each.  I rely on pytest to find all the test files, and execute all the defined tests cases.  How do I accomplish this with Bazel?  

I apologize if this question has already been asked and answered, or if there is documentation somewhere that better defines how to use py_test.

Thanks

Brian Silverman

unread,
Mar 1, 2018, 12:15:30 PM3/1/18
to Charlie White, bazel-discuss
Each *_test rule defines a single executable file, so you will need a separate rule for each file with a main in it. However, you can do that automatically via list comprehensions and glob. Something like this (untested):
[py_test(
  name = src[:-3],
  srcs = [
    src,
  ],
) for src in glob(['*_test.py'])]

And then to test all of them at once, you can use a target wildcard like `bazel test //unit_tests/...`.

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discuss+unsubscribe@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/bazel-discuss/a1f3277d-c5b4-4e98-841c-68f8dcfb9a62%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

rodr...@google.com

unread,
Mar 1, 2018, 1:37:30 PM3/1/18
to bazel-discuss

Charlie White

unread,
Mar 1, 2018, 2:00:42 PM3/1/18
to bazel-discuss
As has been my experience with Bazel thus far, I almost got it working. My application is a trivial python app.  The tree looks like this.
.
|____.env
|____env.sample
|____heartbeat
| |______init__.py
| |____heartbeat.py
| |____heartbeat.pyc
| |____tests
| | |______init__.py
| | |____unit
| | | |______init__.py
| | | |____bogus_test.py
| | | |____heartbeat_test.py
|____kubernetes
| |____configmap.yaml
| |____deployment.yaml
|____Makefile
|____README.md
|____requirements.txt
|____WORKSPACE

I added the following to my BUILD file:

SOURCES = glob(["heartbeat/*.py"])
UNIT_TESTS = glob(["heartbeat/tests/unit/*test.py"])
[
py_test(
name=src[:-3],
deps=all_requirements,
srcs=SOURCES + [src],
) for src in UNIT_TESTS
]

I can execute all the tests if I run in a for loop, such as this:
for i in heartbeat/tests/unit/*_test.py;
do
bazel test //:${i%.py} --cache_test_results=no
done


Unfortunately when I try to run tests with a target wildcard, it does not work
$ bazel test //heartbeat/tests/unit/... --cache_test_results=no
ERROR: Skipping '//heartbeat/tests/unit/...': no targets found beneath 'heartbeat/tests/unit'
ERROR: no targets found beneath 'heartbeat/tests/unit'
INFO: Elapsed time: 0.059s
FAILED: Build did NOT complete successfully (0 packages loaded)
ERROR: Couldn't start the build. Unable to run tests

I'm certain that there is something about the targeting that I fail to grasp.

On Thursday, March 1, 2018 at 9:15:30 AM UTC-8, Brian Silverman wrote:
Each *_test rule defines a single executable file, so you will need a separate rule for each file with a main in it. However, you can do that automatically via list comprehensions and glob. Something like this (untested):
[py_test(
  name = src[:-3],
  srcs = [
    src,
  ],
) for src in glob(['*_test.py'])]

And then to test all of them at once, you can use a target wildcard like `bazel test //unit_tests/...`.
On Thu, Mar 1, 2018 at 12:09 PM, Charlie White <cha...@gmail.com> wrote:
After much trial and error and community support, I have been able to build a Docker image with Python source that includes multiple packages.  I am now trying to figure out tests.  I have defined a set of tests that I have previously executed with pytest.  I have created a unit_test target using the py_test rule and it generates an entire standalone python script app that executes that single unit test file as defined by "main" in the rule.  What I would like to do is run all *_test.py files that are in my unit_tests directory.  I typically break my tests into small pieces that test only one segment of the application in each.  I rely on pytest to find all the test files, and execute all the defined tests cases.  How do I accomplish this with Bazel?  

I apologize if this question has already been asked and answered, or if there is documentation somewhere that better defines how to use py_test.

Thanks

--
You received this message because you are subscribed to the Google Groups "bazel-discuss" group.
To unsubscribe from this group and stop receiving emails from it, send an email to bazel-discus...@googlegroups.com.

Austin Schuh

unread,
Mar 1, 2018, 2:06:43 PM3/1/18
to Charlie White, bazel-discuss
Which folder is your BUILD file in?  That deliminates what's before the : and after the :.  From what you have written, your target should be something like //:heartbeat/tests/unit/heartbeat_test

bazel test //... --cache_test_results=no

should do it as well.

Austin

Charlie White

unread,
Mar 1, 2018, 2:27:24 PM3/1/18
to bazel-discuss
Sorry, I edited out the virtualenv and git folders from the tree, and must have also removed BUILD.  My BUILD file is in the root directory with the WORKFILE.

Charlie White

unread,
Mar 1, 2018, 2:49:14 PM3/1/18
to bazel-discuss
Thanks, the target wildcard you specified worked great, it executes all the tests defined in my BUILD.


On Thursday, March 1, 2018 at 11:06:43 AM UTC-8, Austin Schuh wrote:
Reply all
Reply to author
Forward
0 new messages