UNITSESTS从命令行工作,但不工作

发布于 2025-01-22 09:28:15 字数 1775 浏览 2 评论 0原文

我正在编写一些我想在Pycharm中调试的单元测试。调试工作正常。但是,如果我想在调试模式以外运行测试,我会得到:

C:\Users\my_name\Miniconda3\envs\feature-extraction\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2021.1.1\plugins\python-ce\helpers\pycharm\_jb_unittest_runner.py" --path D:/projects/A2TD/feature_extraction/tests/reconstruction/test_misc.py

Testing started at 5:04 PM ...

Ran 1 test in 0.002s
Launching unittests with arguments python -m unittest D:/projects/A2TD/feature_extraction/tests/reconstruction/test_misc.py in D:/

FAILED (errors=1)

...
...
    module = __import__(module_name)
ModuleNotFoundError: No module named 'test_misc'

但是,我能够通过

"C:\Users\my_name\Miniconda3\envs\feature-extraction\python.exe" "C:\Program Files\JetBrains\PyCharm Community Edition 2021.1.1\plugins\python-ce\helpers\pycharm\_jb_unittest_runner.py" --path D:/projects/A2TD/feature_extraction/tests/reconstruction/test_misc.py

以下方式

python -m unittest D:/projects/A2TD/feature_extraction/tests/reconstruction/test_misc.py

在 命令行中运行测试直接从发生误差的Pycharm Run输出粘贴。

这些命令从运行到顶级项目中的任何位置时起作用。

我正在使用UNITSEST和默认运行配置,这些配置会自动在第一次运行中自动填充,例如:

“在此处输入图像说明”

具有标准项目结构,例如:

project_folder
|
|- top_level_package/
|   |
|   |- __init__.py
|   |- sub_packages/ (containing modules)
|   
|-tests/
    |- __init__.py
    |- package_to_test/
         |
         |- __init__.py
         |- test_module1.py
         |- test_module2.py

问题:

我如何获得单位测试以在调试模式外在Pycharm中运行?显然,我可以在调试模式下运行它们,但是后来我必须删除所有断点。

I'm writing some unit tests that I'd like to debug in pycharm. Debugging works just fine. However, if I want to run the tests outside of debug mode, I get:

C:\Users\my_name\Miniconda3\envs\feature-extraction\python.exe "C:\Program Files\JetBrains\PyCharm Community Edition 2021.1.1\plugins\python-ce\helpers\pycharm\_jb_unittest_runner.py" --path D:/projects/A2TD/feature_extraction/tests/reconstruction/test_misc.py

Testing started at 5:04 PM ...

Ran 1 test in 0.002s
Launching unittests with arguments python -m unittest D:/projects/A2TD/feature_extraction/tests/reconstruction/test_misc.py in D:/

FAILED (errors=1)

...
...
    module = __import__(module_name)
ModuleNotFoundError: No module named 'test_misc'

However, I am able to get them to run the tests in the command line with:

"C:\Users\my_name\Miniconda3\envs\feature-extraction\python.exe" "C:\Program Files\JetBrains\PyCharm Community Edition 2021.1.1\plugins\python-ce\helpers\pycharm\_jb_unittest_runner.py" --path D:/projects/A2TD/feature_extraction/tests/reconstruction/test_misc.py

OR this:

python -m unittest D:/projects/A2TD/feature_extraction/tests/reconstruction/test_misc.py

You will notice that both of the above commands are copied and pasted directly from the PyCharm run output where the error occurs.

These commands work when run from any location within the top level project.

I am using unittest with default run configurations which are automatically populated on first run, for example:

enter image description here

With a standard project structure like:

project_folder
|
|- top_level_package/
|   |
|   |- __init__.py
|   |- sub_packages/ (containing modules)
|   
|-tests/
    |- __init__.py
    |- package_to_test/
         |
         |- __init__.py
         |- test_module1.py
         |- test_module2.py

Question:

How do I get unit tests to run in PyCharm outside of debug mode? Obviously I could just run them in debug mode, but then I'd have to remove all of my breakpoints.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

何以笙箫默 2025-01-29 09:28:15

我的方法是将package_to_test重命名为package_to_test_t_t
在使用默认参数的Pycharm之后,开始解析软件包名称。

要在命令行上运行测试,我通常在导入软件包之前添加Unitest类(需要检查OS.Path.dirname()调用的数量,这取决于测试的目录级别。

script_directory = os.path.dirname(os.path.abspath(__file__))
projroot_directory = os.path.dirname(os.path.dirname(script_directory))
sys.path.extend([os.path.join(projroot_directory, 'top_level_package')])


project_folder
|
|- top_level_package/
|   |
|   |- __init__.py
|   |- sub_packages/ (containing modules)
|   
|-tests/
    |- __init__.py
    |- package_to_test_t/
         |
         |- __init__.py
         |- test_module1.py
         |- test_module2.py

my approach has been to rename package_to_test to package_to_test_t
After it pycharm with default params starts to resolve the package names.

To run tests on command line I usually add in unittest classes before importing my packages (need to check number of os.path.dirname() calls depending the directory levels of your test.

script_directory = os.path.dirname(os.path.abspath(__file__))
projroot_directory = os.path.dirname(os.path.dirname(script_directory))
sys.path.extend([os.path.join(projroot_directory, 'top_level_package')])


project_folder
|
|- top_level_package/
|   |
|   |- __init__.py
|   |- sub_packages/ (containing modules)
|   
|-tests/
    |- __init__.py
    |- package_to_test_t/
         |
         |- __init__.py
         |- test_module1.py
         |- test_module2.py
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文