当它只接受一个时,如何将两个位置参数传递给 unittest.patch?
How is it possible to pass two positional arguments to unittest.patch when it only accepts one?
我正在为从每个测试函数调用的 Home Assistant integration using tests from another integration as an example. There is a method 编写测试,它是使用两个位置参数调用 patch
的一些方式:
async def setup_platform(hass, platform):
"""Set up the ring platform and prerequisites."""
MockConfigEntry(domain=DOMAIN, data={"username": "foo", "token": {}}).add_to_hass(
hass
)
with patch("homeassistant.components.ring.PLATFORMS", [platform]):
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
第一个位置参数 "homeassistant.components.ring.PLATFORMS"
显然是一个字符串,但指向常量 PLATFORMS
,它是一个元组 ("binary_sensor", "light", "sensor", "switch", "camera")
。第二个位置参数 platform
在 setup_platform
中作为字符串传递,它将是 "binary_sensor"
、"light"
、"sensor"
、"switch"
或 "camera"
(与 PLATFORMS
元组相同的项),但作为包含一个字符串项的字典传递到 patch
。
我对发生的事情的理解是这导致只有 platform
通过被修补。
我不明白的是,当 patch
只接受 one positional argument 时,如何将两个位置参数传递给 patch
。在调用 patch
之前,这两个位置参数是如何合并为一个的?
在Python中,其他参数只是获取默认值,但它们仍然是位置参数。
我正在为从每个测试函数调用的 Home Assistant integration using tests from another integration as an example. There is a method 编写测试,它是使用两个位置参数调用 patch
的一些方式:
async def setup_platform(hass, platform):
"""Set up the ring platform and prerequisites."""
MockConfigEntry(domain=DOMAIN, data={"username": "foo", "token": {}}).add_to_hass(
hass
)
with patch("homeassistant.components.ring.PLATFORMS", [platform]):
assert await async_setup_component(hass, DOMAIN, {})
await hass.async_block_till_done()
第一个位置参数 "homeassistant.components.ring.PLATFORMS"
显然是一个字符串,但指向常量 PLATFORMS
,它是一个元组 ("binary_sensor", "light", "sensor", "switch", "camera")
。第二个位置参数 platform
在 setup_platform
中作为字符串传递,它将是 "binary_sensor"
、"light"
、"sensor"
、"switch"
或 "camera"
(与 PLATFORMS
元组相同的项),但作为包含一个字符串项的字典传递到 patch
。
我对发生的事情的理解是这导致只有 platform
通过被修补。
我不明白的是,当 patch
只接受 one positional argument 时,如何将两个位置参数传递给 patch
。在调用 patch
之前,这两个位置参数是如何合并为一个的?
在Python中,其他参数只是获取默认值,但它们仍然是位置参数。