Skip to content

Commit

Permalink
Updated unittest procedure: All tests can now run with autodetect
Browse files Browse the repository at this point in the history
Problem was: In all tests we are importung modules that have the same name (but are in a different path).
  • Loading branch information
Felix-Mac committed Oct 12, 2021
1 parent 445bff4 commit d7c72bb
Show file tree
Hide file tree
Showing 8 changed files with 180 additions and 86 deletions.
8 changes: 1 addition & 7 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,4 @@ install:
# command to run tests
script:
- cd testing
- python test_batch_reactor.py
- python test_CSTR.py
- python test_oscillating_masses_discrete.py
- python test_rotating_oscillating_masses_mhe_mpc.py
- python test_oscillating_masses_discrete_dae.py
- python test_industrial_poly.py
- python test_DIP.py
- python -m unittest
36 changes: 26 additions & 10 deletions testing/test_CSTR.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,34 @@
import sys
import unittest

sys.path.append('../')
from importlib import reload
import copy

do_mpc_path = '../'
if not do_mpc_path in sys.path:
sys.path.append('../')

import do_mpc
sys.path.pop(-1)

sys.path.append('../examples/CSTR/')
from template_model import template_model
from template_mpc import template_mpc
from template_simulator import template_simulator
sys.path.pop(-1)



class TestCSTR(unittest.TestCase):
def setUp(self):
"""Add path of test case and import the modules.
If this test isn't the first to run, the modules need to be reloaded.
Reset path afterwards.
"""
default_path = copy.deepcopy(sys.path)
sys.path.append('../examples/CSTR/')
import template_model
import template_mpc
import template_simulator

self.template_model = reload(template_model)
self.template_mpc = reload(template_mpc)
self.template_simulator = reload(template_simulator)
sys.path = default_path

def test_SX(self):
print('Testing SX implementation')
Expand All @@ -54,9 +70,9 @@ def CSTR(self, symvar_type):
Get configured do-mpc modules:
"""

model = template_model(symvar_type)
mpc = template_mpc(model)
simulator = template_simulator(model)
model = self.template_model.template_model(symvar_type)
mpc = self.template_mpc.template_mpc(model)
simulator = self.template_simulator.template_simulator(model)
estimator = do_mpc.estimator.StateFeedback(model)

"""
Expand Down
36 changes: 25 additions & 11 deletions testing/test_DIP.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,32 @@
import sys
import unittest

sys.path.append('../')
import do_mpc
sys.path.pop(-1)
from importlib import reload
import copy

do_mpc_path = '../'
if not do_mpc_path in sys.path:
sys.path.append('../')

sys.path.append('../examples/DIP/')
from template_model import template_model
from template_mpc import template_mpc
from template_simulator import template_simulator
sys.path.pop(-1)
import do_mpc


class TestDIP(unittest.TestCase):
def setUp(self):
"""Add path of test case and import the modules.
If this test isn't the first to run, the modules need to be reloaded.
Reset path afterwards.
"""
default_path = copy.deepcopy(sys.path)
sys.path.append('../examples/DIP/')
import template_model
import template_mpc
import template_simulator

self.template_model = reload(template_model)
self.template_mpc = reload(template_mpc)
self.template_simulator = reload(template_simulator)
sys.path = default_path

def test_SX(self):
print('Testing SX implementation')
Expand All @@ -58,9 +72,9 @@ def dip(self, symvar_type):
{'x': 0., 'y': 0.6, 'r': 0.3},
]

model = template_model(obstacles, symvar_type)
mpc = template_mpc(model)
simulator = template_simulator(model)
model = self.template_model.template_model(obstacles, symvar_type)
mpc = self.template_mpc.template_mpc(model)
simulator = self.template_simulator.template_simulator(model)
estimator = do_mpc.estimator.StateFeedback(model)

"""
Expand Down
36 changes: 24 additions & 12 deletions testing/test_batch_reactor.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,33 @@
import sys
import unittest

from importlib import reload
import copy

do_mpc_path = '../'
if not do_mpc_path in sys.path:
sys.path.append('../')

sys.path.append('../')
import do_mpc
sys.path.pop(-1)


sys.path.append('../examples/batch_reactor')
from template_model import template_model
from template_mpc import template_mpc
from template_simulator import template_simulator
sys.path.pop(-1)
class TestBatchReactor(unittest.TestCase):
def setUp(self):
"""Add path of test case and import the modules.
If this test isn't the first to run, the modules need to be reloaded.
Reset path afterwards.
"""
default_path = copy.deepcopy(sys.path)
sys.path.append('../examples/batch_reactor/')
import template_model
import template_mpc
import template_simulator

self.template_model = reload(template_model)
self.template_mpc = reload(template_mpc)
self.template_simulator = reload(template_simulator)
sys.path = default_path

class TestBatchReactor(unittest.TestCase):

def test_SX(self):
self.batch_reactor('SX')
Expand All @@ -54,11 +67,10 @@ def batch_reactor(self, symvar_type):
Get configured do-mpc modules:
"""

model = template_model(symvar_type)
mpc = template_mpc(model)
simulator = template_simulator(model)
model = self.template_model.template_model(symvar_type)
mpc = self.template_mpc.template_mpc(model)
simulator = self.template_simulator.template_simulator(model)
estimator = do_mpc.estimator.StateFeedback(model)

"""
Set initial state
"""
Expand Down
35 changes: 24 additions & 11 deletions testing/test_industrial_poly.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,31 @@
import sys
import unittest

sys.path.append('../')
import do_mpc
sys.path.pop(-1)
from importlib import reload
import copy

sys.path.append('../examples/industrial_poly/')
from template_model import template_model
from template_mpc import template_mpc
from template_simulator import template_simulator
sys.path.pop(-1)
do_mpc_path = '../'
if not do_mpc_path in sys.path:
sys.path.append('../')

import do_mpc

class TestIndustrialPoly(unittest.TestCase):
def setUp(self):
"""Add path of test case and import the modules.
If this test isn't the first to run, the modules need to be reloaded.
Reset path afterwards.
"""
default_path = copy.deepcopy(sys.path)
sys.path.append('../examples/industrial_poly/')
import template_model
import template_mpc
import template_simulator

self.template_model = reload(template_model)
self.template_mpc = reload(template_mpc)
self.template_simulator = reload(template_simulator)
sys.path = default_path

def test_SX(self):
print('Testing SX implementation')
Expand All @@ -54,9 +67,9 @@ def industrialpoly(self, symvar_type):
Get configured do-mpc modules:
"""

model = template_model(symvar_type)
mpc = template_mpc(model)
simulator = template_simulator(model)
model = self.template_model.template_model(symvar_type)
mpc = self.template_mpc.template_mpc(model)
simulator = self.template_simulator.template_simulator(model)
estimator = do_mpc.estimator.StateFeedback(model)

"""
Expand Down
36 changes: 26 additions & 10 deletions testing/test_oscillating_masses_discrete.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,34 @@
import sys
import unittest

sys.path.append('../')
from importlib import reload
import copy

do_mpc_path = '../'
if not do_mpc_path in sys.path:
sys.path.append('../')

import do_mpc
sys.path.pop(-1)

sys.path.append('../examples/oscillating_masses_discrete/')
from template_model import template_model
from template_mpc import template_mpc
from template_simulator import template_simulator
sys.path.pop(-1)



class TestOscillatingMassesDiscrete(unittest.TestCase):
def setUp(self):
default_path = copy.deepcopy(sys.path)

sys.path.append('../examples/oscillating_masses_discrete/')
import template_model
import template_mpc
import template_simulator


self.template_model = reload(template_model)
self.template_mpc = reload(template_mpc)
self.template_simulator = reload(template_simulator)

sys.path = default_path


def test_SX(self):
print('Testing SX implementation')
Expand All @@ -54,9 +70,9 @@ def oscillating_masses_discrete(self, symvar_type):
Get configured do-mpc modules:
"""

model = template_model(symvar_type)
mpc = template_mpc(model)
simulator = template_simulator(model)
model = self.template_model.template_model(symvar_type)
mpc = self.template_mpc.template_mpc(model)
simulator = self.template_simulator.template_simulator(model)
estimator = do_mpc.estimator.StateFeedback(model)

"""
Expand Down
38 changes: 26 additions & 12 deletions testing/test_oscillating_masses_discrete_dae.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,18 +28,33 @@
import sys
import unittest

sys.path.append('../')
import do_mpc
sys.path.pop(-1)
from importlib import reload
import copy

do_mpc_path = '../'
if not do_mpc_path in sys.path:
sys.path.append('../')

sys.path.append('../examples/oscillating_masses_discrete_dae/')
from template_model import template_model
from template_mpc import template_mpc
from template_simulator import template_simulator
sys.path.pop(-1)
import do_mpc


class TestOscillatingMassesDiscrete(unittest.TestCase):
def setUp(self):
"""Add path of test case and import the modules.
If this test isn't the first to run, the modules need to be reloaded.
Reset path afterwards.
"""
default_path = copy.deepcopy(sys.path)
sys.path.append('../examples/oscillating_masses_discrete_dae/')
import template_model
import template_mpc
import template_simulator

self.template_model = reload(template_model)
self.template_mpc = reload(template_mpc)
self.template_simulator = reload(template_simulator)
sys.path = default_path

def test_SX(self):
print('Testing SX implementation')
self.oscillating_masses_discrete('SX')
Expand All @@ -53,11 +68,10 @@ def oscillating_masses_discrete(self, symvar_type):
Get configured do-mpc modules:
"""

model = template_model(symvar_type)
mpc = template_mpc(model)
simulator = template_simulator(model)
model = self.template_model.template_model(symvar_type)
mpc = self.template_mpc.template_mpc(model)
simulator = self.template_simulator.template_simulator(model)
estimator = do_mpc.estimator.StateFeedback(model)

"""
Set initial state
"""
Expand Down

0 comments on commit d7c72bb

Please sign in to comment.