From abf1565a8d18baefe47290f4e0cb9f246c8c94ef Mon Sep 17 00:00:00 2001 From: bnavigator Date: Thu, 31 Dec 2020 00:25:23 +0100 Subject: [PATCH] Fix loading of defaults during pytest fixture setup Also make matarrayout and matarrayin function scoped fixtures so that they can set the warnings filters individually as needed for each test. --- control/tests/config_test.py | 2 +- control/tests/conftest.py | 21 ++++++++++++++++++--- control/tests/statesp_test.py | 16 +++++++++++++++- 3 files changed, 34 insertions(+), 5 deletions(-) diff --git a/control/tests/config_test.py b/control/tests/config_test.py index 3979ffca5..3b2a11f12 100644 --- a/control/tests/config_test.py +++ b/control/tests/config_test.py @@ -37,7 +37,7 @@ def test_get_param(self): assert ct.config._get_param('config', 'test1', None) == 1 assert ct.config._get_param('config', 'test1', None, 1) == 1 - ct.config.defaults['config.test3'] is None + ct.config.defaults['config.test3'] = None assert ct.config._get_param('config', 'test3') is None assert ct.config._get_param('config', 'test3', 1) == 1 assert ct.config._get_param('config', 'test3', None, 1) is None diff --git a/control/tests/conftest.py b/control/tests/conftest.py index 7204c8f14..b67ef3674 100644 --- a/control/tests/conftest.py +++ b/control/tests/conftest.py @@ -28,7 +28,22 @@ "PendingDeprecationWarning") -@pytest.fixture(scope="session", autouse=TEST_MATRIX_AND_ARRAY, +@pytest.fixture(scope="session", autouse=True) +def control_defaults(): + """Make sure the testing session always starts with the defaults. + + This should be the first fixture initialized, + so that all other fixtures see the general defaults (unless they set them + themselves) even before importing control/__init__. Enforce this by adding + it as an argument to all other session scoped fixtures. + """ + control.reset_defaults() + the_defaults = control.config.defaults.copy() + yield + # assert that nothing changed it without reverting + assert control.config.defaults == the_defaults + +@pytest.fixture(scope="function", autouse=TEST_MATRIX_AND_ARRAY, params=[pytest.param("arrayout", marks=matrixerrorfilter), pytest.param("matrixout", marks=matrixfilter)]) def matarrayout(request): @@ -70,7 +85,7 @@ def check_deprecated_matrix(): yield -@pytest.fixture(scope="session", +@pytest.fixture(scope="function", params=[p for p, usebydefault in [(pytest.param(np.array, id="arrayin"), @@ -90,7 +105,7 @@ def editsdefaults(): """Make sure any changes to the defaults only last during a test""" restore = control.config.defaults.copy() yield - control.config.defaults.update(restore) + control.config.defaults = restore.copy() @pytest.fixture(scope="function") diff --git a/control/tests/statesp_test.py b/control/tests/statesp_test.py index c7b0a0aaf..23ccab555 100644 --- a/control/tests/statesp_test.py +++ b/control/tests/statesp_test.py @@ -16,7 +16,7 @@ from control.dtime import sample_system from control.lti import evalfr from control.statesp import (StateSpace, _convertToStateSpace, drss, rss, ss, - tf2ss) + tf2ss, _statesp_defaults) from control.tests.conftest import ismatarrayout, slycotonly from control.xferfcn import TransferFunction, ss2tf @@ -826,3 +826,17 @@ def test_returnScipySignalLTI_error(self, mimoss): with pytest.raises(ValueError): mimoss.returnScipySignalLTI(strict=True) + +class TestStateSpaceConfig: + """Test the configuration of the StateSpace module""" + + @pytest.fixture + def matarrayout(self): + """Override autoused global fixture within this class""" + pass + + def test_statespace_defaults(self, matarrayout): + """Make sure the tests are run with the configured defaults""" + for k, v in _statesp_defaults.items(): + assert defaults[k] == v, \ + "{} is {} but expected {}".format(k, defaults[k], v)