使用 pydev 中的 unittest 在 Python 中对整个项目层次结构进行单元测试

发布于 2025-01-04 02:34:18 字数 608 浏览 6 评论 0原文

我正在使用 unittest 模块对一些使用 Pydev 在包的分层结构中创建的 python 代码进行单元测试。 当我尝试在 pydev 中对实际源代码及其单元测试使用单独的源文件夹时,问题就出现了。

project
  |----src
  |     |----com
  |     |     |----myself
  |     |     |       |----MyApplication
  |     |     |       |          |----SampleFileToTest.py  => The application file that I want to test
  |----test
  |     |----com
  |     |     |----myself
  |     |     |       |----MyApplication
  |     |     |       |          |----TestTheSampleFileToTest.py  => My test case file

当我尝试分离层次结构时,在引用测试文件中的应用程序文件时遇到问题。 是否可以采用 Junit 方式,即使用不同的源文件夹但保持相同的包名称?

I am using unittest module to unit test some python code that has been created in a hierarchical struture of packages using Pydev.
The problem arises as I try to use separate source folders for actual source code and its unit test in pydev.

project
  |----src
  |     |----com
  |     |     |----myself
  |     |     |       |----MyApplication
  |     |     |       |          |----SampleFileToTest.py  => The application file that I want to test
  |----test
  |     |----com
  |     |     |----myself
  |     |     |       |----MyApplication
  |     |     |       |          |----TestTheSampleFileToTest.py  => My test case file

As I am trying to separate the hierarchies, I am getting problems in referencing the application file in the test file.
Is it possible to go by the Junit way, i.e. using different source folders but maintaining the same package name ?

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

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

发布评论

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

评论(1

墨洒年华 2025-01-11 02:34:18

Python 本身默认不支持这一点(即:与 PyDev 无关)——我也有 Java 背景,因此,您可能需要在这里忘记一些 Java 概念 :)

在 Python 中,每当文件夹带有找到__init__.py,该包将不再在其他路径中搜索。我认为 setuptools 有一些 hackery 可以实现这一点,我隐约记得 Python 3 可能会添加一些对它的支持,但到目前为止我认为一般不建议这样做......这与 Java 方法有很大不同 - 在Python,扁平比嵌套更好 - 也许你知道,但除此之外,只是为了好玩,启动一个 Python 解释器会话并执行“导入此”:)

即:简而言之,一次 my_app/__init__.py 找到了,它不会尝试解析 PYTHONPATH 中任何其他位置的 my_app 子文件夹

因此,您有两种方法...通常我所做的是将测试靠近 _tests 包中的模块。即:

/project
/project/src
/project/src/myapp
/project/src/myapp/__init__.py
/project/src/myapp/_tests
/project/src/myapp/_tests/__init__.py
/project/src/myapp/_tests/test_myapp.py

另一种方法(我必须说我不太喜欢这种方法,因为测试“感觉”与代码更加分离),将有一个单独的测试包:

/project
/project/src
/project/src/myapp
/project/src/myapp/__init__.py
/project/src/myapp_tests/__init__.py
/project/src/myapp_tests/test_myapp.py

This is not supported by default in Python itself (i.e.: nothing to do with PyDev) -- I also come from a Java background, so, you may need to forget some of your Java concepts here :)

In Python, whenever a folder with __init__.py is found, that package will no longer be searched in other paths. I think setuptools has some hackery to make that work and I remember vaguely that Python 3 may add some support for it, but so far I don't think it's generally recommended... This differs quite a bit from the Java approach -- in Python, flat is better than nested -- maybe you know, but otherwise, just for fun, start a Python interpreter session and do 'import this' :)

I.e.: In short, once my_app/__init__.py is found, it won't try to resolve my_app subfolders in any other place in the PYTHONPATH

So, you have 2 approaches... Usually what I do is having the tests close to the module in a _tests package. I.e.:

/project
/project/src
/project/src/myapp
/project/src/myapp/__init__.py
/project/src/myapp/_tests
/project/src/myapp/_tests/__init__.py
/project/src/myapp/_tests/test_myapp.py

And the other approach (which I must say I like a little bit less as the tests 'feel' more separate from the code), would be having a separate package for tests:

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