1

I'm writing tests for a Home Assistant integration using tests from another integration as an example. There is a method that is called from each test function that is some how calling patch with two positional arguments:

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

The first positional argument "homeassistant.components.ring.PLATFORMS" is obviously a string but is pointing to the constant PLATFORMS which is a tuple ("binary_sensor", "light", "sensor", "switch", "camera"). The second positional argument platform is passed in setup_platform as a string that will be either "binary_sensor", "light", "sensor", "switch" or "camera" (same items as the PLATFORMS tuple), but gets passed into patch as a dictionary with one string item.

My understanding of what's happening is this results in only the platform passed being patched.

What I can't figure out is how two positional arguments can be passed into patch when it only accepts one positional argument. Are the two positional arguments some how being combined into one before patch is called?

shred
  • 13
  • 3
  • According to the documentation https://docs.python.org/3.7/library/unittest.mock.html#unittest.mock.patch path accepts more than one argument. The second position argument to patch is new, the third is spec, etc. – lorg Mar 09 '20 at 04:11

1 Answers1

0

In Python, the other arguments are just getting default values, but they are still positional arguments.

lorg
  • 1,160
  • 1
  • 10
  • 27
  • Well, that explains that, lol. Thanks. I knew that at one point but some how convinced myself keyword arguments required a key. So in this case, a list with one string item `[platform]` is being passed as the `new` key argument in unittest.patch: `unittest.mock.patch(target, new=DEFAULT, spec=None, create=False, spec_set=None, autospec=None, new_callable=None, **kwargs)` I suppose now i just need to figure out how a list works with the `new` keyword argument in unittest.patch. – shred Mar 09 '20 at 05:20