diff options
author | Matt Clay <matt@mystile.com> | 2022-06-09 08:11:45 -0700 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-06-09 10:11:45 -0500 |
commit | 117414e5c2a778c5ed38f65e9f6b2566cae6c0a1 (patch) | |
tree | 0f9a0cea95db7d267aa760e127006e42973fa912 | |
parent | 482ce8fa8bf100ccad50f10fd8116fd732ae99db (diff) | |
download | ansible-117414e5c2a778c5ed38f65e9f6b2566cae6c0a1.tar.gz |
[stable-2.12] ansible-test - Adjust unit test mock usage. (#78003)
* ansible-test - Prefer unittest.mock for core.
(cherry picked from commit 81351022d8d1c0133b47af57f993f4e82f6c1a67)
* ansible-test - Adjust unit test mock usage. (#77961)
(cherry picked from commit 23914d3f0b5d69e7710c3744f373203c8caa6693)
5 files changed, 26 insertions, 10 deletions
diff --git a/changelogs/fragments/ansible-test-ansible-core-mock.yml b/changelogs/fragments/ansible-test-ansible-core-mock.yml new file mode 100644 index 0000000000..60ea2d25fd --- /dev/null +++ b/changelogs/fragments/ansible-test-ansible-core-mock.yml @@ -0,0 +1,2 @@ +minor_changes: + - ansible-test - Avoid using the ``mock_use_standalone_module`` setting for unit tests running on Python 3.8 or later. diff --git a/test/lib/ansible_test/_data/pytest.ini b/test/lib/ansible_test/_data/pytest.ini deleted file mode 100644 index c1c38ff786..0000000000 --- a/test/lib/ansible_test/_data/pytest.ini +++ /dev/null @@ -1,9 +0,0 @@ -[pytest] -xfail_strict = true -mock_use_standalone_module = true -# It was decided to stick with "legacy" (aka "xunit1") for now. -# Currently used pytest versions all support xunit2 format too. -# Except the one used under Python 2.6 - it doesn't process this option -# at all. Ref: -# https://github.com/ansible/ansible/pull/66445#discussion_r372530176 -junit_family = xunit1 diff --git a/test/lib/ansible_test/_data/pytest/config/default.ini b/test/lib/ansible_test/_data/pytest/config/default.ini new file mode 100644 index 0000000000..60575bfe32 --- /dev/null +++ b/test/lib/ansible_test/_data/pytest/config/default.ini @@ -0,0 +1,4 @@ +[pytest] +xfail_strict = true +# avoid using 'mock_use_standalone_module = true' so package maintainers can avoid packaging 'mock' +junit_family = xunit1 diff --git a/test/lib/ansible_test/_data/pytest/config/legacy.ini b/test/lib/ansible_test/_data/pytest/config/legacy.ini new file mode 100644 index 0000000000..b2668dc287 --- /dev/null +++ b/test/lib/ansible_test/_data/pytest/config/legacy.ini @@ -0,0 +1,4 @@ +[pytest] +xfail_strict = true +mock_use_standalone_module = true +junit_family = xunit1 diff --git a/test/lib/ansible_test/_internal/commands/units/__init__.py b/test/lib/ansible_test/_internal/commands/units/__init__.py index 9a2b182754..995f715937 100644 --- a/test/lib/ansible_test/_internal/commands/units/__init__.py +++ b/test/lib/ansible_test/_internal/commands/units/__init__.py @@ -21,6 +21,7 @@ from ...util import ( ANSIBLE_TEST_DATA_ROOT, display, is_subdir, + str_to_version, SubprocessError, ANSIBLE_LIB_ROOT, ANSIBLE_TEST_TARGET_ROOT, @@ -235,6 +236,20 @@ def command_units(args): # type: (UnitsConfig) -> None sys.exit() for test_context, python, paths, env in test_sets: + # When using pytest-mock, make sure that features introduced in Python 3.8 are available to older Python versions. + # This is done by enabling the mock_use_standalone_module feature, which forces use of mock even when unittest.mock is available. + # Later Python versions have not introduced additional unittest.mock features, so use of mock is not needed as of Python 3.8. + # If future Python versions introduce new unittest.mock features, they will not be available to older Python versions. + # Having the cutoff at Python 3.8 also eases packaging of ansible-core since no supported controller version requires the use of mock. + # + # NOTE: This only affects use of pytest-mock. + # Collection unit tests may directly import mock, which will be provided by ansible-test when it installs requirements using pip. + # Although mock is available for ansible-core unit tests, they should import units.compat.mock instead. + if str_to_version(python.version) < (3, 8): + config_name = 'legacy.ini' + else: + config_name = 'default.ini' + cmd = [ 'pytest', '--forked', @@ -243,7 +258,7 @@ def command_units(args): # type: (UnitsConfig) -> None '--color', 'yes' if args.color else 'no', '-p', 'no:cacheprovider', - '-c', os.path.join(ANSIBLE_TEST_DATA_ROOT, 'pytest.ini'), + '-c', os.path.join(ANSIBLE_TEST_DATA_ROOT, 'pytest', 'config', config_name), '--junit-xml', os.path.join(ResultType.JUNIT.path, 'python%s-%s-units.xml' % (python.version, test_context)), ] |