DJANGO UNITSEST:必需的模拟补丁上虚线路径会有所不同,具体取决于一个人调用/运行测试的方式

发布于 2025-02-13 03:43:08 字数 1179 浏览 0 评论 0原文

我花了几个小时才弄清楚如何修补以下代码。通往它的道路非常出乎意料。

根据我运行测试的方式以及我所在的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 or
  • foo.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 技术交流群。

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

发布评论

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

评论(2

青朷 2025-02-20 03:43:08

文件布局如下:

$ tree issue
issue
├── __init__.py
├── loaders.py
├── renders.py
├── tests
│   ├── __init__.py
│   └── test_render.py
├── run_tests.sh
└── typers.py

1 directory, 7 files

root软件包是essue,您应始终从easudy和PATCH essign.xxx.yyy中导入模块。

然后从与tests居民相同的路径上运行PYTEST(或其他一些Unitest工具)。

例如,run_tests.sh是一个shell脚本,可在tests下运行所有​​测试用例。

test_render可能是这样

# file: test_render.py

# Depending where one runs the test from, the path is different, so generate it dynamically
@mock.patch('issue.typers.load_palette', return_value=mocks.palette)
class render_rule_Tests(SimpleTestCase):
    def test_render_preset_rule(self, _):  # _ = mocked_load_palette
        ...

files layout as following:

$ tree issue
issue
├── __init__.py
├── loaders.py
├── renders.py
├── tests
│   ├── __init__.py
│   └── test_render.py
├── run_tests.sh
└── typers.py

1 directory, 7 files

the root package is issue, you should always import modules from issue, and patch issue.xxx.yyy.

then run pytest (or some other unittest tools) from the same path as tests resident.

for example, run_tests.sh is a shell script to run all test cases under tests.

and test_render may be like this

# file: test_render.py

# Depending where one runs the test from, the path is different, so generate it dynamically
@mock.patch('issue.typers.load_palette', return_value=mocks.palette)
class render_rule_Tests(SimpleTestCase):
    def test_render_preset_rule(self, _):  # _ = mocked_load_palette
        ...
暮色兮凉城 2025-02-20 03:43:08

您可以使用sys.path.insert添加“测试”目录的路径。

在“ tests/test_render.py”的顶部添加:

import sys
sys.path.insert(0, "<path/to/the/folder/tests/>")

# 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
        ...

这将在python解释器的系统路径中添加路径。从那里,Python解释器可以找到相对导入。

注意:最安全的选项是将绝对路径添加到测试文件夹中。但是,如果不可能,请添加可能的最短相对路径。

You can add the path of the "tests" directory using sys.path.insert.

In the top of "tests/test_render.py" add:

import sys
sys.path.insert(0, "<path/to/the/folder/tests/>")

# 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
        ...

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.

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