1
0
Fork 0
mirror of https://github.com/ansible-collections/community.general.git synced 2024-09-14 20:13:21 +02:00

encapsulate test function in the helper class (#7247)

This commit is contained in:
Alexei Znamensky 2023-09-13 00:24:10 +12:00 committed by GitHub
parent bdf8852e8d
commit 6e3a56ffce
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
9 changed files with 45 additions and 139 deletions

View file

@ -18,16 +18,16 @@ ModuleTestCase = namedtuple("ModuleTestCase", ["id", "input", "output", "run_com
RunCmdCall = namedtuple("RunCmdCall", ["command", "environ", "rc", "out", "err"]) RunCmdCall = namedtuple("RunCmdCall", ["command", "environ", "rc", "out", "err"])
class CmdRunnerTestHelper(object): class Helper(object):
@staticmethod @staticmethod
def from_list(module_main, list_): def from_list(module_main, list_):
helper = CmdRunnerTestHelper(module_main, test_cases=list_) helper = Helper(module_main, test_cases=list_)
return helper return helper
@staticmethod @staticmethod
def from_file(module_main, filename): def from_file(module_main, filename):
with open(filename, "r") as TEST_CASES: with open(filename, "r") as TEST_CASES:
helper = CmdRunnerTestHelper(module_main, test_cases=TEST_CASES) helper = Helper(module_main, test_cases=TEST_CASES)
return helper return helper
def __init__(self, module_main, test_cases): def __init__(self, module_main, test_cases):
@ -75,6 +75,24 @@ class CmdRunnerTestHelper(object):
def __call__(self, *args, **kwargs): def __call__(self, *args, **kwargs):
return _Context(self, *args, **kwargs) return _Context(self, *args, **kwargs)
@property
def test_module(self):
helper = self
@pytest.mark.parametrize('patch_ansible_module, testcase',
helper.testcases_params, ids=helper.testcases_ids,
indirect=['patch_ansible_module'])
@pytest.mark.usefixtures('patch_ansible_module')
def _test_module(mocker, capfd, patch_bin, testcase):
"""
Run unit tests for test cases listed in TEST_CASES
"""
with helper(testcase, mocker, capfd) as testcase_context:
testcase_context.run()
return _test_module
class _Context(object): class _Context(object):
def __init__(self, helper, testcase, mocker, capfd): def __init__(self, helper, testcase, mocker, capfd):

View file

@ -13,24 +13,10 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import pytest
from ansible_collections.community.general.plugins.modules import cpanm from ansible_collections.community.general.plugins.modules import cpanm
from .helper import CmdRunnerTestHelper from .helper import Helper
helper = CmdRunnerTestHelper.from_file(cpanm.main, "tests/unit/plugins/modules/test_cpanm.yaml") helper = Helper.from_file(cpanm.main, "tests/unit/plugins/modules/test_cpanm.yaml")
patch_bin = helper.cmd_fixture patch_bin = helper.cmd_fixture
test_module = helper.test_module
@pytest.mark.parametrize('patch_ansible_module, testcase',
helper.testcases_params, ids=helper.testcases_ids,
indirect=['patch_ansible_module'])
@pytest.mark.usefixtures('patch_ansible_module')
def test_module(mocker, capfd, patch_bin, testcase):
"""
Run unit tests for test cases listed in TEST_CASES
"""
with helper(testcase, mocker, capfd) as testcase_context:
testcase_context.run()

View file

@ -7,24 +7,10 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import pytest
from ansible_collections.community.general.plugins.modules import gconftool2 from ansible_collections.community.general.plugins.modules import gconftool2
from .helper import CmdRunnerTestHelper from .helper import Helper
helper = CmdRunnerTestHelper.from_file(gconftool2.main, "tests/unit/plugins/modules/test_gconftool2.yaml") helper = Helper.from_file(gconftool2.main, "tests/unit/plugins/modules/test_gconftool2.yaml")
patch_bin = helper.cmd_fixture patch_bin = helper.cmd_fixture
test_module = helper.test_module
@pytest.mark.parametrize('patch_ansible_module, testcase',
helper.testcases_params, ids=helper.testcases_ids,
indirect=['patch_ansible_module'])
@pytest.mark.usefixtures('patch_ansible_module')
def test_module(mocker, capfd, patch_bin, testcase):
"""
Run unit tests for test cases listed in TEST_CASES
"""
with helper(testcase, mocker, capfd) as testcase_context:
testcase_context.run()

View file

@ -7,24 +7,10 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import pytest
from ansible_collections.community.general.plugins.modules import gconftool2_info from ansible_collections.community.general.plugins.modules import gconftool2_info
from .helper import CmdRunnerTestHelper from .helper import Helper
helper = CmdRunnerTestHelper.from_file(gconftool2_info.main, "tests/unit/plugins/modules/test_gconftool2_info.yaml") helper = Helper.from_file(gconftool2_info.main, "tests/unit/plugins/modules/test_gconftool2_info.yaml")
patch_bin = helper.cmd_fixture patch_bin = helper.cmd_fixture
test_module = helper.test_module
@pytest.mark.parametrize('patch_ansible_module, testcase',
helper.testcases_params, ids=helper.testcases_ids,
indirect=['patch_ansible_module'])
@pytest.mark.usefixtures('patch_ansible_module')
def test_module(mocker, capfd, patch_bin, testcase):
"""
Run unit tests for test cases listed in TEST_CASES
"""
with helper(testcase, mocker, capfd) as testcase_context:
testcase_context.run()

View file

@ -7,24 +7,10 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import pytest
from ansible_collections.community.general.plugins.modules import opkg from ansible_collections.community.general.plugins.modules import opkg
from .helper import CmdRunnerTestHelper from .helper import Helper
helper = CmdRunnerTestHelper.from_file(opkg.main, "tests/unit/plugins/modules/test_opkg.yaml") helper = Helper.from_file(opkg.main, "tests/unit/plugins/modules/test_opkg.yaml")
patch_bin = helper.cmd_fixture patch_bin = helper.cmd_fixture
test_module = helper.test_module
@pytest.mark.parametrize('patch_ansible_module, testcase',
helper.testcases_params, ids=helper.testcases_ids,
indirect=['patch_ansible_module'])
@pytest.mark.usefixtures('patch_ansible_module')
def test_module(mocker, capfd, patch_bin, testcase):
"""
Run unit tests for test cases listed in TEST_CASES
"""
with helper(testcase, mocker, capfd) as testcase_context:
testcase_context.run()

View file

@ -13,24 +13,10 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import pytest
from ansible_collections.community.general.plugins.modules import puppet from ansible_collections.community.general.plugins.modules import puppet
from .helper import CmdRunnerTestHelper from .helper import Helper
helper = CmdRunnerTestHelper.from_file(puppet.main, "tests/unit/plugins/modules/test_puppet.yaml") helper = Helper.from_file(puppet.main, "tests/unit/plugins/modules/test_puppet.yaml")
patch_bin = helper.cmd_fixture patch_bin = helper.cmd_fixture
test_module = helper.test_module
@pytest.mark.parametrize('patch_ansible_module, testcase',
helper.testcases_params, ids=helper.testcases_ids,
indirect=['patch_ansible_module'])
@pytest.mark.usefixtures('patch_ansible_module')
def test_module(mocker, capfd, patch_bin, testcase):
"""
Run unit tests for test cases listed in TEST_CASES
"""
with helper(testcase, mocker, capfd) as testcase_context:
testcase_context.run()

View file

@ -6,9 +6,7 @@
from __future__ import (absolute_import, division, print_function) from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import pytest from .helper import Helper, ModuleTestCase, RunCmdCall
from .helper import CmdRunnerTestHelper, ModuleTestCase, RunCmdCall
from ansible_collections.community.general.plugins.modules import snap from ansible_collections.community.general.plugins.modules import snap
@ -444,18 +442,6 @@ TEST_CASES = [
), ),
] ]
helper = CmdRunnerTestHelper.from_list(snap.main, TEST_CASES) helper = Helper.from_list(snap.main, TEST_CASES)
patch_bin = helper.cmd_fixture patch_bin = helper.cmd_fixture
test_module = helper.test_module
@pytest.mark.parametrize('patch_ansible_module, testcase',
helper.testcases_params, ids=helper.testcases_ids,
indirect=['patch_ansible_module'])
@pytest.mark.usefixtures('patch_ansible_module')
def test_module(mocker, capfd, patch_bin, testcase):
"""
Run unit tests for test cases listed in TEST_CASES
"""
with helper(testcase, mocker, capfd) as testcase_context:
testcase_context.run()

View file

@ -13,24 +13,10 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import pytest
from ansible_collections.community.general.plugins.modules import xfconf from ansible_collections.community.general.plugins.modules import xfconf
from .helper import CmdRunnerTestHelper from .helper import Helper
helper = CmdRunnerTestHelper.from_file(xfconf.main, "tests/unit/plugins/modules/test_xfconf.yaml") helper = Helper.from_file(xfconf.main, "tests/unit/plugins/modules/test_xfconf.yaml")
patch_bin = helper.cmd_fixture patch_bin = helper.cmd_fixture
test_module = helper.test_module
@pytest.mark.parametrize('patch_ansible_module, testcase',
helper.testcases_params, ids=helper.testcases_ids,
indirect=['patch_ansible_module'])
@pytest.mark.usefixtures('patch_ansible_module')
def test_module(mocker, capfd, patch_bin, testcase):
"""
Run unit tests for test cases listed in TEST_CASES
"""
with helper(testcase, mocker, capfd) as testcase_context:
testcase_context.run()

View file

@ -6,24 +6,10 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type __metaclass__ = type
import pytest
from ansible_collections.community.general.plugins.modules import xfconf_info from ansible_collections.community.general.plugins.modules import xfconf_info
from .helper import CmdRunnerTestHelper from .helper import Helper
helper = CmdRunnerTestHelper.from_file(xfconf_info.main, "tests/unit/plugins/modules/test_xfconf_info.yaml") helper = Helper.from_file(xfconf_info.main, "tests/unit/plugins/modules/test_xfconf_info.yaml")
patch_bin = helper.cmd_fixture patch_bin = helper.cmd_fixture
test_module = helper.test_module
@pytest.mark.parametrize('patch_ansible_module, testcase',
helper.testcases_params, ids=helper.testcases_ids,
indirect=['patch_ansible_module'])
@pytest.mark.usefixtures('patch_ansible_module')
def test_module(mocker, capfd, patch_bin, testcase):
"""
Run unit tests for test cases listed in TEST_CASES
"""
with helper(testcase, mocker, capfd) as testcase_context:
testcase_context.run()