From ffca7eaf52b4d077ec8f5aac04e32dc7729473e0 Mon Sep 17 00:00:00 2001 From: "patchback[bot]" <45432694+patchback[bot]@users.noreply.github.com> Date: Wed, 13 Sep 2023 07:58:02 +0200 Subject: [PATCH] [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 6e3a56ffcea12398889b7ae8ba142181de201708) Co-authored-by: Alexei Znamensky <103110+russoz@users.noreply.github.com> --- tests/unit/plugins/modules/helper.py | 24 ++++++++++++++++--- tests/unit/plugins/modules/test_cpanm.py | 20 +++------------- tests/unit/plugins/modules/test_gconftool2.py | 20 +++------------- .../plugins/modules/test_gconftool2_info.py | 20 +++------------- tests/unit/plugins/modules/test_opkg.py | 20 +++------------- tests/unit/plugins/modules/test_puppet.py | 20 +++------------- tests/unit/plugins/modules/test_snap.py | 20 +++------------- tests/unit/plugins/modules/test_xfconf.py | 20 +++------------- .../unit/plugins/modules/test_xfconf_info.py | 20 +++------------- 9 files changed, 45 insertions(+), 139 deletions(-) diff --git a/tests/unit/plugins/modules/helper.py b/tests/unit/plugins/modules/helper.py index 10e43242d5..b02fe88635 100644 --- a/tests/unit/plugins/modules/helper.py +++ b/tests/unit/plugins/modules/helper.py @@ -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): diff --git a/tests/unit/plugins/modules/test_cpanm.py b/tests/unit/plugins/modules/test_cpanm.py index d508638f7f..7f8c8660a2 100644 --- a/tests/unit/plugins/modules/test_cpanm.py +++ b/tests/unit/plugins/modules/test_cpanm.py @@ -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 diff --git a/tests/unit/plugins/modules/test_gconftool2.py b/tests/unit/plugins/modules/test_gconftool2.py index a1a4c6df51..11e6255a0a 100644 --- a/tests/unit/plugins/modules/test_gconftool2.py +++ b/tests/unit/plugins/modules/test_gconftool2.py @@ -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 diff --git a/tests/unit/plugins/modules/test_gconftool2_info.py b/tests/unit/plugins/modules/test_gconftool2_info.py index 986e2632d6..d7e0670673 100644 --- a/tests/unit/plugins/modules/test_gconftool2_info.py +++ b/tests/unit/plugins/modules/test_gconftool2_info.py @@ -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 diff --git a/tests/unit/plugins/modules/test_opkg.py b/tests/unit/plugins/modules/test_opkg.py index 62a4a2157a..03c160e00d 100644 --- a/tests/unit/plugins/modules/test_opkg.py +++ b/tests/unit/plugins/modules/test_opkg.py @@ -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 diff --git a/tests/unit/plugins/modules/test_puppet.py b/tests/unit/plugins/modules/test_puppet.py index 858c2c4365..ff8b894e89 100644 --- a/tests/unit/plugins/modules/test_puppet.py +++ b/tests/unit/plugins/modules/test_puppet.py @@ -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 diff --git a/tests/unit/plugins/modules/test_snap.py b/tests/unit/plugins/modules/test_snap.py index 41e339c3b8..8f31828604 100644 --- a/tests/unit/plugins/modules/test_snap.py +++ b/tests/unit/plugins/modules/test_snap.py @@ -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 diff --git a/tests/unit/plugins/modules/test_xfconf.py b/tests/unit/plugins/modules/test_xfconf.py index 001d2279dc..5a082fccc3 100644 --- a/tests/unit/plugins/modules/test_xfconf.py +++ b/tests/unit/plugins/modules/test_xfconf.py @@ -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 diff --git a/tests/unit/plugins/modules/test_xfconf_info.py b/tests/unit/plugins/modules/test_xfconf_info.py index e24b6022ce..d59da8fa08 100644 --- a/tests/unit/plugins/modules/test_xfconf_info.py +++ b/tests/unit/plugins/modules/test_xfconf_info.py @@ -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