如何解决E0611:模块“test”中没有名称“Test_HC”(模块中没有名称)?

问题描述 投票:0回答:1

背景

我有一个名为

something
的 pip 包,具有树结构:

src/something/__main__.py
src/something/__init__.py
src/something/hardcoded.py
src/something/bike/ride.py

test/__init__.py
test/bike/test_ride.py
test/Test_HC.py

Hardcoded_testdata
中有一个名为
Test_HC.py
的类。 当我从预提交运行 pylint 时:

# Performs static code analysis to check for programming errors.
 - repo: local
   hooks:
     - id: pylint
       name: pylint
       entry: pylint
       language: system
       types: [python]
       args:
         [
          #  "--init-hook='import sys; sys.path.append(\"/home/name/git/something/test/\")'",
           "--init-hook='from pylint.config import find_pylintrc; import os, sys; sys.path.append(os.path.dirname(find_pylintrc()))'",
          #  "--init-hook='from pylint.config import find_pylintrc; import os, sys; sys.path.append(os.path.dirname(find_pylintrc()))'"
           "-rn", # Only display messages
           "-sn", # Don't display the score
         ]
       exclude: test/test_files/

我收到错误:

test/bike/test_ridepy:4:0: E0611: No name 'Test_HC' in module 'test' (no-name-in-module)

进口时:

from test.hardcoded_testdata import TestHC

查看 flake8,标准做法似乎是不将测试文件包含在 pip 包中,因此将它们放在存储库根目录下的单独文件夹(名为

tests
)中。 因此,我也希望不要用用于测试的辅助文件污染我的 pip 包源代码(如
hardcoded_testdata.py
)。

方法

我尝试的第一个解决方案是“告诉 pylint 寻找

hardcoded_testdata.py
”。所以在应用 this 后,答案如下:

# Performs static code analysis to check for programming errors.
 - repo: local
   hooks:
     - id: pylint
       name: pylint
       entry: pylint
       language: system
       types: [python]
       args:
         [
          #  "--init-hook='import sys; sys.path.append(\"/home/name/git//something/test/\")'",
           "--init-hook='from pylint.config import find_pylintrc; import os, sys; sys.path.append(os.path.dirname(find_pylintrc()))'",
          #  "--init-hook='from pylint.config import find_pylintrc; import os, sys; sys.path.append(os.path.dirname(find_pylintrc()))'"
           "-rn", # Only display messages
           "-sn", # Don't display the score
         ]
       exclude: test/test_files/

我仍然收到错误。我什至尝试对文件路径进行硬编码(这是不可取的,因为我希望其他开发人员也能够在这个项目中工作),但这也不起作用。

我还将

__init__.py
文件包含在
/test/
目录中,但是,这并不能解决问题。

当我运行

python -m pytest
时,它会完美导入文件。

问题

如何确保 pylint 能够找到

test/hardcoded_testdata.py
文件和/或解决 E0611 错误?

python pylint
1个回答
0
投票

必须进行一些更改才能解决该问题:

  • test/
    重命名为
    tests/
    ,(并将导入相应地从
    from test.Test_HC import ...etc
    重构为
    from tests.Test_HC import ...etc
    ,然后错误得到解决。

  • 按照评论中的建议包含测试/conftest.py 文件。

  • 在包含测试文件的所有测试目录中添加了

    __init__.py

  • 确保所有测试类名称都是唯一的。

最后,我按照评论中的建议将硬编码的测试数据移动到固定装置中:

@pytest.fixture  # type:ignore[misc]
def hardcoded_testdata() -> HardcodedTestdata:
    """This is a fixture.

    That means it is an object that can be used as a parameter to a test
    function. It is a way to share data between tests, without having to
    create files that do not adhere to the test_<name>.py naming
    convention.
    """
    hc_testdata: HardcodedTestdata = HardcodedTestdata()

    # Any setup logic can go here
    return hc_testdata  # The fixture provides this data to the tests

然后用作:

class Test_something(unittest.TestCase):
    """Object used to test something returned
    successfully."""

    # Initialize test object
    @typechecked
    def __init__(  # type: ignore[no-untyped-def]
        self, hardcoded_testdata: HardcodedTestdata, *args, **kwargs
    ):
        super().__init__(*args, **kwargs)
        self.current_directory: str = os.getcwd()
        self.hardcoded_testdata: HardcodedTestdata=hardcoded_testdata


© www.soinside.com 2019 - 2024. All rights reserved.