Pay*_*ian 7 python pycharm python-3.x python-unittest
我在 pycharm 中调试单元测试时遇到问题。我可以用我的配置运行它们,但是当我运行调试器时,我得到以下错误输出:
错误 回溯(最近一次调用最后一次): 文件“/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py”,第59行,在testPartExecutor 屈服 文件“/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/case.py”,第605行,运行中 测试方法() 文件“/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py”,第34行,在testFailure 引发 self._exception 导入错误:无法导入测试模块:测试 回溯(最近一次调用最后一次): 文件“/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py”,第462行,在_find_test_path 包 = self._get_module_from_name(name) 文件“/usr/local/Cellar/python3/3.6.4_2/Frameworks/Python.framework/Versions/3.6/lib/python3.6/unittest/loader.py”,第369行,_get_module_from_name __import__(名称) 文件“/Users/paymahn/solvvy/scheduler/tests/__init__.py”,第 2 行,在 导入 tests.test_setup_script 文件“/Users/paymahn/solvvy/scheduler/tests/test_setup_script.py”,第 3 行,在 导入设置 文件“/Applications/PyCharm.app/Contents/helpers/pydev/setup.py”,第 87 行,在 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] 没有这样的文件或目录:'pydevd_attach_to_process'
我的目录结构是:
我的单元测试配置是:
tests/test_setup_script.py
好像:
导入单元测试 导入操作系统 import setup # 这个文件的路径是 scheduler/setup.py。这个导入可能会破坏事情 类 TestSetupScript(unittest.TestCase): def test_load_db_connection_valid_yaml_file(self): file_name = "valid-yaml.yml" 使用 open(file_name, 'w') 作为文件: file.write(""" 测试: 你好,世界 a = b """) yaml = setup.load_yaml_configuration(file_name) # 我想调试上面的行,因此这里没有断言
有什么作用pydevd_attach_to_process
以及如何确保在调试期间找到它?问题实际上与找到的文件/目录无关吗?
实际上,发生故障是因为代码中的“导入设置”导入了设置模块,该模块是 PyCharm 调试器运行时的一部分,而不是您自己的设置模块。最简单的解决方法是将 setup.py 文件重命名为其他名称,并相应地更新代码中的导入。