diff --git a/changelogs/fragments/ansible-test-pytest-plugin-loading.yml b/changelogs/fragments/ansible-test-pytest-plugin-loading.yml new file mode 100644 index 00000000000..2a052a7ff16 --- /dev/null +++ b/changelogs/fragments/ansible-test-pytest-plugin-loading.yml @@ -0,0 +1,2 @@ +bugfixes: + - ansible-test now loads the collection loader plugin early enough for ansible_collections imports to work in unit test conftest.py modules diff --git a/test/lib/ansible_test/_data/pytest/plugins/ansible_pytest_collections.py b/test/lib/ansible_test/_data/pytest/plugins/ansible_pytest_collections.py index ae87187b835..91e20478786 100644 --- a/test/lib/ansible_test/_data/pytest/plugins/ansible_pytest_collections.py +++ b/test/lib/ansible_test/_data/pytest/plugins/ansible_pytest_collections.py @@ -1,4 +1,4 @@ -"""Enable unit testing of Ansible collections.""" +"""Enable unit testing of Ansible collections. PYTEST_DONT_REWRITE""" from __future__ import (absolute_import, division, print_function) __metaclass__ = type @@ -20,6 +20,12 @@ def collection_pypkgpath(self): def pytest_configure(): """Configure this pytest plugin.""" + try: + if pytest_configure.executed: + return + except AttributeError: + pytest_configure.executed = True + from ansible.utils.collection_loader import AnsibleCollectionLoader # allow unit tests to import code from collections @@ -32,3 +38,6 @@ def pytest_configure(): # original idea from https://stackoverflow.com/questions/50174130/how-do-i-pytest-a-project-using-pep-420-namespace-packages/50175552#50175552 # noinspection PyProtectedMember py._path.local.LocalPath.pypkgpath = collection_pypkgpath # pylint: disable=protected-access + + +pytest_configure() diff --git a/test/lib/ansible_test/_data/pytest/plugins/ansible_pytest_coverage.py b/test/lib/ansible_test/_data/pytest/plugins/ansible_pytest_coverage.py index 344a091e1c5..b05298ab0b3 100644 --- a/test/lib/ansible_test/_data/pytest/plugins/ansible_pytest_coverage.py +++ b/test/lib/ansible_test/_data/pytest/plugins/ansible_pytest_coverage.py @@ -1,10 +1,16 @@ -"""Monkey patch os._exit when running under coverage so we don't lose coverage data in forks, such as with `pytest --boxed`.""" +"""Monkey patch os._exit when running under coverage so we don't lose coverage data in forks, such as with `pytest --boxed`. PYTEST_DONT_REWRITE""" from __future__ import (absolute_import, division, print_function) __metaclass__ = type def pytest_configure(): """Configure this pytest plugin.""" + try: + if pytest_configure.executed: + return + except AttributeError: + pytest_configure.executed = True + try: import coverage except ImportError: @@ -57,3 +63,6 @@ def pytest_configure(): if cov: cov.start() + + +pytest_configure() diff --git a/test/lib/ansible_test/_internal/units/__init__.py b/test/lib/ansible_test/_internal/units/__init__.py index 15655c4c1d3..22145431117 100644 --- a/test/lib/ansible_test/_internal/units/__init__.py +++ b/test/lib/ansible_test/_internal/units/__init__.py @@ -122,9 +122,7 @@ def command_units(args): if plugins: env['PYTHONPATH'] += ':%s' % os.path.join(ANSIBLE_TEST_DATA_ROOT, 'pytest/plugins') - - for plugin in plugins: - cmd.extend(['-p', plugin]) + env['PYTEST_PLUGINS'] = ','.join(plugins) if args.collect_only: cmd.append('--collect-only')