mirror of
https://github.com/home-assistant/core.git
synced 2025-11-09 02:49:40 +00:00
Pytest tests (#17750)
* Convert core tests
* Convert component tests to use pytest assert
* Lint 🤷♂️
* Fix test
* Fix 3 typos in docs
This commit is contained in:
@@ -7,6 +7,7 @@ from tempfile import mkdtemp
|
||||
from homeassistant.util.json import (SerializationError,
|
||||
load_json, save_json)
|
||||
from homeassistant.exceptions import HomeAssistantError
|
||||
import pytest
|
||||
|
||||
# Test data that can be saved as JSON
|
||||
TEST_JSON_A = {"a": 1, "B": "two"}
|
||||
@@ -38,7 +39,7 @@ class TestJSON(unittest.TestCase):
|
||||
fname = self._path_for("test1")
|
||||
save_json(fname, TEST_JSON_A)
|
||||
data = load_json(fname)
|
||||
self.assertEqual(data, TEST_JSON_A)
|
||||
assert data == TEST_JSON_A
|
||||
|
||||
# Skipped on Windows
|
||||
@unittest.skipIf(sys.platform.startswith('win'),
|
||||
@@ -48,9 +49,9 @@ class TestJSON(unittest.TestCase):
|
||||
fname = self._path_for("test2")
|
||||
save_json(fname, TEST_JSON_A, private=True)
|
||||
data = load_json(fname)
|
||||
self.assertEqual(data, TEST_JSON_A)
|
||||
assert data == TEST_JSON_A
|
||||
stats = os.stat(fname)
|
||||
self.assertEqual(stats.st_mode & 0o77, 0)
|
||||
assert stats.st_mode & 0o77 == 0
|
||||
|
||||
def test_overwrite_and_reload(self):
|
||||
"""Test that we can overwrite an existing file and read back."""
|
||||
@@ -58,12 +59,12 @@ class TestJSON(unittest.TestCase):
|
||||
save_json(fname, TEST_JSON_A)
|
||||
save_json(fname, TEST_JSON_B)
|
||||
data = load_json(fname)
|
||||
self.assertEqual(data, TEST_JSON_B)
|
||||
assert data == TEST_JSON_B
|
||||
|
||||
def test_save_bad_data(self):
|
||||
"""Test error from trying to save unserialisable data."""
|
||||
fname = self._path_for("test4")
|
||||
with self.assertRaises(SerializationError):
|
||||
with pytest.raises(SerializationError):
|
||||
save_json(fname, TEST_BAD_OBJECT)
|
||||
|
||||
def test_load_bad_data(self):
|
||||
@@ -71,5 +72,5 @@ class TestJSON(unittest.TestCase):
|
||||
fname = self._path_for("test5")
|
||||
with open(fname, "w") as fh:
|
||||
fh.write(TEST_BAD_SERIALIED)
|
||||
with self.assertRaises(HomeAssistantError):
|
||||
with pytest.raises(HomeAssistantError):
|
||||
load_json(fname)
|
||||
|
||||
Reference in New Issue
Block a user