-
-
Notifications
You must be signed in to change notification settings - Fork 28.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve fyta tests #117661
Improve fyta tests #117661
Conversation
Co-authored-by: Joost Lekkerkerker <joostlek@outlook.com>
tests/components/fyta/conftest.py
Outdated
with ( | ||
patch( | ||
"homeassistant.components.fyta.FytaConnector", | ||
return_value=mock_fyta_connector, | ||
), | ||
patch( | ||
"homeassistant.components.fyta.config_flow.FytaConnector", | ||
return_value=mock_fyta_connector, | ||
), | ||
): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can you check analytics insights on how we do it there? That also makes sure the object is actually completely specced to the class it's mocking, which makes it a bit better to use and raises errors if something is not mocked properly
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
with (
patch(
"homeassistant.components.analytics_insights.HomeassistantAnalyticsClient",
autospec=True,
) as mock_client,
patch(
"homeassistant.components.analytics_insights.config_flow.HomeassistantAnalyticsClient",
new=mock_client,
),
):
do you only mean autospec=True
?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
and new=..
, but because the mock_client comes from as mock_client
, the mock is automatically autospecced
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For my understanding: new=mock_client
you use, if you refer to as mock_client
above. However, if you define mock_client
before the with/patch-clause, one uses return_value=mock_client
(which makes new=
obsolete). Correct?
tests/components/fyta/test_init.py
Outdated
with patch( | ||
"homeassistant.components.fyta.config_flow.FytaConfigFlow.async_step_reauth", | ||
return_value={ | ||
"type": FlowResultType.ABORT, | ||
"flow_id": "mock_flow", | ||
"step_id": "reauth_confirm", | ||
"reason": "reauth_successful", | ||
}, | ||
) as mock_async_step_reauth: | ||
await setup_platform(hass, mock_config_entry, [SENSOR_DOMAIN]) | ||
await hass.async_block_till_done() |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We should not patch the config flow, we should just assert this sets the state of the entry to SETUP_ERROR and starts a new flow
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Got this from
core/tests/components/abode/test_init.py
Line 73 in 3efdeaa
patch( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I see assert mock_config_entry.state is ConfigEntryState.SETUP_ERROR
.
How do I test if a new flow is started? Or is the first sufficient?
Please take a look at the requested changes, and use the Ready for review button when you are done, thanks 👍 |
Proposed change
Add tests for init.py, remove file from .coveragerc
Type of change
Additional information
Checklist
ruff format homeassistant tests
)If user exposed functionality or configuration variables are added/changed:
- [ ] Documentation added/updated for www.home-assistant.ioIf the code communicates with devices, web services, or third-party tools:
- [ ] The manifest file has all fields filled out correctly.Updated and included derived files by running:
python3 -m script.hassfest
.- [ ] New or updated dependencies have been added torequirements_all.txt
.Updated by running
python3 -m script.gen_requirements_all
.- [ ] For the updated dependencies - a link to the changelog, or at minimum a diff between library versions is added to the PR description..coveragerc
.To help with the load of incoming pull requests: