使用 src/package 目录结构运行单元测试

发布于 2025-01-20 00:16:32 字数 1902 浏览 2 评论 0原文

这个问题与这个问题类似,但是我的目录结构略有不同不同,我认为这就是问题所在。

我的结构如下:

project/
    src/
        mypackage/
            module.py
    tests/
        tests_module.py
    pyproject.toml
    setup.cfg
    etc.

主要区别是我的包位于 src/ 下的子目录中。我读过一些文章说这是一个很好的做法,因为它本质上会“强制”您安装软件包来测试它,就像用户一样......但这就是我似乎陷入困境的地方。

当我尝试进行单元测试时,会发生以下情况:

% python -m unittest tests_module
E
======================================================================
ERROR: tests_module (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests_module
Traceback (most recent call last):
  File "/usr/local/Cellar/[email protected]/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "project/tests/tests_module.py", line 3, in <module>
    from mypackage.module import func
ModuleNotFoundError: No module named 'mypackage'


----------------------------------------------------------------------
Ran 1 test in 0.000s

这是我的 tests_module.py 文件:(

import unittest

from mypackage.module import func


class TestCore(unittest.TestCase):

    def test_func(self):
        """
        Test that func returns the parameter it receives
        """
        self.assertEqual(func(1), 1)


if __name__ == '__main__':
    unittest.main()

测试文件可能有其他问题,这只是一个开始,但现在我需要修复导入问题)。

我发现的一些解决方案建议在 setup.py 中放入一些内容来安装用于此目的的软件包,但我没有 setup.py 因为我似乎是印象中这是一种指定设置信息的“旧”方式,我试图避免它。

非常感谢任何有关最佳实践的帮助或建议!

This question is similar to this one, but my directory structure is slightly different and I think that's causing the problem.

My structure is as follows:

project/
    src/
        mypackage/
            module.py
    tests/
        tests_module.py
    pyproject.toml
    setup.cfg
    etc.

The key difference being I have my package in a subdirectory under src/. I've read a few things saying this is good practice as it will essentially "force" you to install the package to test it, just like a user... but that is where I seem to be stuck.

When I try to do my unit test the following happens:

% python -m unittest tests_module
E
======================================================================
ERROR: tests_module (unittest.loader._FailedTest)
----------------------------------------------------------------------
ImportError: Failed to import test module: tests_module
Traceback (most recent call last):
  File "/usr/local/Cellar/[email protected]/3.9.12/Frameworks/Python.framework/Versions/3.9/lib/python3.9/unittest/loader.py", line 154, in loadTestsFromName
    module = __import__(module_name)
  File "project/tests/tests_module.py", line 3, in <module>
    from mypackage.module import func
ModuleNotFoundError: No module named 'mypackage'


----------------------------------------------------------------------
Ran 1 test in 0.000s

And here is my tests_module.py file:

import unittest

from mypackage.module import func


class TestCore(unittest.TestCase):

    def test_func(self):
        """
        Test that func returns the parameter it receives
        """
        self.assertEqual(func(1), 1)


if __name__ == '__main__':
    unittest.main()

(the test file might have other problems, it's just a start, but right now I need to fix the import issue).

A few solutions I have found suggest putting something in setup.py that will install the package for this purpose, but I don't have setup.py because I seem to be under the impression that's kind of an "old" way to specify setup information, and I am trying to avoid it.

Any help or advice on best practices greatly appreciated!

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

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

发布评论

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

评论(1

影子的影子 2025-01-27 00:16:32

我想我可能已经找到了答案...

我想我需要做的是以可编辑的形式安装我的包(使用 python -m pip install -e 。)

之后测试运行良好!我认为这就是人们所说的“它迫使你安装软件包来测试它”的意思。

I think I may have found an answer...

I think what I needed to do was install my package in editable form (using python -m pip install -e .)

The tests ran fine after that! I think this is what people meant by "it forces you to install the package to test it."

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文