Skip to content

Commit

Permalink
Add a Pytest for #10718 and register it as a CTest
Browse files Browse the repository at this point in the history
  • Loading branch information
jmarrec committed Dec 16, 2024
1 parent acda84e commit 98f914a
Show file tree
Hide file tree
Showing 5 changed files with 86 additions and 13 deletions.
15 changes: 15 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -228,6 +228,21 @@ if(BUILD_TESTING)
endif()
set_property(GLOBAL PROPERTY CTEST_TARGETS_ADDED 1) # This avoids all the CTest Nightly, Continuous, etc. tests.
enable_testing()

execute_process(COMMAND ${Python_EXECUTABLE} -m pytest --version
RESULT_VARIABLE _Pytest_STATUS
OUTPUT_VARIABLE Pytest_Version
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(_Pytest_STATUS AND NOT _Pytest_STATUS EQUAL 0)
message(AUTHOR_WARNING "Pytest isn't installed on your system python, so some tests won't be run. Run `${Python_EXECUTABLE} -m pip install pytest`")
set(Pytest_AVAILABLE OFF)
else()
message(VERBOSE "Found Pytest: ${Pytest_Version}")
set(Pytest_AVAILABLE ON)
endif()

endif()

if(ENABLE_REGRESSION_TESTING)
Expand Down
13 changes: 0 additions & 13 deletions idd/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -68,19 +68,6 @@ set(PREVIOUS_IDD ${PREVIOUS_IDD} PARENT_SCOPE)
install(FILES "${PREVIOUS_IDD}" DESTINATION "PreProcess/IDFVersionUpdater")

if (BUILD_TESTING)
execute_process(COMMAND ${Python_EXECUTABLE} -m pytest --version
RESULT_VARIABLE _Pytest_STATUS
OUTPUT_VARIABLE Pytest_Version
ERROR_QUIET
OUTPUT_STRIP_TRAILING_WHITESPACE
)
if(_Pytest_STATUS AND NOT _Pytest_STATUS EQUAL 0)
message(AUTHOR_WARNING "Pytest isn't installed on your system python, so some tests won't be run. Run `${Python_EXECUTABLE} -m pip install pytest`")
set(Pytest_AVAILABLE OFF)
else()
message(VERBOSE "Found Pytest: ${Pytest_Version}")
set(Pytest_AVAILABLE ON)
endif()

if (Pytest_AVAILABLE)
add_test(NAME idd_schema.test_idd_parser
Expand Down
32 changes: 32 additions & 0 deletions scripts/pytests/conftest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
from pathlib import Path

import pytest


def validate_file(arg):
if (filepath := Path(arg).expanduser()).is_file():
return filepath
else:
raise FileNotFoundError(arg)


def pytest_addoption(parser):
parser.addoption("--ep-cli-path", type=validate_file, help="Path to the E+ CLI") # , required=True


@pytest.fixture(scope="module")
def epclipath(request):
cli_path = request.config.getoption("--ep-cli-path")
if cli_path is None:
raise ValueError("You must supply --ep-cli-path [Path]")
return cli_path


@pytest.fixture(scope="module")
def testfiles_dir():
return Path(__file__).parent / "../../testfiles"


@pytest.fixture(scope="module")
def chig_epw():
return Path(__file__).parent / "../../weather/USA_IL_Chicago-OHare_TMY2.epw"
32 changes: 32 additions & 0 deletions scripts/pytests/test_xml_output.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
import re
import subprocess
from pathlib import Path

import pytest


@pytest.fixture
def prepare_idf_with_xml(tmp_path: Path, testfiles_dir: Path): # tmp_path is a built-in pytest fixture
RE_TABLE = re.compile(r"(?P<Prefix>OutputControl:Table:Style.*)HTML;", re.DOTALL)
idf_file_path = testfiles_dir / "DXCoilSystemAuto.idf"
assert idf_file_path.is_file()
content = idf_file_path.read_text()
content = RE_TABLE.sub(r"\g<1>XMLandHTML;", content)
mod_idf_file_path = tmp_path / "in.idf"
mod_idf_file_path.write_text(content)
return mod_idf_file_path


def test_xml_with_standard_ratings_is_valid(epclipath, prepare_idf_with_xml, chig_epw):
lxml = pytest.importorskip("lxml", reason="lxml not found, please `pip install lxml`")
from lxml import etree

subprocess.check_call(
[str(epclipath), "-w", str(chig_epw)],
stdout=subprocess.DEVNULL,
stderr=subprocess.STDOUT,
cwd=prepare_idf_with_xml.parent,
)
xml_out_path = prepare_idf_with_xml.parent / "eplustbl.xml"
assert xml_out_path.is_file()
root = lxml.etree.fromstring(xml_out_path.read_text(encoding="latin-1"))
7 changes: 7 additions & 0 deletions src/EnergyPlus/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1159,6 +1159,13 @@ if(BUILD_TESTING)
TIMEOUT 10 # This used to timeout! and we expect it NOT to
)
endif()

if(Pytest_AVAILABLE)
add_test(NAME energyplus.TabularXML_IsValid
COMMAND ${Python_EXECUTABLE} -m pytest --verbose --ep-cli-path $<TARGET_FILE:energyplus> "${PROJECT_SOURCE_DIR}/scripts/pytest/test_xml_output.py"
)
endif()

endif()

if(UNIX AND NOT APPLE)
Expand Down

0 comments on commit 98f914a

Please sign in to comment.