Skip to content
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

Cannot run individual tests when using "pytest" #6930

Open
jpgxs opened this issue Oct 26, 2024 · 4 comments
Open

Cannot run individual tests when using "pytest" #6930

jpgxs opened this issue Oct 26, 2024 · 4 comments
Assignees
Labels
awaiting-maintainer Awaiting review from Bazel team on issues product: IntelliJ IntelliJ plugin product: PyCharm PyCharm plugin type: feature request

Comments

@jpgxs
Copy link

jpgxs commented Oct 26, 2024

Description of the feature request:

When using pytest, there are no play icons ▶️ for running individual tests (individual methods, classes, modules) using Bazel.

It appears that only unittest.TestCase is supported, based on python/src/com/google/idea/blaze/python/run/PyTestUtils.java.

I'm happy to submit a PR for this if we can agree on the implementation.

Which category does this issue belong to?

Intellij, PyCharm

What underlying problem are you trying to solve with this feature?

Want to run run filtered pytest tests from the IntelliJ/PyCharm editor

What operating system, Intellij IDE and programming languages are you using? Please provide specific versions.

No response

Have you found anything relevant by searching the web?

No response

Any other information, logs, or outputs that you want to share?

No response

@jpgxs
Copy link
Author

jpgxs commented Oct 26, 2024

To add support for pytest, we would need to detect:

a. Test Classes: any class with name "Test*"
b. Test Methods: any method with name "test_" (possibly inside a test class)
c. (for bonus points) Test Modules: any module with name "test_
"

(a) and (b) are relatively easy - not so sure about (c) though.

In any case, we would need to agree...

  • Should this be an opt-in feature? If so, how should it be opted into and where should it be documented?
  • Should we use the "Python Integrated Tools -> Default test runner" setting?

@JeroenSchmidt
Copy link

Hi @jpgxs
I'm a user of the plugin and I'm able to select individual pytest tests to run. Debugging also works. See the screenshot.
I would suggest you check that your using the latest version of pycharm/intelij (+python plugin)

image

Having said that. When running an "individual test" it ends up running all test tests in that target (I'm not sure if this is expected behaviour or a bug). A question to the maintainers, is there a reason something like the following isn't passed? bazel test //path/to/target:test --test_arg='-k {test_function_name}'

@jpgxs
Copy link
Author

jpgxs commented Oct 26, 2024

Thanks @JeroenSchmidt, I seem to get some interesting behaviour now that I'm looking closely.

If I define a test at the module level (not inside a class), I get the same behaviour as you:

image

However, for test classes (Test*.py) and their methods (test_*.py), I either get:

  1. No play button
  2. A play button with "Nothing here"
  3. A play button to run all the tests in the Bazel test

It's not clear to me why (1) happens as it seems to have changed over the course of today; I think it's related to IntelliJ's native pytest integration + caching.

Currently, I get "Nothing Here" for all tests in the file:

image

This changes in all cases if I either:

(a) Define a test at the module level:

image

(b) Define a test class with name "*Test":

image

I think this logic is being handled here:


Of course, these aren't very useful if they run all the tests in the suite anyway; some of our py_test targets take 10+ minutes to run if you're running everything!

The unittest integration seems to pass though the --test_filter argument with TestClass.test_method / TestClass / test_method.

For pytest, I think it should either:

a. Pass it through with exactly the same scheme as unittest.
b. Pass it through in a pytest native format module.py::TestClass::test_method

Personally, I'm leaning towards (b) but it might be a bit trickier

@mai93 mai93 assigned tpasternak and unassigned mai93 Nov 6, 2024
@tpasternak
Copy link
Collaborator

tpasternak commented Nov 7, 2024

I'm happy to submit a PR for this if we can agree on the implementation.

Thank you @jpgxs , you're welcome! cc @agluszak

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
awaiting-maintainer Awaiting review from Bazel team on issues product: IntelliJ IntelliJ plugin product: PyCharm PyCharm plugin type: feature request
Projects
Status: Untriaged
Development

No branches or pull requests

4 participants