Changeset 13794


Ignore:
Timestamp:
Oct 22, 2019 8:38:02 AM (3 weeks ago)
Author:
randersson
Message:

#5819 Modified some assert a == b to also follow up with a proper error message when the assert fails.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/dev-5819/Python/src/tests_jmodelica/simulation/test_assimulo_interface_fmi.py

    r13791 r13794  
    5959input_object = (["u"],input_linear)
    6060
    61 class Test_When:
     61class AssertWithMessage:
     62    def __init__(self):
     63        self.message = ""
     64    def assert_equal_msg(self, actual, desired):
     65        err_msg = "\nACTUAL: {}\nDESIRED: {}".format(actual, desired)
     66        assert actual == desired, err_msg
     67
     68class Test_When(AssertWithMessage):
    6269    @classmethod
    6370    def setUpClass(cls):
     
    7279        res = model.simulate(final_time=3.5)
    7380       
    74         assert res.final("nextTime") == 4.0
    75         assert res.final("nextTime2") == 3.0
    76         assert res.final("nextTime3") == 8.0
    77        
    78 class Test_Sparse_Linear_Block:
     81        self.assert_equal_msg(res.final("nextTime"), 4.0)
     82        self.assert_equal_msg(res.final("nextTime2"), 3.0)
     83        self.assert_equal_msg(res.final("nextTime3"), 8.0)
     84       
     85class Test_Sparse_Linear_Block(AssertWithMessage):
    7986    @classmethod
    8087    def setUpClass(cls):
     
    98105       
    99106        #Assert that sparse handling has no impact on the number of steps
    100         assert res1.solver.statistics["nsteps"] == res2.solver.statistics["nsteps"]
     107        self.assert_equal_msg(res2.solver.statistics["nsteps"], res1.solver.statistics["nsteps"])
    101108        N.testing.assert_almost_equal(res1.final("J1.w"), res2.final("J1.w"), 3)
    102109       
     
    108115                        compiler_options={"generate_sparse_block_jacobian_threshold": 0})
    109116
    110 class Test_Time_Events_FMU10:
     117class Test_Time_Events_FMU10(AssertWithMessage):
    111118    @classmethod
    112119    def setUpClass(cls):
     
    144151        ev = model.get_event_info()
    145152        print(ev.nextEventTime)
    146         assert ev.nextEventTime == 1
     153        self.assert_equal_msg(ev.nextEventTime, 1)
    147154       
    148155    @testattr(stddist_full = True)
     
    152159        ev = model.get_event_info()
    153160        print(ev.nextEventTime)
    154         assert ev.nextEventTime == 2
    155         assert ev.nextEventTime == model.get("p")
     161        self.assert_equal_msg(ev.nextEventTime, 2)
     162        self.assert_equal_msg(ev.nextEventTime, model.get("p"))
    156163       
    157164    @testattr(stddist_full = True)
     
    161168        ev = model.get_event_info()
    162169        print(ev.nextEventTime)
    163         assert ev.nextEventTime == 1.5
     170        self.assert_equal_msg(ev.nextEventTime, 1.5)
    164171       
    165172    @testattr(stddist_full = True)
     
    169176        model.initialize()
    170177        ev = model.get_event_info()
    171         assert ev.upcomingTimeEvent == False
    172         assert model.get("x")== 2
     178        self.assert_equal_msg(ev.upcomingTimeEvent, False)
     179        self.assert_equal_msg(model.get("x"), 2)
    173180       
    174181        model.reset()
     
    176183        model.initialize()
    177184       
    178         assert ev.upcomingTimeEvent == False
    179         assert model.get("x") == 1
     185        self.assert_equal_msg(ev.upcomingTimeEvent, False)
     186        self.assert_equal_msg(model.get("x"), 1)
    180187       
    181188    @testattr(stddist_base = True)
     
    185192        ev = model.get_event_info()
    186193        print(ev.nextEventTime)
    187         assert ev.nextEventTime == 0.5
     194        self.assert_equal_msg(ev.nextEventTime, 0.5)
    188195       
    189196        model.simulate(options={"initialize":False})
    190197       
    191198        print("i (should be 2): ", model.get("i"))
    192         assert model.get("i") == 2
     199        self.assert_equal_msg(model.get("i"), 2)
    193200       
    194201    @testattr(stddist_base = True)
     
    198205        ev = model.get_event_info()
    199206        print(ev.nextEventTime)
    200         assert ev.nextEventTime == 0.5
     207        self.assert_equal_msg(ev.nextEventTime, 0.5)
    201208       
    202209        model.simulate(options={"initialize":False})
    203210       
    204211        print("i (should be 2): ", model.get("i"))
    205         assert model.get("i") == 2
     212        self.assert_equal_msg(model.get("i"), 2)
    206213       
    207214    @testattr(stddist_base = True)
     
    211218        ev = model.get_event_info()
    212219        print(ev.nextEventTime)
    213         assert ev.nextEventTime == 0.5
     220        self.assert_equal_msg(ev.nextEventTime, 0.5)
    214221       
    215222        model.simulate(options={"initialize":False})
     
    217224        print("i (should be 2): ", model.get("i"))
    218225        print("j (should be 1): ", model.get("j"))
    219         assert model.get("i") == 2
    220         assert model.get("j") == 1
     226        self.assert_equal_msg(model.get("i"), 2)
     227        self.assert_equal_msg(model.get("j"), 1)
    221228       
    222229    @testattr(stddist_base = True)
     
    226233        ev = model.get_event_info()
    227234        print(ev.nextEventTime)
    228         assert ev.nextEventTime == 0.5
     235        self.assert_equal_msg(ev.nextEventTime, 0.5)
    229236       
    230237        model.simulate(options={"initialize":False})
     
    232239        print("i (should be 1): ", model.get("i"))
    233240        print("j (should be 1): ", model.get("j"))
    234         assert model.get("i") == 1
    235         assert model.get("j") == 1
     241        self.assert_equal_msg(model.get("i"), 1)
     242        self.assert_equal_msg(model.get("j"), 1)
    236243       
    237244    @testattr(stddist_full = True)
     
    241248        ev = model.get_event_info()
    242249        print(ev.nextEventTime)
    243         assert ev.nextEventTime == 1.5
     250        self.assert_equal_msg(ev.nextEventTime , 1.5)
    244251       
    245252        res = model.simulate(final_time=4, options={"initialize":False})
     
    248255        print("dx: ", res["der(x)"])
    249256       
    250         assert res.solver.statistics["ntimeevents"] == 2
    251         assert res.solver.statistics["nstateevents"] == 2
     257        self.assert_equal_msg(res.solver.statistics["ntimeevents"], 2)
     258        self.assert_equal_msg(res.solver.statistics["nstateevents"], 2)
    252259
    253260    """                 """
     
    261268        model = load_fmu("TimeEvents_TestSampling1.fmu")
    262269        model.initialize()
    263         res = model.simulate(0, 1e3, options={"initialize":False});
    264         assert res.solver.statistics["ntimeevents"] == 1e4
     270        res = model.simulate(0, 1e3, options={"initialize":False})
     271        self.assert_equal_msg(res.solver.statistics["ntimeevents"], 1e4)
    265272
    266273    """ Only small interval. """
     
    270277        model = load_fmu("TimeEvents_TestSampling2.fmu")
    271278        model.initialize()
    272         res = model.simulate(0,1e-6, options={"initialize":False});
    273         assert res.solver.statistics["ntimeevents"] == 1e4
     279        res = model.simulate(0,1e-6, options={"initialize":False})
     280        self.assert_equal_msg(res.solver.statistics["ntimeevents"], 1e4)
    274281
    275282    """ Only big interval. """
     
    279286        model = load_fmu("TimeEvents_TestSampling3.fmu")
    280287        model.initialize()
    281         res = model.simulate(0,1e64, options={"initialize":False});
    282         assert res.solver.statistics["ntimeevents"] == 1e4
     288        res = model.simulate(0,1e64, options={"initialize":False})
     289        self.assert_equal_msg(res.solver.statistics["ntimeevents"], 1e4)
    283290
    284291    """ Basic test using offset. """
     
    288295        model = load_fmu("TimeEvents_TestSampling4.fmu")
    289296        model.initialize()
    290         res = model.simulate(0,2e-6, options={"initialize":False});
    291         assert res.solver.statistics["ntimeevents"] == (1e4)+1
     297        res = model.simulate(0,2e-6, options={"initialize":False})
     298        self.assert_equal_msg(res.solver.statistics["ntimeevents"], (1e4)+1)
    292299
    293300    """ Big interval, small offset. """
     
    297304        model = load_fmu("TimeEvents_TestSampling5.fmu")
    298305        model.initialize()
    299         res = model.simulate(0,1e64, options={"initialize":False});
    300         assert res.solver.statistics["ntimeevents"] == 1e4
     306        res = model.simulate(0,1e64, options={"initialize":False})
     307        self.assert_equal_msg(res.solver.statistics["ntimeevents"], 1e4)
    301308
    302309    """ Big interval and offset. """
     
    306313        model = load_fmu("TimeEvents_TestSampling6.fmu")
    307314        model.initialize()
    308         res = model.simulate(0,1e64, options={"initialize":False});
    309         assert res.solver.statistics["ntimeevents"] == 1e4
     315        res = model.simulate(0,1e64, options={"initialize":False})
     316        self.assert_equal_msg(res.solver.statistics["ntimeevents"], 1e4)
    310317
    311318    @testattr(sample = True)
     
    313320        model = load_fmu("TimeEvents_TestSampling7.fmu")
    314321        model.initialize()
    315         res = model.simulate(0,1e5, options={"initialize":False});
    316         assert res.solver.statistics["ntimeevents"] == 1e4
     322        res = model.simulate(0,1e5, options={"initialize":False})
     323        self.assert_equal_msg(res.solver.statistics["ntimeevents"], 1e4)
    317324
    318325    """ Test 8 verifies that sampling raises an exception when a too small step is required. """
     
    330337        model.initialize()
    331338        res = model.simulate(0,1, options={"initialize":False})
    332         assert res.solver.statistics["ntimeevents"] == 10
     339        self.assert_equal_msg(res.solver.statistics["ntimeevents"], 10)
    333340
    334341    @testattr(stddist_base = True)
     
    340347        res = model.simulate(0,1, options=opts)
    341348        N.testing.assert_almost_equal(model.get("s"), 2.8)
    342         assert res.solver.statistics["ntimeevents"] == 2     
     349        self.assert_equal_msg(res.solver.statistics["ntimeevents"], 2)
    343350
    344351class Test_Time_Events_FMU20:
Note: See TracChangeset for help on using the changeset viewer.