繁体   English   中英

无法在 Pycharm 中调试单元测试

[英]Can't debug unittests in Pycharm

我在 pycharm 中调试单元测试时遇到问题。 我可以用我的配置运行它们,但是当我运行调试器时,我得到以下错误输出:

Error
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 59, in testPartExecutor
    yield
  File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py", line 605, in run
    testMethod()
  File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 34, in testFailure
    raise self._exception
ImportError: Failed to import test module: tests
Traceback (most recent call last):
  File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 462, in _find_test_path
    package = self._get_module_from_name(name)
  File "/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py", line 369, in _get_module_from_name
    __import__(name)
  File "/Users/paymahn/solvvy/scheduler/tests/__init__.py", line 2, in 
    import tests.test_setup_script
  File "/Users/paymahn/solvvy/scheduler/tests/test_setup_script.py", line 3, in 
    import setup
  File "/Applications/PyCharm.app/Contents/helpers/pydev/setup.py", line 87, in 
    data_files.append(('pydevd_attach_to_process', [os.path.join('pydevd_attach_to_process', f) for f in os.listdir('pydevd_attach_to_process') if accept_file(f)]))
FileNotFoundError: [Errno 2] No such file or directory: 'pydevd_attach_to_process'

我的目录结构是:

在此处输入图片说明

我的单元测试配置是:

在此处输入图片说明

tests/test_setup_script.py看起来像:

import unittest
import os
import setup # the path to this file is scheduler/setup.py. This import may be breaking things


class TestSetupScript(unittest.TestCase):


    def test_load_db_connection_valid_yaml_file(self):
        file_name = "valid-yaml.yml"
        with open(file_name, 'w') as file:
            file.write("""
            test:
                hello world
                a = b
                """)

        yaml = setup.load_yaml_configuration(file_name)
        # I want to debug the line above, hence no assertions here

pydevd_attach_to_process什么作用,如何确保在调试过程中找到它? 问题实际上与找到的文件/目录无关吗?

实际上,发生故障是因为代码中的“导入设置”导入了设置模块,该模块是 PyCharm 调试器运行时的一部分,而不是您自己的设置模块。 最简单的解决方法是将 setup.py 文件重命名为其他名称,并相应地更新代码中的导入。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM