当前位置: 首页 > 知识库问答 >
问题:

无法在Pycharm中调试单元测试

茅涵映
2023-03-14

我在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'

我的目录结构是:

我的unittest配置是:

测试/测试设置脚本。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\u attach\u to\u进程做什么?如何确保在调试期间找到它?问题实际上与正在查找的文件/目录无关吗?

共有1个答案

南宫海超
2023-03-14

事实上,失败是因为代码中的“导入设置”导入了作为PyCharm调试器运行时一部分的设置模块,而不是您自己的设置模块。最简单的修复方法是重命名您的设置。py文件,并相应地更新代码中的导入。

 类似资料:
  • 问题内容: 我正在尝试为我的项目编写一个单元测试,但是它不允许我使用配置管理器。现在我的项目像 ASP.Net应用程序(所有aspx页) ProjectCore(所有C#文件-模型) ProjectTest(所有测试) 在我的ProjectCore中,我可以从System.Configuration访问ConfigurationManager对象,并将信息传递到项目中。但是,当我运行涉及Confi

  • 问题内容: 假设我在Python单元测试中具有以下代码: 有没有一种简单的方法可以断言在测试的第二行期间调用了特定方法(在我的情况下)?例如是否有这样的事情: 问题答案: 我为此使用Mock(在py3.3 +上现在是unittest.mock): 对于您的情况,它可能看起来像这样: Mock支持许多有用的功能,包括修补对象或模块的方式以及检查是否调用了正确的东西等。 买者自负! (请当心!) 如果

  • 我正在尝试测试我的PreUpdateEventListener流,但我似乎无法使其在JUnit测试中工作。我没有收到任何错误,但没有调用代码。 我的PreUpdateEventListener: 测试: 我曾尝试将SessionFactory注入测试并调用SessionFactory#flush方法,但这不会引发CurrentContextSession错误,我似乎无法修复。

  • 在我的测试中,我在尝试在我正在测试的函数中打回调函数时,很难得到完整的覆盖。这里是功能: 因此,除了函数回调之外,我已经覆盖了函数: 我似乎不知道如何通过测试来达到这个目的。 在上面,我把请求像这样删掉了 所以我不确定如何才能让它点击回调并测试它。可能需要一些洞察力,因为这对我来说还是新的。谢谢 所以我开始尝试一个简单的测试-

  • 问题内容: 我正在创建一个Java单元测试来测试一些我最近更改的代码。但是,我正在测试的方法实例化了一个使用ResourceBundle的类…… 资源文件位于Web包中,位于 我的测试保存在我的xml包中 在正常运行时,资源束是可访问的,但在运行单元测试时则不可访问。它引发此错误…… 我该怎么办?我可以启用测试以某种方式查看资源包吗?我可以在某个地方以某种方式看到代码的模拟资源文件吗? 问题答案: