Skip to content

Commit

Permalink
Replacing nose by pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
PeterMeisrimelModelon committed Feb 12, 2024
1 parent be7fa37 commit d485940
Show file tree
Hide file tree
Showing 12 changed files with 270 additions and 536 deletions.
4 changes: 2 additions & 2 deletions .github/workflows/build.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ jobs:
python-version: '3.11.x'
- name: Setup Python
run: |
python3 -m pip install Cython numpy scipy matplotlib nose-py3
python3 -m pip install Cython numpy scipy matplotlib pytest
- name: Install system
run: |
sudo apt-get -y install cmake liblapack-dev libsuitesparse-dev libhypre-dev
Expand Down Expand Up @@ -59,4 +59,4 @@ jobs:
run: |
rm src/pyfmi/__init__.py
cp -rv src/pyfmi/tests/files tests
python3 -m nose --verbose tests/*.py
pytest --verbose tests/
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,6 @@ install_requires =
numpy >= 1.26.3
scipy >= 1.11.4
cython >= 3.0.7
nose-py3 >= 1.6.3
pytest >= 7.4.4
matplotlib > 3
assimulo >= 3.5.0
14 changes: 0 additions & 14 deletions src/pyfmi/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,20 +34,6 @@
int = np.int32
np.int = np.int32

def testattr(**kwargs):
"""Add attributes to a test function/method/class.
This function is needed to be able to add
@attr(slow = True)
for functions.
"""
def wrap(func):
func.__dict__.update(kwargs)
return func
return wrap


try:
curr_dir = os.path.dirname(os.path.abspath(__file__))
_fpath=os.path.join(curr_dir,'version.txt')
Expand Down
357 changes: 138 additions & 219 deletions tests/test_fmi.py

Large diffs are not rendered by default.

63 changes: 29 additions & 34 deletions tests/test_fmi_coupled.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,9 @@
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.

import nose
import pytest
import os

from pyfmi import testattr
from pyfmi.fmi import FMUModelME2
from pyfmi.fmi_coupled import CoupledFMUModelME2
import pyfmi.fmi as fmi
Expand All @@ -36,7 +35,6 @@

if assimulo_installed:
class Test_CoupledFMUModelME2_Simulation:
@testattr(stddist = True)
def test_linear_example(self):
model_sub_1 = Dummy_FMUModelME2([], os.path.join(me2_xml_path, "LinearStability.SubSystem1.fmu"), _connect_dll=False)
model_sub_2 = Dummy_FMUModelME2([], os.path.join(me2_xml_path, "LinearStability.SubSystem2.fmu"), _connect_dll=False)
Expand Down Expand Up @@ -76,20 +74,18 @@ def sub2(*args, **kwargs):

res = coupled.simulate(options=opts)

nose.tools.assert_almost_equal(res.final("First.x1"),0.08597302307099872)
nose.tools.assert_almost_equal(res.final("Second.x2"),0.0083923348082567)
nose.tools.assert_almost_equal(res.initial("First.x1"),1.0)
nose.tools.assert_almost_equal(res.initial("Second.x2"),1.0)
assert res.final("First.x1") == pytest.approx(0.08597302307099872)
assert res.final("Second.x2") == pytest.approx(0.0083923348082567)
assert res.initial("First.x1") == pytest.approx(1.0)
assert res.initial("Second.x2") == pytest.approx(1.0)

nose.tools.assert_almost_equal(res.final("First.u1"),-0.25909975860402856)
nose.tools.assert_almost_equal(res.final("Second.u2"),-0.0011806893910324295)
nose.tools.assert_almost_equal(res.initial("First.u1"),-17.736842105263158)
nose.tools.assert_almost_equal(res.initial("Second.u2"),-14.73684210526316)
assert res.final("First.u1") == pytest.approx(-0.25909975860402856)
assert res.final("Second.u2") == pytest.approx(-0.0011806893910324295)
assert res.initial("First.u1") == pytest.approx(-17.736842105263158)
assert res.initial("Second.u2") == pytest.approx(-14.73684210526316)


class Test_CoupledFMUModelME2:

@testattr(stddist = True)
def test_reversed_connections(self):
model_sub_1 = FMUModelME2(os.path.join(me2_xml_path, "LinearStability.SubSystem1.fmu"), _connect_dll=False)
model_sub_2 = FMUModelME2(os.path.join(me2_xml_path, "LinearStability.SubSystem2.fmu"), _connect_dll=False)
Expand All @@ -99,16 +95,16 @@ def test_reversed_connections(self):
connections = [(model_sub_2,"y1",model_sub_1,"u2"),
(model_sub_1,"y2",model_sub_2,"u1")]

nose.tools.assert_raises(fmi.FMUException, CoupledFMUModelME2, models, connections)
with pytest.raises(fmi.FMUException):
CoupledFMUModelME2(models, connections)

connections = [(model_sub_2,"u2",model_sub_1,"y1"),
(model_sub_1,"u1",model_sub_2,"y2")]

nose.tools.assert_raises(fmi.FMUException, CoupledFMUModelME2, models, connections)
with pytest.raises(fmi.FMUException):
CoupledFMUModelME2(models, connections)

@testattr(stddist = True)
def test_inputs_list(self):

model_sub_1 = FMUModelME2(os.path.join(me2_xml_path, "LinearStability.SubSystem1.fmu"), _connect_dll=False)
model_sub_2 = FMUModelME2(os.path.join(me2_xml_path, "LinearStability.SubSystem2.fmu"), _connect_dll=False)
model_full = FMUModelME2(os.path.join(me2_xml_path, "LinearStability.FullSystem.fmu"), _connect_dll=False)
Expand All @@ -128,7 +124,6 @@ def test_inputs_list(self):
assert "First.u1" in vars
assert "Second.u2" in vars

@testattr(stddist = True)
def test_alias(self):
model_cc_1 = FMUModelME2(os.path.join(me2_xml_path, "CoupledClutches.fmu"), _connect_dll=False)
model_cc_2 = FMUModelME2(os.path.join(me2_xml_path, "CoupledClutches.fmu"), _connect_dll=False)
Expand All @@ -142,32 +137,35 @@ def test_alias(self):
assert "First.J4.phi" in aliases.keys()
assert coupled.get_variable_alias_base("First.J4.phi") == "First.J4.flange_a.phi"

@testattr(stddist = True)
def test_loading(self):
model_cc_1 = FMUModelME2(os.path.join(me2_xml_path, "CoupledClutches.fmu"), _connect_dll=False)
model_cc_2 = FMUModelME2(os.path.join(me2_xml_path, "CoupledClutches.fmu"), _connect_dll=False)

models = [model_cc_1, model_cc_2]
connections = []

nose.tools.assert_raises(fmi.FMUException, CoupledFMUModelME2, models, connections)
with pytest.raises(fmi.FMUException):
CoupledFMUModelME2(models, connections)

models = [("First", model_cc_1), model_cc_2]
nose.tools.assert_raises(fmi.FMUException, CoupledFMUModelME2, models, connections)
with pytest.raises(fmi.FMUException):
CoupledFMUModelME2(models, connections)

models = [("First", model_cc_1), ("First", model_cc_2)]
nose.tools.assert_raises(fmi.FMUException, CoupledFMUModelME2, models, connections)
with pytest.raises(fmi.FMUException):
CoupledFMUModelME2(models, connections)

models = [("First", model_cc_1), ("Second", model_cc_2)]
coupled = CoupledFMUModelME2(models, connections)

connections = [("k")]
nose.tools.assert_raises(fmi.FMUException, CoupledFMUModelME2, models, connections)
with pytest.raises(fmi.FMUException):
CoupledFMUModelME2(models, connections)

connections = [(model_cc_1, "J1.phi", model_cc_2, "J2.phi")]
nose.tools.assert_raises(fmi.FMUException, CoupledFMUModelME2, models, connections)
with pytest.raises(fmi.FMUException):
CoupledFMUModelME2(models, connections)

@testattr(stddist = True)
def test_get_variable_valueref(self):
model_cc_1 = FMUModelME2(os.path.join(me2_xml_path, "CoupledClutches.fmu"), _connect_dll=False)
model_cc_2 = FMUModelME2(os.path.join(me2_xml_path, "CoupledClutches.fmu"), _connect_dll=False)
Expand All @@ -177,7 +175,8 @@ def test_get_variable_valueref(self):

coupled = CoupledFMUModelME2(models, connections)

nose.tools.assert_raises(fmi.FMUException, coupled.get_variable_valueref, "J1.w")
with pytest.raises(fmi.FMUException):
coupled.get_variable_valueref("J1.w")

vr_1 = coupled.get_variable_valueref("First.J1.w")
vr_2 = coupled.get_variable_valueref("Second.J1.w")
Expand All @@ -190,7 +189,6 @@ def test_get_variable_valueref(self):
assert var_name_1 == "First.J1.w"
assert var_name_2 == "Second.J1.w"

@testattr(stddist = True)
def test_ode_sizes(self):
model_cc_1 = FMUModelME2(os.path.join(me2_xml_path, "CoupledClutches.fmu"), _connect_dll=False)
model_cc_2 = FMUModelME2(os.path.join(me2_xml_path, "CoupledClutches.fmu"), _connect_dll=False)
Expand All @@ -205,7 +203,6 @@ def test_ode_sizes(self):
assert nbr_states == 16
assert nbr_event_ind == 66

@testattr(stddist = True)
def test_variable_variability(self):
model_cc_1 = FMUModelME2(os.path.join(me2_xml_path, "CoupledClutches.fmu"), _connect_dll=False)
model_cc_2 = FMUModelME2(os.path.join(me2_xml_path, "CoupledClutches.fmu"), _connect_dll=False)
Expand All @@ -215,13 +212,13 @@ def test_variable_variability(self):

coupled = CoupledFMUModelME2(models, connections)

nose.tools.assert_raises(fmi.FMUException, coupled.get_variable_variability, "J1.w")
with pytest.raises(fmi.FMUException):
coupled.get_variable_variability("J1.w")

variability = coupled.get_variable_variability("First.J1.w")

assert variability == model_cc_1.get_variable_variability("J1.w")

@testattr(stddist = True)
def test_variable_causality(self):
model_cc_1 = FMUModelME2(os.path.join(me2_xml_path, "CoupledClutches.fmu"), _connect_dll=False)
model_cc_2 = FMUModelME2(os.path.join(me2_xml_path, "CoupledClutches.fmu"), _connect_dll=False)
Expand All @@ -231,13 +228,13 @@ def test_variable_causality(self):

coupled = CoupledFMUModelME2(models, connections)

nose.tools.assert_raises(fmi.FMUException, coupled.get_variable_causality, "J1.w")
with pytest.raises(fmi.FMUException):
coupled.get_variable_causality("J1.w")

causality = coupled.get_variable_causality("First.J1.w")

assert causality == model_cc_1.get_variable_causality("J1.w")

@testattr(stddist = True)
def test_derivatives_list(self):
model_cc_1 = FMUModelME2(os.path.join(me2_xml_path, "CoupledClutches.fmu"), _connect_dll=False)
model_cc_2 = FMUModelME2(os.path.join(me2_xml_path, "CoupledClutches.fmu"), _connect_dll=False)
Expand All @@ -255,7 +252,6 @@ def test_derivatives_list(self):
alias_vars = coupled.get_variable_alias(var).keys()
assert state in alias_vars

@testattr(stddist = True)
def test_states_list(self):
model_cc_1 = FMUModelME2(os.path.join(me2_xml_path, "CoupledClutches.fmu"), _connect_dll=False)
model_cc_2 = FMUModelME2(os.path.join(me2_xml_path, "CoupledClutches.fmu"), _connect_dll=False)
Expand All @@ -273,7 +269,6 @@ def test_states_list(self):
alias_vars = coupled.get_variable_alias(var).keys()
assert state in alias_vars

@testattr(stddist = True)
def test_model_variables(self):
model_cc_1 = FMUModelME2(os.path.join(me2_xml_path, "CoupledClutches.fmu"), _connect_dll=False)
model_cc_2 = FMUModelME2(os.path.join(me2_xml_path, "CoupledClutches.fmu"), _connect_dll=False)
Expand Down
3 changes: 0 additions & 3 deletions tests/test_fmi_estimate.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import os
import numpy as np

from pyfmi import testattr
from pyfmi.tests.test_util import Dummy_FMUModelME2
from scipy.io.matlab.mio import loadmat

Expand All @@ -32,8 +31,6 @@

if assimulo_installed:
class Test_FMUModelME2_Estimate:

@testattr(stddist = True)
def test_quadtank_estimate(self):
model = Dummy_FMUModelME2([], os.path.join(file_path, "files", "FMUs", "XML", "ME2.0", "QuadTankPack_Sim_QuadTank.fmu"), _connect_dll=False)

Expand Down
3 changes: 0 additions & 3 deletions tests/test_fmi_extended.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import os
import numpy as np

from pyfmi import testattr
from pyfmi.fmi_extended import FMUModelME1Extended

file_path = os.path.dirname(os.path.abspath(__file__))
Expand All @@ -27,14 +26,12 @@

class Test_FMUModelME1Extended:

@testattr(stddist = True)
def test_log_file_name(self):
model = FMUModelME1Extended(os.path.join(me1_xml_path, "bouncingBall.fmu"), _connect_dll=False)
assert os.path.exists("bouncingBall_log.txt")
model = FMUModelME1Extended(os.path.join(me1_xml_path, "bouncingBall.fmu"), log_file_name="Test_log.txt", _connect_dll=False)
assert os.path.exists("Test_log.txt")

@testattr(stddist = True)
def test_default_experiment(self):
model = FMUModelME1Extended(os.path.join(me1_xml_path, "CoupledClutches.fmu"), _connect_dll=False)

Expand Down
Loading

0 comments on commit d485940

Please sign in to comment.