Is this a bug? Running individual test after running unittest discovery on entire project fails

The Project

I have set up a sample project to illustrate this problem. It's available on GitHub, but here is the source tree so you get the idea:

PyCharm 2023.3.2 (Community Edition)
Build #PC-233.13135.95, built on December 19, 2023
Runtime version: 17.0.9+7-b1087.9 amd64
VM: OpenJDK 64-Bit Server VM by JetBrains s.r.o.
Windows 10.0
GC: G1 Young Generation, G1 Old Generation
Memory: 4060M
Cores: 8
Registry:
 ide.experimental.ui=true
Non-Bundled Plugins:
 com.intellij.plugins.eclipsekeymap (233.11799.165)
 com.github.copilot (1.4.6.4092)

Running All Tests Works Fine

I've set a run configuration for running unittest test discovery on the entire project (available on GitHub), and it works perfectly. Here's the Run configuration:

For copy & paste purposes, the Additional Arguments field contains:

discover -t C:\Users\rodrigobraz\Documents\PyCharmProjects\UnittestDiscovery -s C:\Users\rodrigobraz\Documents\PyCharmProjects\UnittestDiscovery\package

After running the tests, I get the test tree on the left side of the Run tool window:

Re-running a Single Test Doesn't Work

Now suppose I want to re-run test_some_unit (which is of course useful when a particular test fails and we want to try again after fixing it). I can open the context menu, and select Run Unittest for <test description>:

This should just run that particular test individually, but instead I get an error (see full error at the end of post).

That action generates a new Run configuration like this:

Note that this configuration is not “Custom” like the other, but of the type “Module name”.

For copy & paste purposes, the Module name field contains:

package.unit.test_some_unit.TestSomeUnit.test_some_unit

and the Additional Arguments field contains:

discover -t C:\Users\rodrigobraz\Documents\PyCharmProjects\UnittestDiscovery -s C:\Users\rodrigobraz\Documents\PyCharmProjects\UnittestDiscovery\package

This does not run properly. The full output of the command is at the end of this post.

What Seems Wrong

The command being run is

python -m unittest discover -t C:\Users\rodrigobraz\Documents\PyCharmProjects\UnittestDiscovery -s C:\Users\rodrigobraz\Documents\PyCharmProjects\UnittestDiscovery\package package.unit.test_some_unit.TestSomeUnit.test_some_unit

This does not seem to conform with the use of the discover command of unittest according to its documentation. Specifically, I don't think you can pass a single test like package.unit.test_some_unit.TestSomeUnit.test_some_unit to it.

It seems particularly confused when it says

ImportError: Start directory is not importable: 'package.unit.test_some_unit.TestSomeUnit.test_some_unit'

because the starting directory passed (through option -s) was actually C:\Users\rodrigobraz\Documents\PyCharmProjects\UnittestDiscovery\package.

Proposed Solution

The context-menu action for running a single test should instead simply create a Unittest run configuration with a Module name equal to the module path and test method, without any additional arguments:

This runs the single test without a problem.

In other words, test discovery should not be used when we are running a single specific test (there is no need to discover it).

Full Error Output

C:\ProgramData\anaconda3\python.exe "C:/Program Files/JetBrains/PyCharm Community Edition 2023.2.1/plugins/python-ce/helpers/pycharm/_jb_unittest_runner.py" --target package.unit.test_some_unit.TestSomeUnit.test_some_unit -- discover -t C:\Users\rodrigobraz\Documents\PyCharmProjects\UnittestDiscovery -s C:\Users\rodrigobraz\Documents\PyCharmProjects\UnittestDiscovery\package 
Testing started at 8:51 PM ...
Launching unittests with arguments python -m unittest discover -t C:\Users\rodrigobraz\Documents\PyCharmProjects\UnittestDiscovery -s C:\Users\rodrigobraz\Documents\PyCharmProjects\UnittestDiscovery\package package.unit.test_some_unit.TestSomeUnit.test_some_unit in C:\Users\rodrigobraz\Documents\PyCharmProjects\UnittestDiscovery\package

Traceback (most recent call last):
  File "C:\Program Files\JetBrains\PyCharm Community Edition 2023.2.1\plugins\python-ce\helpers\pycharm\_jb_unittest_runner.py", line 38, in <module>
    sys.exit(main(argv=args, module=None, testRunner=unittestpy.TeamcityTestRunner,
             ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\Lib\unittest\main.py", line 101, in __init__
    self.parseArgs(argv)
  File "C:\ProgramData\anaconda3\Lib\unittest\main.py", line 127, in parseArgs
    self._do_discovery(argv[2:])
  File "C:\ProgramData\anaconda3\Lib\unittest\main.py", line 247, in _do_discovery
    self.createTests(from_discovery=True, Loader=Loader)
  File "C:\ProgramData\anaconda3\Lib\unittest\main.py", line 157, in createTests
    self.test = loader.discover(self.start, self.pattern, self.top)
                ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
  File "C:\ProgramData\anaconda3\Lib\unittest\loader.py", line 320, in discover
    raise ImportError('Start directory is not importable: %r' % start_dir)
ImportError: Start directory is not importable: 'package.unit.test_some_unit.TestSomeUnit.test_some_unit'

Process finished with exit code 1

Empty suite
0
Hello there,
This is definitely an unexpected behavior. We want to create a YouTrack bug report based on this thread for our developers to take a look at. Wouldn't you mind if we do so?
0

Sure, that sounds like the best thing to do. Thank you!

0
Thank you very much, here is the link to the report: https://youtrack.jetbrains.com/issue/PY-70800/Rerunning-a-single-test-from-a-custom-unittest-configuration-results-in-ImportError
Please vote for it to raise its relevance and receive updates regarding the fix status.
1

请先登录再写评论。