DJANGO UNITSEST:必需的模拟补丁上虚线路径会有所不同,具体取决于一个人调用/运行测试的方式
我花了几个小时才弄清楚如何修补以下代码。通往它的道路非常出乎意料。
根据我运行测试的方式以及我所在的DIR,我找到了通往模块更改的虚线路径。这真的很糟糕。这让我认为我做错了。
与代码相关的文件结构是:
loaders.py <-- Has a load_palette() func required to be patched
typers.py <-- Has `from . loaders import load_palette`, and calls load_palette()
render.py <-- Has a func that calls the typers func
tests/test_render.py <-- Tests for render which calls a func in render, which calls a func in typers, which calls load_palette()
下面的代码中__软件包。
- 在 > bar.tests 或
foo.bar.tests
- 或其他东西
,并相对构建虚线的途径,以便是正确的。这似乎很刺眼。应该如何保护防止此类问题?
理想情况下,虚线路径将是.. typers.load_palette
,但它不接受相对虚线路径。
这是实际的代码:
# file: test_render.py
# Depending where one runs the test from, the path is different, so generate it dynamically
@mock.patch(__package__.replace('.tests', '.typers.load_palette'), return_value=mocks.palette)
class render_rule_Tests(SimpleTestCase):
def test_render_preset_rule(self, _): # _ = mocked_load_palette
...
It took me hours to figure out how to patch the below code. The path to it was very much unexpected.
Depending on how I run the tests, and which dir I am in, I find the dotted path to the module to patch changes. This is really bad for unittesting. Which makes me think I am doing it wrong.
The file structure related to the code is:
loaders.py <-- Has a load_palette() func required to be patched
typers.py <-- Has `from . loaders import load_palette`, and calls load_palette()
render.py <-- Has a func that calls the typers func
tests/test_render.py <-- Tests for render which calls a func in render, which calls a func in typers, which calls load_palette()
In the code below __package__.replace('.tests', '.typers.load_palette')
takes the current path to the current package which could be:
bar.tests
orfoo.bar.tests
- or something else
and builds the dotted path relatively so that is is correct. This seems very hackish. How is one supposed to safe guard against these kind of issues?
Ideally the dotted path would be ..typers.load_palette
but it did not accept the relative dotted path.
Heres the actual code:
# file: test_render.py
# Depending where one runs the test from, the path is different, so generate it dynamically
@mock.patch(__package__.replace('.tests', '.typers.load_palette'), return_value=mocks.palette)
class render_rule_Tests(SimpleTestCase):
def test_render_preset_rule(self, _): # _ = mocked_load_palette
...
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
文件布局如下:
root软件包是
essue
,您应始终从easudy
和PATCHessign.xxx.yyy
中导入模块。然后从与
tests
居民相同的路径上运行PYTEST
(或其他一些Unitest工具)。例如,
run_tests.sh
是一个shell脚本,可在tests
下运行所有测试用例。test_render
可能是这样files layout as following:
the root package is
issue
, you should always import modules fromissue
, and patchissue.xxx.yyy
.then run
pytest
(or some other unittest tools) from the same path astests
resident.for example,
run_tests.sh
is a shell script to run all test cases undertests
.and
test_render
may be like this您可以使用sys.path.insert添加“测试”目录的路径。
在“ tests/test_render.py”的顶部添加:
这将在python解释器的系统路径中添加路径。从那里,Python解释器可以找到相对导入。
注意:最安全的选项是将绝对路径添加到测试文件夹中。但是,如果不可能,请添加可能的最短相对路径。
You can add the path of the "tests" directory using sys.path.insert.
In the top of "tests/test_render.py" add:
This will add the path in system paths where python interpreter. From there, the python interpreter can locate the relative imports.
Note: The safest option would be to add the absolute path to the tests folder. However, if it's not possible, add the shortest relative path possible.