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

[PR #7247/6e3a56ff backport][stable-7] encapsulate test function in the helper class (#7255)

encapsulate test function in the helper class (#7247)

(cherry picked from commit 6e3a56ffce)

Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com>
This commit is contained in:
patchback[bot] 2023-09-13 07:58:02 +02:00 committed by GitHub
parent 5b9b98340b
commit ffca7eaf52
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"])
class CmdRunnerTestHelper(object):
class Helper(object):
@staticmethod
def from_list(module_main, list_):
helper = CmdRunnerTestHelper(module_main, test_cases=list_)
helper = Helper(module_main, test_cases=list_)
return helper
@staticmethod
def from_file(module_main, filename):
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
def __init__(self, module_main, test_cases):
@ -75,6 +75,24 @@ class CmdRunnerTestHelper(object):
def __call__(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):
def __init__(self, helper, testcase, mocker, capfd):

View file

@ -13,24 +13,10 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import pytest
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
@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()
test_module = helper.test_module

View file

@ -7,24 +7,10 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import pytest
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
@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()
test_module = helper.test_module

View file

@ -7,24 +7,10 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import pytest
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
@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()
test_module = helper.test_module

View file

@ -7,24 +7,10 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import pytest
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
@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()
test_module = helper.test_module

View file

@ -13,24 +13,10 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import pytest
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
@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()
test_module = helper.test_module

View file

@ -6,9 +6,7 @@
from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import pytest
from .helper import CmdRunnerTestHelper, ModuleTestCase, RunCmdCall
from .helper import Helper, ModuleTestCase, RunCmdCall
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
@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()
test_module = helper.test_module

View file

@ -13,24 +13,10 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import pytest
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
@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()
test_module = helper.test_module

View file

@ -6,24 +6,10 @@ from __future__ import (absolute_import, division, print_function)
__metaclass__ = type
import pytest
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
@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()
test_module = helper.test_module