From cbd838c6cf37458cd5ee9bc86f68159b2442fd4d Mon Sep 17 00:00:00 2001 From: Marco Clemencic Date: Mon, 19 Jun 2023 12:40:06 +0200 Subject: [PATCH] GaudiConfig2: converted tests to pytest --- GaudiConfiguration/CMakeLists.txt | 10 +- .../ConfPackage/ConfModule.py | 0 .../{nose => python}/ConfPackage/__init__.py | 0 .../tests/{nose => python}/__init__.py | 0 GaudiConfiguration/tests/python/conftest.py | 21 ++++ .../{nose => python}/test_ConfDB_wrapper.py | 6 +- .../{nose => python}/test_configurable.py | 36 ++----- .../test_configurables_merge.py | 36 +++---- .../test_default_value_stability.py | 21 +--- .../{nose => python}/test_full_option_flow.py | 33 ++++--- .../test_global_instances_flag.py | 43 ++++---- .../test_mapping_semantics.py | 18 ++-- .../test_options_generation.py | 51 +++------- .../tests/{nose => python}/test_semantics.py | 99 ++++++++----------- .../test_sequence_semantics.py | 18 ++-- .../{nose => python}/test_serialization.py | 25 +---- 16 files changed, 164 insertions(+), 253 deletions(-) rename GaudiConfiguration/tests/{nose => python}/ConfPackage/ConfModule.py (100%) rename GaudiConfiguration/tests/{nose => python}/ConfPackage/__init__.py (100%) rename GaudiConfiguration/tests/{nose => python}/__init__.py (100%) create mode 100644 GaudiConfiguration/tests/python/conftest.py rename GaudiConfiguration/tests/{nose => python}/test_ConfDB_wrapper.py (97%) rename GaudiConfiguration/tests/{nose => python}/test_configurable.py (81%) rename GaudiConfiguration/tests/{nose => python}/test_configurables_merge.py (79%) rename GaudiConfiguration/tests/{nose => python}/test_default_value_stability.py (82%) rename GaudiConfiguration/tests/{nose => python}/test_full_option_flow.py (90%) rename GaudiConfiguration/tests/{nose => python}/test_global_instances_flag.py (76%) rename GaudiConfiguration/tests/{nose => python}/test_mapping_semantics.py (92%) rename GaudiConfiguration/tests/{nose => python}/test_options_generation.py (81%) rename GaudiConfiguration/tests/{nose => python}/test_semantics.py (76%) rename GaudiConfiguration/tests/{nose => python}/test_sequence_semantics.py (93%) rename GaudiConfiguration/tests/{nose => python}/test_serialization.py (86%) diff --git a/GaudiConfiguration/CMakeLists.txt b/GaudiConfiguration/CMakeLists.txt index b3c21d5bb..8c43a46aa 100644 --- a/GaudiConfiguration/CMakeLists.txt +++ b/GaudiConfiguration/CMakeLists.txt @@ -13,13 +13,7 @@ # Install python modules gaudi_install(PYTHON) -if(BUILD_TESTING) - # FIXME: gaudi_add_test(nosetests) is not configurable enough, so I copied a bit of it - _import_nosetests() - add_test(NAME GaudiConfiguration.nose - COMMAND run $ -v --with-doctest --with-coverage --cover-package=GaudiConfig2 - --cover-min-percentage=100 - ${CMAKE_CURRENT_SOURCE_DIR}/tests/nose) -endif() +# FIXME: with nosetests we were using --cover-min-percentage=100, but it is not available with gaudi_add_pytest +gaudi_add_pytest(tests/python) # Note: The file tools/print_limits.cpp is a ROOT macro, so it is not meant to be compiled diff --git a/GaudiConfiguration/tests/nose/ConfPackage/ConfModule.py b/GaudiConfiguration/tests/python/ConfPackage/ConfModule.py similarity index 100% rename from GaudiConfiguration/tests/nose/ConfPackage/ConfModule.py rename to GaudiConfiguration/tests/python/ConfPackage/ConfModule.py diff --git a/GaudiConfiguration/tests/nose/ConfPackage/__init__.py b/GaudiConfiguration/tests/python/ConfPackage/__init__.py similarity index 100% rename from GaudiConfiguration/tests/nose/ConfPackage/__init__.py rename to GaudiConfiguration/tests/python/ConfPackage/__init__.py diff --git a/GaudiConfiguration/tests/nose/__init__.py b/GaudiConfiguration/tests/python/__init__.py similarity index 100% rename from GaudiConfiguration/tests/nose/__init__.py rename to GaudiConfiguration/tests/python/__init__.py diff --git a/GaudiConfiguration/tests/python/conftest.py b/GaudiConfiguration/tests/python/conftest.py new file mode 100644 index 000000000..42ac8efbe --- /dev/null +++ b/GaudiConfiguration/tests/python/conftest.py @@ -0,0 +1,21 @@ +##################################################################################### +# (c) Copyright 2023 CERN for the benefit of the LHCb and ATLAS collaborations # +# # +# This software is distributed under the terms of the Apache version 2 licence, # +# copied verbatim in the file "LICENSE". # +# # +# In applying this licence, CERN does not waive the privileges and immunities # +# granted to it by virtue of its status as an Intergovernmental Organization # +# or submit itself to any jurisdiction. # +##################################################################################### +import pytest +from GaudiConfig2 import Configurable, useGlobalInstances + + +@pytest.fixture +def with_global_instances(): + Configurable.instances.clear() + useGlobalInstances(True) + yield + Configurable.instances.clear() + useGlobalInstances(False) diff --git a/GaudiConfiguration/tests/nose/test_ConfDB_wrapper.py b/GaudiConfiguration/tests/python/test_ConfDB_wrapper.py similarity index 97% rename from GaudiConfiguration/tests/nose/test_ConfDB_wrapper.py rename to GaudiConfiguration/tests/python/test_ConfDB_wrapper.py index 75f838465..1b94075c1 100644 --- a/GaudiConfiguration/tests/nose/test_ConfDB_wrapper.py +++ b/GaudiConfiguration/tests/python/test_ConfDB_wrapper.py @@ -8,7 +8,7 @@ # granted to it by virtue of its status as an Intergovernmental Organization # # or submit itself to any jurisdiction. # ##################################################################################### -from nose.tools import raises +import pytest def test_import(): @@ -64,11 +64,11 @@ def test_get_by_full_type_ignore_spaces(): assert C.getByType("TestConf::" + name) is C.getByType("TestConf::" + mix_name) -@raises(AttributeError) def get_get_by_full_type_missing(): from GaudiConfig2 import Configurables as C - C.getByType("Gaudi::NotThere") + with pytest.raises(AttributeError): + C.getByType("Gaudi::NotThere") def test_confdb(): diff --git a/GaudiConfiguration/tests/nose/test_configurable.py b/GaudiConfiguration/tests/python/test_configurable.py similarity index 81% rename from GaudiConfiguration/tests/nose/test_configurable.py rename to GaudiConfiguration/tests/python/test_configurable.py index 0af36308c..46d7825e6 100644 --- a/GaudiConfiguration/tests/nose/test_configurable.py +++ b/GaudiConfiguration/tests/python/test_configurable.py @@ -8,23 +8,11 @@ # granted to it by virtue of its status as an Intergovernmental Organization # # or submit itself to any jurisdiction. # ##################################################################################### -from GaudiConfig2 import Configurable, useGlobalInstances +import pytest from GaudiConfig2.Configurables.TestConf import MyAlg, SimpleOptsAlgTool -from nose.tools import raises, with_setup -def setup_func(): - Configurable.instances.clear() - useGlobalInstances(True) - - -def teardown_func(): - Configurable.instances.clear() - useGlobalInstances(False) - - -@with_setup(setup_func, teardown_func) -def test_configurable(): +def test_configurable(with_global_instances): from GaudiConfig2 import Configurable @@ -98,8 +86,7 @@ def test_properties(): assert p.AnIntProp == 42 -@with_setup(setup_func, teardown_func) -def test_parent_handling(): +def test_parent_handling(with_global_instances): p = MyAlg("Dummy") t = SimpleOptsAlgTool("ATool", parent=p) @@ -112,20 +99,17 @@ def test_parent_handling(): assert t.getName() == "Dummy.ATool.BTool" -@with_setup(setup_func, teardown_func) -@raises(AttributeError) -def test_parent_with_no_name(): - SimpleOptsAlgTool("ATool", parent=MyAlg()) +def test_parent_with_no_name(with_global_instances): + with pytest.raises(AttributeError): + SimpleOptsAlgTool("ATool", parent=MyAlg()) -@with_setup(setup_func, teardown_func) -@raises(TypeError) -def test_child_with_no_name(): - SimpleOptsAlgTool(parent=MyAlg("Dummy")) +def test_child_with_no_name(with_global_instances): + with pytest.raises(TypeError): + SimpleOptsAlgTool(parent=MyAlg("Dummy")) -@with_setup(setup_func, teardown_func) -def test_clone(): +def test_clone(with_global_instances): a = MyAlg("a", AnIntProp=42) b = a.clone("b") assert id(a) != id(b) diff --git a/GaudiConfiguration/tests/nose/test_configurables_merge.py b/GaudiConfiguration/tests/python/test_configurables_merge.py similarity index 79% rename from GaudiConfiguration/tests/nose/test_configurables_merge.py rename to GaudiConfiguration/tests/python/test_configurables_merge.py index 01c33408b..f4a135695 100644 --- a/GaudiConfiguration/tests/nose/test_configurables_merge.py +++ b/GaudiConfiguration/tests/python/test_configurables_merge.py @@ -8,19 +8,9 @@ # granted to it by virtue of its status as an Intergovernmental Organization # # or submit itself to any jurisdiction. # ##################################################################################### -from GaudiConfig2 import Configurable, mergeConfigs, useGlobalInstances +import pytest +from GaudiConfig2 import mergeConfigs from GaudiConfig2.Configurables.TestConf import MyAlg, SimpleOptsAlgTool -from nose.tools import raises, with_setup - - -def setup_func(): - Configurable.instances.clear() - useGlobalInstances(True) - - -def teardown_func(): - Configurable.instances.clear() - useGlobalInstances(False) def test_merge_identical(): @@ -44,8 +34,7 @@ def test_merge_same_value(): assert a.AnIntProp == 42 -@with_setup(setup_func, teardown_func) -def test_merge_unnamed(): +def test_merge_unnamed(with_global_instances): a = MyAlg(AnIntProp=42) assert not hasattr(a, "name") b = MyAlg(AStringProp="42") @@ -56,34 +45,33 @@ def test_merge_unnamed(): assert a.AStringProp == "42" -@raises(TypeError) def test_diff_type(): a = MyAlg() b = SimpleOptsAlgTool() - a.merge(b) + with pytest.raises(TypeError): + a.merge(b) -@raises(ValueError) def test_diff_name(): a = MyAlg("a") b = MyAlg("b") - a.merge(b) + with pytest.raises(ValueError): + a.merge(b) -@with_setup(setup_func, teardown_func) -@raises(ValueError) -def test_diff_unnamed(): +def test_diff_unnamed(with_global_instances): a = MyAlg("MyAlg") b = MyAlg() assert not hasattr(b, "name") - a.merge(b) + with pytest.raises(ValueError): + a.merge(b) -@raises(ValueError) def test_merge_conflict(): a = MyAlg("ThisAlg", AnIntProp=42) b = MyAlg("ThisAlg", AnIntProp=50) - a.merge(b) + with pytest.raises(ValueError): + a.merge(b) def test_merge_lists(): diff --git a/GaudiConfiguration/tests/nose/test_default_value_stability.py b/GaudiConfiguration/tests/python/test_default_value_stability.py similarity index 82% rename from GaudiConfiguration/tests/nose/test_default_value_stability.py rename to GaudiConfiguration/tests/python/test_default_value_stability.py index 98c01a9c1..8a3eff669 100644 --- a/GaudiConfiguration/tests/nose/test_default_value_stability.py +++ b/GaudiConfiguration/tests/python/test_default_value_stability.py @@ -8,22 +8,9 @@ # granted to it by virtue of its status as an Intergovernmental Organization # # or submit itself to any jurisdiction. # ##################################################################################### -from GaudiConfig2 import Configurable, useGlobalInstances -from nose.tools import with_setup -def setup_func(): - Configurable.instances.clear() - useGlobalInstances(True) - - -def teardown_func(): - Configurable.instances.clear() - useGlobalInstances(False) - - -@with_setup(setup_func, teardown_func) -def test_property_default(): +def test_property_default(with_global_instances): import GaudiConfig2.Configurables.TestConf as TC a = TC.AlgWithComplexProperty() @@ -41,8 +28,7 @@ def test_property_default(): assert b.TH.typeAndName == orig_b_value -@with_setup(setup_func, teardown_func) -def test_sequence_property_default(): +def test_sequence_property_default(with_global_instances): import GaudiConfig2.Configurables.TestConf as TC a = TC.AlgWithComplexProperty() @@ -57,8 +43,7 @@ def test_sequence_property_default(): assert list(b.VS) == [] -@with_setup(setup_func, teardown_func) -def test_set_property_default(): +def test_set_property_default(with_global_instances): import GaudiConfig2.Configurables.TestConf as TC a = TC.AlgWithComplexProperty() diff --git a/GaudiConfiguration/tests/nose/test_full_option_flow.py b/GaudiConfiguration/tests/python/test_full_option_flow.py similarity index 90% rename from GaudiConfiguration/tests/nose/test_full_option_flow.py rename to GaudiConfiguration/tests/python/test_full_option_flow.py index ef8a66d95..2e94e0a88 100644 --- a/GaudiConfiguration/tests/nose/test_full_option_flow.py +++ b/GaudiConfiguration/tests/python/test_full_option_flow.py @@ -13,10 +13,17 @@ import os import sys +import pytest from GaudiConfig2 import invokeConfig from GaudiConfig2._configurables import Configurable, Property from GaudiConfig2.semantics import SEMANTICS, PropertySemantics -from nose.tools import raises, with_setup + + +@pytest.fixture +def clear_instances(): + Configurable.instances.clear() + yield + Configurable.instances.clear() class InternalType(object): @@ -114,18 +121,14 @@ def test_set_and_get(): assert a.Option == dict(zip("abc", "123")) -@raises(AssertionError) -def test_set_bad_1(): - MyAlgo1(Option=(1, 2, 3, 4)) - - -@raises(AssertionError) -def test_set_bad_2(): - MyAlgo1(Option="ab") +def test_set_bad(): + with pytest.raises(AssertionError): + MyAlgo1(Option=(1, 2, 3, 4)) + with pytest.raises(AssertionError): + MyAlgo1(Option="ab") -@with_setup(Configurable.instances.clear, Configurable.instances.clear) -def test_to_cpp_string(): +def test_to_cpp_string(clear_instances): # default a = MyAlgo1("a") assert a.__opt_properties__() == {} @@ -163,11 +166,11 @@ def test_invoke_config_function_string(): sys.path.pop(0) -@raises(TypeError) def test_invoke_no_call(): - invokeConfig(tuple()) + with pytest.raises(TypeError): + invokeConfig(tuple()) -@raises(ValueError) def test_invoke_bad_string(): - invokeConfig("bad string") + with pytest.raises(ValueError): + invokeConfig("bad string") diff --git a/GaudiConfiguration/tests/nose/test_global_instances_flag.py b/GaudiConfiguration/tests/python/test_global_instances_flag.py similarity index 76% rename from GaudiConfiguration/tests/nose/test_global_instances_flag.py rename to GaudiConfiguration/tests/python/test_global_instances_flag.py index 0074b5cc6..11eb2be02 100644 --- a/GaudiConfiguration/tests/nose/test_global_instances_flag.py +++ b/GaudiConfiguration/tests/python/test_global_instances_flag.py @@ -9,23 +9,21 @@ # or submit itself to any jurisdiction. # ##################################################################################### import GaudiConfig2._configurables +import pytest from GaudiConfig2 import Configurable, useGlobalInstances from GaudiConfig2.Configurables.TestConf import MyAlg -from nose.tools import raises, with_setup -def setup_func(): +@pytest.fixture +def reset_global_instances_flag(): Configurable.instances.clear() GaudiConfig2._configurables._GLOBAL_INSTANCES = False - - -def teardown_func(): + yield Configurable.instances.clear() GaudiConfig2._configurables._GLOBAL_INSTANCES = False -@with_setup(setup_func, teardown_func) -def test_enable_disable(): +def test_enable_disable(reset_global_instances_flag): useGlobalInstances(False) assert not GaudiConfig2._configurables._GLOBAL_INSTANCES @@ -43,16 +41,14 @@ def test_enable_disable(): assert not GaudiConfig2._configurables._GLOBAL_INSTANCES -@with_setup(setup_func, teardown_func) -@raises(AssertionError) -def test_cannot_disable(): +def test_cannot_disable(reset_global_instances_flag): useGlobalInstances(True) MyAlg("a") - useGlobalInstances(False) + with pytest.raises(AssertionError): + useGlobalInstances(False) -@with_setup(setup_func, teardown_func) -def test_no_globals(): +def test_no_globals(reset_global_instances_flag): useGlobalInstances(True) anonymous = MyAlg() assert not hasattr(anonymous, "name") @@ -65,25 +61,22 @@ def test_no_globals(): assert a.name == MyAlg.__cpp_type__ -@with_setup(setup_func, teardown_func) -@raises(TypeError) -def test_cannot_delete_name(): +def test_cannot_delete_name(reset_global_instances_flag): useGlobalInstances(False) a = MyAlg() - del a.name + with pytest.raises(TypeError): + del a.name -@with_setup(setup_func, teardown_func) -@raises(TypeError) -def test_cannot_nullify_name_1(): +def test_cannot_nullify_name_1(reset_global_instances_flag): useGlobalInstances(False) a = MyAlg() - a.name = None + with pytest.raises(TypeError): + a.name = None -@with_setup(setup_func, teardown_func) -@raises(TypeError) -def test_cannot_nullify_name_2(): +def test_cannot_nullify_name_2(reset_global_instances_flag): useGlobalInstances(False) a = MyAlg() - a.name = "" + with pytest.raises(TypeError): + a.name = "" diff --git a/GaudiConfiguration/tests/nose/test_mapping_semantics.py b/GaudiConfiguration/tests/python/test_mapping_semantics.py similarity index 92% rename from GaudiConfiguration/tests/nose/test_mapping_semantics.py rename to GaudiConfiguration/tests/python/test_mapping_semantics.py index 735d99017..6dbbb4099 100644 --- a/GaudiConfiguration/tests/nose/test_mapping_semantics.py +++ b/GaudiConfiguration/tests/python/test_mapping_semantics.py @@ -8,9 +8,9 @@ # granted to it by virtue of its status as an Intergovernmental Organization # # or submit itself to any jurisdiction. # ##################################################################################### +import pytest from GaudiConfig2 import semantics as S from GaudiConfig2.Configurables.TestConf import AlgWithMaps -from nose.tools import raises def test_init_map(): @@ -46,12 +46,12 @@ def test_empyDict(): assert d == {} -@raises(RuntimeError) def test_default_read_only(): s = S.getSemanticsFor("std::map") d = s.default({"a": "A", "b": "B", "c": "C"}) assert len(d) == 3 - del d[1] + with pytest.raises(RuntimeError): + del d[1] def test_changes(): @@ -95,16 +95,16 @@ def test_access(): assert d.get("q", "Q") == "Q" -@raises(TypeError) def test_assignment_bad_value(): s = S.getSemanticsFor("std::map") - s.store({"a": "A", "b": "B", "c": 1}) + with pytest.raises(TypeError): + s.store({"a": "A", "b": "B", "c": 1}) -@raises(TypeError) def test_assignment_bad_key(): s = S.getSemanticsFor("std::map") - s.store({"a": "A", "b": "B", 1: "C"}) + with pytest.raises(TypeError): + s.store({"a": "A", "b": "B", 1: "C"}) def test_in_alg(): @@ -132,7 +132,7 @@ def test_nested_map_vector(): assert list(d[2]) == list("cde") -@raises(ValueError) def test_nested_map_vector_bad(): s = S.getSemanticsFor("map>") - s.store({1: ["a", "b"], 2: ["c", 0, "e"]}) + with pytest.raises(TypeError): + s.store({1: ["a", "b"], 2: ["c", 0, "e"]}) diff --git a/GaudiConfiguration/tests/nose/test_options_generation.py b/GaudiConfiguration/tests/python/test_options_generation.py similarity index 81% rename from GaudiConfiguration/tests/nose/test_options_generation.py rename to GaudiConfiguration/tests/python/test_options_generation.py index ac98001d2..b90479ebb 100644 --- a/GaudiConfiguration/tests/nose/test_options_generation.py +++ b/GaudiConfiguration/tests/python/test_options_generation.py @@ -8,7 +8,7 @@ # granted to it by virtue of its status as an Intergovernmental Organization # # or submit itself to any jurisdiction. # ##################################################################################### -from GaudiConfig2 import Configurable, useGlobalInstances +import pytest from GaudiConfig2.Configurables.TestConf import ( AlgWithComplexProperty, AlgWithMaps, @@ -16,39 +16,24 @@ SimpleOptsAlg, SimpleOptsAlgTool, ) -from nose.tools import raises, with_setup -def setup_func(): - Configurable.instances.clear() - useGlobalInstances(True) - - -def teardown_func(): - Configurable.instances.clear() - useGlobalInstances(False) - - -@with_setup(setup_func, teardown_func) -def test_opt_value(): +def test_opt_value(with_global_instances): assert SimpleOptsAlg("Dummy").__opt_value__() == "TestConf::SimpleOptsAlg/Dummy" del SimpleOptsAlg.instances["Dummy"].name -@with_setup(setup_func, teardown_func) -@raises(AttributeError) -def test_comp_requires_name(): - SimpleOptsAlg().__opt_value__() +def test_comp_requires_name(with_global_instances): + with pytest.raises(AttributeError): + SimpleOptsAlg().__opt_value__() -@with_setup(setup_func, teardown_func) -@raises(AttributeError) -def test_props_require_name(): - SimpleOptsAlg(AnIntProp=10).__opt_properties__() +def test_props_require_name(with_global_instances): + with pytest.raises(AttributeError): + SimpleOptsAlg(AnIntProp=10).__opt_properties__() -@with_setup(setup_func, teardown_func) -def test_props(): +def test_props(with_global_instances): p = SimpleOptsAlg("Dummy") assert p.__opt_properties__() == {} @@ -70,8 +55,7 @@ def test_props(): } -@with_setup(setup_func, teardown_func) -def test_special_cases_for_is_set_with_custom_handler(): +def test_special_cases_for_is_set_with_custom_handler(with_global_instances): p = SimpleOptsAlg("Dummy") assert p.__opt_properties__() == {} @@ -83,8 +67,7 @@ def test_special_cases_for_is_set_with_custom_handler(): } -@with_setup(setup_func, teardown_func) -def test_special_cases_for_is_set_with_default_handler(): +def test_special_cases_for_is_set_with_default_handler(with_global_instances): p = AlgWithComplexProperty("Dummy") assert p.__opt_properties__() == {} # just accessing is not setting, so it should not appear @@ -112,8 +95,7 @@ def test_special_cases_for_is_set_with_default_handler(): } -@with_setup(setup_func, teardown_func) -def test_all_options_default(): +def test_all_options_default(with_global_instances): SimpleOptsAlgTool("MyTool", parent=SimpleOptsAlg("Parent")) SimpleOptsAlg("AnotherAlg") @@ -131,8 +113,7 @@ def test_all_options_default(): } -@with_setup(setup_func, teardown_func) -def test_all_options(): +def test_all_options(with_global_instances): SimpleOptsAlgTool("MyTool", parent=SimpleOptsAlg("Parent", Int=9), Bool=False) SimpleOptsAlg("AnotherAlg", Int=0) @@ -154,8 +135,7 @@ def test_all_options(): } -@with_setup(setup_func, teardown_func) -def test_vector_options(): +def test_vector_options(with_global_instances): a = AlgWithVectors("a") a.VS = list("abc") @@ -174,8 +154,7 @@ def test_vector_options(): } -@with_setup(setup_func, teardown_func) -def test_map_options(): +def test_map_options(with_global_instances): a = AlgWithMaps("a") a.MSS = dict(zip("abc", "ABC")) diff --git a/GaudiConfiguration/tests/nose/test_semantics.py b/GaudiConfiguration/tests/python/test_semantics.py similarity index 76% rename from GaudiConfiguration/tests/nose/test_semantics.py rename to GaudiConfiguration/tests/python/test_semantics.py index f7452e2d5..41167521f 100644 --- a/GaudiConfiguration/tests/nose/test_semantics.py +++ b/GaudiConfiguration/tests/python/test_semantics.py @@ -8,7 +8,8 @@ # granted to it by virtue of its status as an Intergovernmental Organization # # or submit itself to any jurisdiction. # ##################################################################################### -from GaudiConfig2 import Configurable, useGlobalInstances +import pytest +from GaudiConfig2 import Configurable from GaudiConfig2.Configurables.TestConf import MyAlg from GaudiConfig2.semantics import ( ComponentSemantics, @@ -16,17 +17,6 @@ StringSemantics, getSemanticsFor, ) -from nose.tools import raises, with_setup - - -def setup_func(): - Configurable.instances.clear() - useGlobalInstances(True) - - -def teardown_func(): - Configurable.instances.clear() - useGlobalInstances(False) def test_semantics_lookup(): @@ -39,9 +29,9 @@ def test_string_ok(): assert s.store("something") == "something" -@raises(TypeError) def test_string_bad(): - getSemanticsFor("std::string").store(123) + with pytest.raises(TypeError): + getSemanticsFor("std::string").store(123) def test_semantics_delegation(): @@ -50,9 +40,9 @@ def test_semantics_delegation(): assert p.AStringProp == "something" -@raises(TypeError) def test_semantics_delegation_bad(): - MyAlg(AStringProp=123) + with pytest.raises(TypeError): + MyAlg(AStringProp=123) def test_no_change_after_exception(): @@ -83,10 +73,10 @@ def test_float_ok(): assert s.store(1e30) == 1e30 -@raises(TypeError) def test_float_bad(): s = getSemanticsFor("double") - s.store("number") + with pytest.raises(TypeError): + s.store("number") def test_int_ok(): @@ -95,19 +85,19 @@ def test_int_ok(): assert s.store(-999) == -999 -@raises(ValueError) def test_int_bad(): - getSemanticsFor("int").store(2**32) + with pytest.raises(ValueError): + getSemanticsFor("int").store(2**32) -@raises(ValueError) def test_uint_bad(): - getSemanticsFor("unsigned int").store(-1) + with pytest.raises(ValueError): + getSemanticsFor("unsigned int").store(-1) -@raises(TypeError) def test_int_bad_type(): - getSemanticsFor("int").store("42") + with pytest.raises(TypeError): + getSemanticsFor("int").store("42") def test_int_coercion(): @@ -116,8 +106,7 @@ def test_int_coercion(): assert s.store(True) == 1 -@with_setup(setup_func, teardown_func) -def test_alg_semantics(): +def test_alg_semantics(with_global_instances): s = getSemanticsFor("Algorithm") p = MyAlg("p") @@ -136,36 +125,31 @@ def test_alg_semantics(): assert q is Configurable.instances["q"] -@with_setup(setup_func, teardown_func) -@raises(AttributeError) -def test_alg_semantics_bad_type_name(): +def test_alg_semantics_bad_type_name(with_global_instances): s = getSemanticsFor("Algorithm") - s.store("GluGluGlu") + with pytest.raises(AttributeError): + s.store("GluGluGlu") -@with_setup(setup_func, teardown_func) -@raises(TypeError) -def test_alg_semantics_bad_type(): +def test_alg_semantics_bad_type(with_global_instances): s = getSemanticsFor("Algorithm") - s.store(123) + with pytest.raises(TypeError): + s.store(123) -@with_setup(setup_func, teardown_func) -@raises(TypeError) -def test_alg_semantics_bad_comp_type(): +def test_alg_semantics_bad_comp_type(with_global_instances): s = getSemanticsFor("Service") - s.store("TestConf::MyAlg") + with pytest.raises(TypeError): + s.store("TestConf::MyAlg") -@with_setup(setup_func, teardown_func) -@raises(AttributeError) -def test_alg_semantics_bad_unnamed_comp(): +def test_alg_semantics_bad_unnamed_comp(with_global_instances): s = getSemanticsFor("Algorithm") - s.store(MyAlg()) + with pytest.raises(AttributeError): + s.store(MyAlg()) -@with_setup(setup_func, teardown_func) -def test_interfaces_check(): +def test_interfaces_check(with_global_instances): s = getSemanticsFor("AlgTool") assert isinstance(s, ComponentSemantics) assert s.store("TestConf::SimpleOptsAlgTool") @@ -187,39 +171,34 @@ def test_interfaces_check(): assert s.store("TestConf::SimpleOptsAlgTool") -@with_setup(setup_func, teardown_func) -@raises(TypeError) -def test_interfaces_check_bad1(): +def test_interfaces_check_bad1(with_global_instances): s = getSemanticsFor("AlgTool:IToolTypeA") assert isinstance(s, ComponentSemantics) - assert s.store("TestConf::SimpleOptsAlgTool") + with pytest.raises(TypeError): + s.store("TestConf::SimpleOptsAlgTool") -@with_setup(setup_func, teardown_func) -@raises(TypeError) -def test_interfaces_check_bad2(): +def test_interfaces_check_bad2(with_global_instances): s = getSemanticsFor("AlgTool:IToolType1,IToolTypeA") assert isinstance(s, ComponentSemantics) - assert s.store("TestConf::SimpleOptsAlgTool") + with pytest.raises(TypeError): + s.store("TestConf::SimpleOptsAlgTool") -@with_setup(setup_func, teardown_func) -@raises(TypeError) -def test_interfaces_check_bad3(): +def test_interfaces_check_bad3(with_global_instances): s = getSemanticsFor("AlgTool:IToolType1,IToolType2,IToolTypeA") - assert s.store("TestConf::SimpleOptsAlgTool") + with pytest.raises(TypeError): + s.store("TestConf::SimpleOptsAlgTool") -@with_setup(setup_func, teardown_func) -def test_component_default(): +def test_component_default(with_global_instances): from GaudiConfig2 import Configurables as C a = C.TestConf.SimpleOptsAlg() assert isinstance(a.Tool, C.TestConf.SimpleOptsAlgTool) -@with_setup(setup_func, teardown_func) -def test_parent_handling(): +def test_parent_handling(with_global_instances): import GaudiConfig2.Configurables.TestConf as TC p = TC.SimpleOptsAlg("Dummy") diff --git a/GaudiConfiguration/tests/nose/test_sequence_semantics.py b/GaudiConfiguration/tests/python/test_sequence_semantics.py similarity index 93% rename from GaudiConfiguration/tests/nose/test_sequence_semantics.py rename to GaudiConfiguration/tests/python/test_sequence_semantics.py index 94c600435..82b5efc9f 100644 --- a/GaudiConfiguration/tests/nose/test_sequence_semantics.py +++ b/GaudiConfiguration/tests/python/test_sequence_semantics.py @@ -8,9 +8,9 @@ # granted to it by virtue of its status as an Intergovernmental Organization # # or submit itself to any jurisdiction. # ##################################################################################### +import pytest from GaudiConfig2 import semantics as S from GaudiConfig2.Configurables.TestConf import AlgWithVectors -from nose.tools import raises def test_init_vector(): @@ -37,12 +37,12 @@ def test_default(): assert d == list("abc") -@raises(RuntimeError) def test_default_read_only(): s = S.getSemanticsFor("std::vector >") d = s.default(["a", "b", "c"]) assert len(d) == 3 - del d[1] + with pytest.raises(RuntimeError): + del d[1] def test_comparison(): @@ -84,10 +84,10 @@ def test_opt_value(): assert s.opt_value(d) == ["a", "b", "c"] -@raises(ValueError) def test_assignment_bad(): s = S.getSemanticsFor("std::vector >") - s.store(["a", "b", 1]) + with pytest.raises(TypeError): + s.store(["a", "b", 1]) def test_in_alg(): @@ -115,10 +115,10 @@ def test_nested_vector(): assert d[1] == list("cde") -@raises(ValueError) def test_nested_vector_bad(): s = S.getSemanticsFor("std::vector, a >") - s.store([["a", "b"], ["c", 0, "e"]]) + with pytest.raises(TypeError): + s.store([["a", "b"], ["c", 0, "e"]]) def test_sequence_semantics(): @@ -137,13 +137,13 @@ def test_merge(): s.merge(s1, s2) -@raises(ValueError) def test_merge_fail(): s = S.getSemanticsFor("std::vector") s1 = s.store([1, 2]) s2 = s.store([1, 1]) assert s1 != s2 - s.merge(s1, s2) + with pytest.raises(ValueError): + s.merge(s1, s2) def testValueSem(): diff --git a/GaudiConfiguration/tests/nose/test_serialization.py b/GaudiConfiguration/tests/python/test_serialization.py similarity index 86% rename from GaudiConfiguration/tests/nose/test_serialization.py rename to GaudiConfiguration/tests/python/test_serialization.py index 19ef11520..621dd6d4a 100644 --- a/GaudiConfiguration/tests/nose/test_serialization.py +++ b/GaudiConfiguration/tests/python/test_serialization.py @@ -8,24 +8,13 @@ # granted to it by virtue of its status as an Intergovernmental Organization # # or submit itself to any jurisdiction. # ##################################################################################### -from GaudiConfig2 import Configurable, all_options, useGlobalInstances +from GaudiConfig2 import Configurable, all_options from GaudiConfig2.Configurables.TestConf import ( AlgWithMaps, AlgWithVectors, MyAlg, SimpleOptsAlgTool, ) -from nose.tools import with_setup - - -def setup_func(): - Configurable.instances.clear() - useGlobalInstances(True) - - -def teaardown_func(): - Configurable.instances.clear() - useGlobalInstances(False) class MyDerivedAlg(MyAlg): @@ -36,8 +25,7 @@ def __init__(self): self.AnIntProp = 42 -@with_setup(setup_func, teaardown_func) -def test_repr(): +def test_repr(with_global_instances): p = MyAlg(AnIntProp=42) q = eval(repr(p)) @@ -73,8 +61,7 @@ def test_repr(): assert repr(q) == repr(p) -@with_setup(setup_func, teaardown_func) -def test_pickle(): +def test_pickle(with_global_instances): from pickle import dumps, loads p = MyAlg(AnIntProp=42) @@ -117,8 +104,7 @@ def test_pickle(): assert dumps(q) == dumps(p) -@with_setup(setup_func, teaardown_func) -def test_full_serialization_repr(): +def test_full_serialization_repr(with_global_instances): MyAlg("Alg1", AnIntProp=1) SimpleOptsAlgTool("ToolA", parent=MyAlg("Alg2", AnIntProp=2)) AlgWithVectors("AV", VS="abc") @@ -131,8 +117,7 @@ def test_full_serialization_repr(): assert all_options(explicit_defaults=True) == opts -@with_setup(setup_func, teaardown_func) -def test_full_serialization_pickle(): +def test_full_serialization_pickle(with_global_instances): from pickle import dumps, loads MyAlg("Alg1", AnIntProp=1)