diff --git a/test/test_initialize.py b/test/test_initialize.py index 95f1adb..be90ca5 100644 --- a/test/test_initialize.py +++ b/test/test_initialize.py @@ -2,48 +2,56 @@ Use FMU deviation to check if initialization is taken into account. """ -import unittest import otfmi import otfmi.example.utility import numpy as np import os +import pytest -class TestModel(unittest.TestCase): - def setUp(self): - """Load FMU""" +@pytest.fixture +def model(): + """Load FMU""" + path_fmu = otfmi.example.utility.get_path_fmu("deviation") + model = otfmi.fmi.load_fmu(path_fmu) + return model - path_fmu = otfmi.example.utility.get_path_fmu("deviation") - self.model = otfmi.fmi.load_fmu(path_fmu) - self.var_name = "L" - self.var_val = float(300) - def test_no_default(self): - default_init_var = self.model.get_variable_start(self.var_name) - assert self.var_val != default_init_var +@pytest.fixture +def var_name(): + return "L" - def test_inline_initialization(self): - """Test initialization inline""" - result = otfmi.fmi.simulate( - self.model, input=(self.var_name, np.atleast_2d([0, self.var_val])) - ) - obtained = result.final(self.var_name) - assert self.var_val == obtained - def test_initialization_script(self): - """Test initialization scripts""" +@pytest.fixture +def var_val(): + return 300.0 - temporary_file = "initialization.mos" - with open(temporary_file, "w") as f: - f.write("{} = {};".format(self.var_name, self.var_val)) - # Simulate with this script - result = otfmi.fmi.simulate( - self.model, initialization_script=os.path.abspath(temporary_file) - ) - obtained = result.final(self.var_name) - assert self.var_val == obtained +def test_no_default(model, var_name, var_val): + default_init_var = model.get_variable_start(var_name) + assert var_val != default_init_var -if __name__ == "__main__": - unittest.main() +def test_inline_initialization(model, var_name, var_val): + """Test initialization inline""" + result = otfmi.fmi.simulate( + model, input=(var_name, np.atleast_2d([0, var_val])) + ) + obtained = result.final(var_name) + assert var_val == obtained + + +def test_initialization_script(model, var_name, var_val): + """Test initialization scripts""" + + temporary_file = "initialization.mos" + with open(temporary_file, "w") as f: + f.write("{} = {};".format(var_name, var_val)) + + # Simulate with this script + result = otfmi.fmi.simulate( + model, initialization_script=os.path.abspath(temporary_file) + ) + obtained = result.final(var_name) + assert var_val == obtained + diff --git a/test/test_variables.py b/test/test_variables.py index 28097eb..7f50e57 100644 --- a/test/test_variables.py +++ b/test/test_variables.py @@ -1,12 +1,14 @@ #!/usr/bin/env python import otfmi -import unittest import os +import pytest import tempfile import shutil +@pytest.mark.parametrize("varType", ["input", "parameter"]) +@pytest.mark.parametrize("fmuType", ["cs", "me"]) def variable_tcase(varType, fmuType): temp_path = tempfile.mkdtemp() @@ -38,21 +40,3 @@ def variable_tcase(varType, fmuType): print(y) assert abs(y[0] - 24.0) < 1e-4, "wrong value" shutil.rmtree(temp_path) - - -class TestExport(unittest.TestCase): - def test_input_cs(self): - variable_tcase("input", "cs") - - def test_parameter_cs(self): - variable_tcase("parameter", "cs") - - def test_input_me(self): - variable_tcase("input", "me") - - def test_parameter_me(self): - variable_tcase("parameter", "me") - - -if __name__ == "__main__": - unittest.main()