The smtp integration still contains unittest.TestCase based unit tests. We want to rewrite them to standalone pytest test functions.
The Home Assistant core standard is to write tests as standalone pytest test functions. We still have some old tests that are based on unittest.TestCase. We want all these tests to be rewritten as pytest test functions.
Here are the docs for pytest: https://docs.pytest.org/en/stable/
Here's an example of an async pytest test function in Home Assistant core:
There are many pytest fixtures to help writing the tests. See:
hass which will set up a HomeAssistant instance and start it.Here's an example of a pull request that rewrote a module of unittest.TestCase tests to standalone pytest test functions:
https://github.com/home-assistant/core/pull/40749
Here's an example command to run a single test module with pytest inside tox on Python 3.8:
tox -e py38 -- --cov-report term-missing --cov=homeassistant.components.command_line.switch tests/components/command_line/test_switch.py
It will print coverage information with lines that are missing coverage.
Hello @MartinHjelmare I want to work on this issue. I have already cloned the project and set up the prerequisites required for working on this project, also I have started with the test cases.
May I carry forward here?
Yes, please go ahead. 👍
Hello @MartinHjelmare I am facing some issue with mocking in pytest. I was using pytest mocker. But it reads fixture not available. Though I have installed pytest-mock using pip but issue still persists. What is the best alternative you are using in your project for mocker fixture and also please share a document for me to gather an understanding on same.
We use the standard library unittest.mock module together with asynctest library. Make sure to import the mock helpers from tests.async_mock.
https://github.com/home-assistant/core/blob/dev/tests/async_mock.py
You can see examples of using mocks together with pytest fixtures in the tradfri light test module linked above.
Here's the standard library documentation:
https://docs.python.org/3/library/unittest.mock.html
@MartinHjelmare I did write the test cases using Pytest style test functions. I also checked the code based on style linters.
I installed the project based on the remote container of vs code approach. I also created a new branch. However, when I try to commit my code, I am facing this issue.
` /usr/bin/env: ‘python3.6’: No such file or directory
git config --get-all user.name
git config --get-all user.email`
Try setting your global git config first in your normal environment.
This is already set for me in both normal environment and dev container environment.
Is there something like git hooks in the project which I need to set?
We do use pre-commit. But I don't think that should be a problem.
I just tried using the dev container on Ubuntu 20.04 and Python 3.8 and that didn't present problems when committing. I didn't need to set anything up after launching the container, besides selecting Python environment. I already had my global git config set and available.
Hello @MartinHjelmare, I did resolve this. I was able to commit. I created a pull request. Please review. Thanks for the opportunity. It was great contributing on this project. I learned lot of new things.