Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
RWTH-EBC
GitHub Repository: RWTH-EBC/ebcpy
Path: blob/master/tests/test_examples.py
505 views
1
"""Test-module for all examples"""
2
import os
3
import sys
4
import unittest
5
import pathlib
6
import importlib
7
8
9
class TestExample(unittest.TestCase):
10
11
def setUp(self) -> None:
12
self.timeout = 10 # Seconds which the script is allowed to run
13
14
def _run_example(self, file: str, func_name: str, **kwargs):
15
module_name = f'examples_test'
16
file = pathlib.Path(__file__).parents[1].joinpath('examples', file)
17
# Custom file import
18
spec = importlib.util.spec_from_file_location(module_name, file)
19
custom_module = importlib.util.module_from_spec(spec)
20
sys.modules.update({module_name: custom_module})
21
spec.loader.exec_module(custom_module)
22
assert func_name in custom_module.__dict__.keys(), \
23
f"Given filepath {file} does not contain the specified function {func_name}"
24
25
test_func = custom_module.__dict__.get(func_name)
26
test_func(**kwargs)
27
28
def test_tst_example(self):
29
"""Execute e1_time_series_data_example.py"""
30
self._run_example(file="e1_time_series_data_example.py",
31
func_name='main',
32
with_plot=False)
33
34
def test_fmu_example(self):
35
"""Execute e2_fmu_example.py"""
36
if "linux" in sys.platform:
37
self.skipTest("Not supported in CI")
38
self._run_example(file="e2_fmu_example.py",
39
func_name='main',
40
with_plot=False,
41
log_fmu=False,
42
n_cpu=1)
43
44
def test_opt_example(self):
45
"""Execute e4_optimization_example.py"""
46
self._run_example(file="e4_optimization_example.py",
47
func_name='main',
48
with_plot=False)
49
50