Skip to content
Snippets Groups Projects
Commit b9abf6ff authored by Marco Clemencic's avatar Marco Clemencic
Browse files

GaudiKernel: migrate tests to pytest and various fixes

See merge request !1517
parents e3adce27 a14dbd07
No related branches found
No related tags found
1 merge request!1517GaudiKernel: migrate tests to pytest and various fixes
Pipeline #6430596 passed
...@@ -400,7 +400,7 @@ if(BUILD_TESTING) ...@@ -400,7 +400,7 @@ if(BUILD_TESTING)
LABELS "Gaudi.GaudiKernel" LABELS "Gaudi.GaudiKernel"
) )
gaudi_add_pytest(tests/nose) gaudi_add_pytest(tests/pytest)
gaudi_add_module(test_CustomFactory gaudi_add_module(test_CustomFactory
SOURCES tests/src/custom_factory.cpp SOURCES tests/src/custom_factory.cpp
......
##################################################################################### #####################################################################################
# (c) Copyright 1998-2020 CERN for the benefit of the LHCb and ATLAS collaborations # # (c) Copyright 1998-2023 CERN for the benefit of the LHCb and ATLAS collaborations #
# # # #
# This software is distributed under the terms of the Apache version 2 licence, # # This software is distributed under the terms of the Apache version 2 licence, #
# copied verbatim in the file "LICENSE". # # copied verbatim in the file "LICENSE". #
...@@ -8,16 +8,19 @@ ...@@ -8,16 +8,19 @@
# granted to it by virtue of its status as an Intergovernmental Organization # # granted to it by virtue of its status as an Intergovernmental Organization #
# or submit itself to any jurisdiction. # # or submit itself to any jurisdiction. #
##################################################################################### #####################################################################################
# Prepare dummy configurables import pytest
from GaudiKernel.Configurable import Configurable, ConfigurableAlgorithm from GaudiKernel.Configurable import Configurable, ConfigurableAlgorithm
from GaudiKernel.DataHandle import DataHandle from GaudiKernel.DataHandle import DataHandle
# Prepare dummy configurables
class MyAlg(ConfigurableAlgorithm): class MyAlg(ConfigurableAlgorithm):
__slots__ = { __slots__ = {
"Text": "some text", "Text": "some text",
"Int": 23, "Int": 23,
"DataHandle": DataHandle("Location", "R"), "DataHandle": DataHandle("Location", "R"),
"Dict": {},
"List": [],
} }
def getDlls(self): def getDlls(self):
...@@ -27,7 +30,10 @@ class MyAlg(ConfigurableAlgorithm): ...@@ -27,7 +30,10 @@ class MyAlg(ConfigurableAlgorithm):
return "MyAlg" return "MyAlg"
def _clean_confs(): @pytest.fixture(autouse=True)
def clean_confs():
"""ensure that all tests start from a clean configuration"""
MyAlg.configurables.clear()
Configurable.allConfigurables.clear() Configurable.allConfigurables.clear()
...@@ -41,10 +47,14 @@ def test_correct(): ...@@ -41,10 +47,14 @@ def test_correct():
a.Int = 42 a.Int = 42
a.Text = "value" a.Text = "value"
a.DataHandle = "/Event/X" a.DataHandle = "/Event/X"
a.Dict = {"a": 1}
a.List = [1, 2]
assert a.getValuedProperties() == { assert a.getValuedProperties() == {
"Int": 42, "Int": 42,
"Text": "value", "Text": "value",
"DataHandle": DataHandle("/Event/X", "R"), "DataHandle": DataHandle("/Event/X", "R"),
"Dict": {"a": 1},
"List": [1, 2],
} }
...@@ -56,51 +66,34 @@ def test_str_from_datahandle(): ...@@ -56,51 +66,34 @@ def test_str_from_datahandle():
def test_invalid_value(): def test_invalid_value():
a = MyAlg() a = MyAlg()
try:
with pytest.raises(ValueError):
a.Int = "value" a.Int = "value"
assert False, "exception expected"
except AssertionError: with pytest.raises(ValueError):
raise
except ValueError:
pass
except Exception as x:
assert False, "ValueError exception expected, got %s" % type(x).__name__
try:
a.Text = [123] a.Text = [123]
assert False, "exception expected"
except AssertionError: with pytest.raises(ValueError):
raise
except ValueError:
pass
except Exception as x:
assert False, "ValueError exception expected, got %s" % type(x).__name__
try:
a.DataHandle = [123] a.DataHandle = [123]
assert False, "exception expected"
except AssertionError: with pytest.raises(ValueError):
raise a.Dict = []
except ValueError:
pass with pytest.raises(ValueError):
except Exception as x: a.List = {}
assert False, "ValueError exception expected, got %s" % type(x).__name__
def test_invalid_key(): def test_invalid_key():
a = MyAlg() a = MyAlg()
try:
with pytest.raises(AttributeError):
a.Dummy = "abc" a.Dummy = "abc"
assert False, "exception expected"
except AssertionError:
raise def test_collection_defaults():
except AttributeError: a = MyAlg()
pass assert a.Dict == {}
except Exception as x: assert a.List == []
assert False, "AttributeError exception expected, got %s" % type(x).__name__
a.List += [1]
assert a.List == [1]
# ensure that all tests start from clean configuration
for _f in dir():
if _f.startswith("test"):
setattr(locals()[_f], "setup", _clean_confs)
...@@ -11,16 +11,14 @@ ...@@ -11,16 +11,14 @@
from platform import processor from platform import processor
from subprocess import PIPE, Popen from subprocess import PIPE, Popen
from nose import SkipTest import pytest
@pytest.mark.skipif(
processor() != "x86_64",
reason=f"platform {processor()} not supported (instructionsetLevel only works on x86_64)",
)
def test(): def test():
if processor() != "x86_64":
raise SkipTest(
"platform {} not supported (instructionsetLevel only works on x86_64".format(
processor()
)
)
out = Popen(["instructionsetLevel", "all"], stdout=PIPE).communicate()[0] out = Popen(["instructionsetLevel", "all"], stdout=PIPE).communicate()[0]
out = out.decode("utf-8") out = out.decode("utf-8")
known_flags = set(l.strip() for l in out.splitlines()) known_flags = set(l.strip() for l in out.splitlines())
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment