Feature/component test fixture (#1142)

This commit is contained in:
Peter Kuehne 2020-07-15 13:04:00 +01:00 committed by GitHub
parent d5c59292c8
commit 3ec9bcaed6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 54 additions and 12 deletions

View File

@ -1,24 +1,43 @@
""" Tests for the binary sensor component """ """ Tests for the binary sensor component """
from esphome.core import CORE
from esphome.config import read_config def test_binary_sensor_is_setup(generate_main):
from esphome.__main__ import generate_cpp_contents """
When the binary sensor is set in the yaml file, it should be registered in main
"""
# Given
# When
main_cpp = generate_main("tests/component_tests/binary_sensor/test_binary_sensor.yaml")
# Then
assert "new gpio::GPIOBinarySensor();" in main_cpp
assert "App.register_binary_sensor" in main_cpp
def test_binary_sensor_config_value_internal_set(): def test_binary_sensor_sets_mandatory_fields(generate_main):
"""
When the mandatory fields are set in the yaml, they should be set in main
"""
# Given
# When
main_cpp = generate_main("tests/component_tests/binary_sensor/test_binary_sensor.yaml")
# Then
assert "bs_1->set_name(\"test bs1\");" in main_cpp
assert "bs_1->set_pin(new GPIOPin" in main_cpp
def test_binary_sensor_config_value_internal_set(generate_main):
""" """
Test that the "internal" config value is correctly set Test that the "internal" config value is correctly set
""" """
# Given # Given
CORE.config_path = "tests/component_tests/binary_sensor/test_binary_sensor.yaml"
CORE.config = read_config({})
# When # When
generate_cpp_contents(CORE.config) main_cpp = generate_main("tests/component_tests/binary_sensor/test_binary_sensor.yaml")
# print(CORE.cpp_main_section)
# Then # Then
assert "bs_1->set_internal(true);" in CORE.cpp_main_section assert "bs_1->set_internal(true);" in main_cpp
assert "bs_2->set_internal(false);" in CORE.cpp_main_section assert "bs_2->set_internal(false);" in main_cpp
CORE.reset()

View File

@ -0,0 +1,23 @@
""" Fixtures for component tests """
import pytest
from esphome.core import CORE
from esphome.config import read_config
from esphome.__main__ import generate_cpp_contents
@pytest.fixture
def generate_main():
""" Generates the C++ main.cpp file and returns it in string form """
def generator(path: str) -> str:
CORE.config_path = path
CORE.config = read_config({})
generate_cpp_contents(CORE.config)
print(CORE.cpp_main_section)
return CORE.cpp_main_section
yield generator
CORE.reset()