Skip to content

Commit

Permalink
GaudiConfig2: converted tests to pytest
Browse files Browse the repository at this point in the history
  • Loading branch information
pikacic committed Sep 12, 2023
1 parent 0e96fb2 commit cbd838c
Show file tree
Hide file tree
Showing 16 changed files with 164 additions and 253 deletions.
10 changes: 2 additions & 8 deletions GaudiConfiguration/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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 $<TARGET_FILE:nosetests> -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
File renamed without changes.
21 changes: 21 additions & 0 deletions GaudiConfiguration/tests/python/conftest.py
Original file line number Diff line number Diff line change
@@ -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)
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand Down Expand Up @@ -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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand All @@ -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)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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():
Expand All @@ -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")
Expand All @@ -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():
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand All @@ -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()
Expand All @@ -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()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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):
Expand Down Expand Up @@ -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__() == {}
Expand Down Expand Up @@ -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")
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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")
Expand All @@ -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 = ""
Loading

0 comments on commit cbd838c

Please sign in to comment.