在 pydev 中使用 unittest 在 Python 中对整个项目层次结构进行单元测试 [英] Unit testing entire project hierarchy in Python using unittest in pydev

查看:45
本文介绍了在 pydev 中使用 unittest 在 Python 中对整个项目层次结构进行单元测试的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

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

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 ?

推荐答案

Python 本身默认不支持这个(即:与 PyDev 无关)——我也是 Java 背景的,所以,你可能需要在这里忘记你的一些 Java 概念:)

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 :)

在 Python 中,每当找到带有 __init__.py 的文件夹时,将不再在其他路径中搜索该包.我认为 setuptools 有一些技巧可以使它工作,我依稀记得 Python 3 可能会添加一些对它的支持,但到目前为止,我认为通常不推荐它......这与 Java 方法有很大不同——在Python,扁平比嵌套更好——也许你知道,但除此之外,只是为了好玩,启动一个 Python 解释器会话并执行导入":)

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' :)

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

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

所以,你有 2 种方法......通常我所做的是让测试靠近 _tests 包中的模块.即:

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

这篇关于在 pydev 中使用 unittest 在 Python 中对整个项目层次结构进行单元测试的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆