Core: Rewrite smtp unittest tests to pytest style test functions

Created on 1 Oct 2020  ·  10Comments  ·  Source: home-assistant/core

Problem

The smtp integration still contains unittest.TestCase based unit tests. We want to rewrite them to standalone pytest test functions.

Background

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:

https://github.com/home-assistant/core/blob/4cce724473233d4fb32c08bd251940b1ce2ba570/tests/components/tradfri/test_light.py#L156-L176

There are many pytest fixtures to help writing the tests. See:

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.

Prerequisites

Task

  • Rewrite the tests one module at a time and submit the changes as a pull request to this repository.
  • We want to limit the change scope to a single module to not have the pull request be too long, which would take longer time to review.
  • Remember to reference this issue in your pull request, but don't close or fix the issue until all tests for the integration are updated.
Hacktoberfest Help wanted smtp unittest.TestCase

All 10 comments

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.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

sogeniusio picture sogeniusio  ·  3Comments

Konstigt picture Konstigt  ·  3Comments

moskovskiy82 picture moskovskiy82  ·  3Comments

kirichkov picture kirichkov  ·  3Comments

Elmardus picture Elmardus  ·  3Comments