-
Notifications
You must be signed in to change notification settings - Fork 541
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
pytest support #240
Comments
It is already possible to use
test.py
|
Thanks for the quick reply. I saw someone mention this trick on the mailing list https://groups.google.com/forum/?nomobile=true#!searchin/bazel-discuss/pytest|sort:date/bazel-discuss/d1RKUmyve_Q/hm_Dc3XYCAAJ If you forget the |
i added a sample macro for pytest here https://github.com/ali5h/rules_pip/blob/master/defs.bzl#L116 |
PTAL at pull request #243. It takes a slightly different approach. In particular, it does not pull dependency on pytest on its own, to give users more freedom (e.g. whether to use requirements.txt, virtualenv or vendor pytest in repository). |
i added that as a sample. there is no need to load the |
Suppose defining your own BUILD rule is the right solution: semantically it would belong to /contrib (though there isn't one), instead of /experimental, which is more for things that might become official in the future. This issue specifically is a Personally I think having a 10-line main file as @ali5h did is most straightforward and I probably won't even create any custom BUILD rule:
|
If the |
We don't use relative imports anywhere in our Bazel workspace. They're not worth the trouble imo, and I wouldn't be surprised at all if implementation details in Python rules made them not work correctly. |
@thundergolfer why do python rules has the |
@doubler We used to use that |
@dayfine makes a solid point, There's also the more generalized approach from what @groodt shares above, that includes the passing of arguments from bazel
py_test(
name = "test_foo",
srcs = [
"test_foo.py",
],
args = [
"--junit-xml=test_foo_results.xml",
],
deps = [
"pytest",
],
)
import pytest
import sys
...
if __name__ == "__main__":
# if using 'bazel test ...'
args = sys.argv[1:]
ret_code = pytest.main([__file__, "--verbose"] + args)
# Print relevant debug logs or other artifacts for CI job to have richer context
if ret_code != 0:
print_debug_stuff(args)
# Exit with return code
sys.exit(ret_code) |
Actually now we're hitting the case that @double mentioned above...
The use case hoping to have supported for us is to be able to execute test runs from either the With pytest I can drop import pdb
pdb.set_trace()
|
@jxramos RE the "forces the debug session to quit" I've copied below a workaround:
|
You should be able "bazel run" a py_test target; no need to change it to
py_binary first.
śr., 31 mar 2021 o 01:23 Jonathon Belotti ***@***.***>
napisał(a):
… @jxramos <https://github.com/jxramos> RE the "forces the debug session to
quite" I've copied below a workaround:
... Unfortunately, this does not work so simply for bazel test and py_test
targets. If you add a breakpoint using pdb and run a test that will
trigger that breakpoint, you won't be dropped into a REPL the process will
crash. As a workaround, change your py_test target to py_binary and do bazel
run on it instead of bazel test. Your test code will run and you’ll be
dropped into the pdb debugger:
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#240 (comment)>,
or unsubscribe
<https://github.com/notifications/unsubscribe-auth/AKU4J4LJNAB4HUJPG53LTKLTGJMNVANCNFSM4I7SLRTQ>
.
--
Paweł Stradomski
|
@pstradomski I tested using |
Dnia poniedziałek, 19 kwietnia 2021 05:36:04 CEST Jonathon Belotti pisze:
@pstradomski I tested using `py_test` and `pdb` and it did not work for me.
If it actually does work, then I'd certainly be interested in finding out
what was wrong in my setup.
So in my toy project I just did
$ bazel run //rssreader/common:tests_util_test_py
and it just worked (without pdb).
I suspect pdb has issues since "bazel run" closes stdin, but then I don't
understand how changing to py_binary would help.
It's also possible to do:
$ bazel run //rssreader/common:tests_util_test_py
and then
$ bazel-bin/rssreader/common/tests_util_test_py
which avoids the problem of stdin takeover, but is not quite friendly.
…--
Paweł Stradomski
|
I don't either, but it does help. Didn't bother venturing into the source to understand what was up. |
Some work has begun here on |
It would be nice to support Bazel's I'm using this in my own pytest running in combinations with some changes I've made to the IntelliJ plugin to pass
|
Bazel is throwing ModuleNotFoundError when sys.exit(pytest.main()) is used instead of just pytest.main(). But the same script works in PyCharm. test_file.pyimport os def test_true(): if __ name __ == "__ main __": # Left whitespace around keywords to prevent Bold BUILDpy_test( test.logE ModuleNotFoundError: No module named '__main.source'; 'main' is not a package source is the directory containing test_file.py & BUILD. |
I put up something until it's available in |
This issue has been automatically marked as stale because it has not had any activity for 180 days. It will be closed if no further activity occurs in 30 days. |
*activities* |
this is the solution I'm using currently: https://gist.github.com/betaboon/c1dd785b5ba468b4df4e382eafff969a |
This issue has been automatically marked as stale because it has not had any activity for 180 days. It will be closed if no further activity occurs in 30 days. |
Should close this out; see #723 (comment) Ftr here's the solution I've been using happily for some time - |
Closing. @arrdem if you want to do a PR with the support I would love to take a look at it. |
https://github.com/caseyduquettesc/rules_python_pytest this came up in the slack yesterday, leaving it here for posterity. I note with some amusement it's almost line-for-line what I invented for my repo and what we're now using at Abnormal. I think this comment https://github.com/caseyduquettesc/rules_python_pytest/blob/main/python_pytest/defs.bzl#L34 captures why having "in the box" support would be hard. Maybe you could do the same dance as aignas@6b54226, but sideloading in requirements alongside user code with potential for requirements conflicts seems like trouble. It'd be better to find a way to configure the Python toolchain with a |
Another 3p pointer: https://docs.aspect.build/rules/aspect_rules_py/docs/rules#py_pytest_main provides missing glue for pytest, thanks to @mattem and @f0rmiga |
Another project that provides See the doc links in the repo for usage. |
py_test
is a Bazel rule andpytest
is a Python library for writing and running testshttps://docs.pytest.org
https://github.com/pytest-dev/pytest
I've found two third-party Bazel rules for running tests written with pytest bazel_rules_pex and rules_pyz, but both projects are unmaintaned and don't work anymore. Edit 2020-01-15: see https://github.com/ali5h/rules_pip/blob/b4e94d0ddc95367217b7059bcdb926c159d28602/defs.bzl#L139 as well.
pytest has been mentioned a few times on the bazel mailing list https://groups.google.com/forum/?nomobile=true#!searchin/bazel-discuss/pytest%7Csort:date
I'm not sure what the correct way to add support for it would be. Probably another rule but I don't know what it should be called, because the names are so similar.
pytest
pytest_py_test
py_test_pytest
all seem pretty funny. Or maybe it could be an option forpy_test
or a global one in WORKSPACE.pytest
can run tests written withunittest
(and has nicer output when a test fails), so theoretically you could just change thepy_test
rule to run tests with pytest but that's obviously not happening.The text was updated successfully, but these errors were encountered: