-
Notifications
You must be signed in to change notification settings - Fork 24.2k
Move patch_ansible_module to a pytest plugin
#66430
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: devel
Are you sure you want to change the base?
Move patch_ansible_module to a pytest plugin
#66430
Conversation
This comment was marked as outdated.
This comment was marked as outdated.
This comment has been minimized.
This comment has been minimized.
1d8fb63 to
4768355
Compare
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
This comment has been minimized.
4768355 to
c0ee9ee
Compare
c0ee9ee to
2b4e004
Compare
|
(docs commit for history, could go into commit 5b4c415e55fcc5e8a4d988a25c29bee8144c0cca
Author: Sviatoslav Sydorenko <webknjaz@redhat.com>
Date: Thu Apr 1 23:19:26 2021 +0200
Document pytest fixtures for module unit-tests
diff --git a/docs/docsite/rst/dev_guide/testing_units_modules.rst b/docs/docsite/rst/dev_guide/testing_units_modules.rst
index d07dcff9448..276cdf9f91c 100644
--- a/docs/docsite/rst/dev_guide/testing_units_modules.rst
+++ b/docs/docsite/rst/dev_guide/testing_units_modules.rst
@@ -544,6 +544,72 @@ creation of module objects for testing.
The same restructuring technique can be valuable for testing other functionality, such as the part of the module which queries the object that the module configures.
+What About The Pytest Fixtures?
+===============================
+
+.. versionadded:: 2.15
+
+
+We now have some pytest-native fixture-based helpers that simplify
+calling modules from unit tests using an interface close to the one
+available in playbooks.
+
+
+.. code-block:: python
+ :caption: test_new_mod.py
+
+ import pytest
+
+
+ @pytest.fixture
+ def ansible_global_module_name():
+ return 'my_module'
+
+
+ def test_new_mod_caller(make_ansible_module_caller):
+ run_my_module = make_ansible_module_caller('my_module')
+
+ my_module_args = {'arg1': 'val1', 'arg2': 2, 'arg3': [1, 2, 3]}
+ expected_result = {}
+
+ assert run_my_module(**my_module_args) == expected_result
+
+
+ def test_new_mod_yaml(make_ansible_module_caller):
+ run_my_module = make_ansible_module_caller('my_module')
+
+ assert run_my_module.with_yaml("""
+ arg1: val1
+ arg2: val2
+ """) == {}
+
+
+ def test_new_mod_global(run_ansible_module):
+ my_module_args = {'arg1': 'val1', 'arg2': 2, 'arg3': [1, 2, 3]}
+ expected_result = {}
+
+ assert run_ansible_module(**my_module_args) == expected_result
+
+
+ def test_new_mod_error(run_ansible_module):
+ my_module_args = {'arg1': 'val1'}
+
+ failed_response = run_module(**my_module_args)
+
+ assert failed_response['failed']
+
+
+ @pytest.mark.parametrize(
+ 'ansible_module_name',
+ ('my_module2', ),
+ indirect=('ansible_module_name', ),
+ )
+ def test_new_mod2(run_ansible_module):
+ my_module_args = {'arg1': 'val1', 'arg2': 2, 'arg3': [1, 2, 3]}
+ expected_result = {}
+
+ assert run_ansible_module(**my_module_args) == expected_result
+
Traps for maintaining Python 2 compatibility
============================================ |
This patch automatically loads patch_ansible_module fixture into pytest when running `ansible-test units`. It previously existed in `conftest.py` hence only available to tests of ansible/ansible itself. Now, third-party collections will be able to use it without having to vendor the source code.
dfc1da5 to
2d088cf
Compare
|
The test The test The test The test The test The test The test The test The test |
This patch automatically loads
patch_ansible_modulefixture intopytest when running
ansible-test units. It previously existedin
conftest.pyhence only available to tests of ansible/ansibleitself. Now, third-party collections will be able to use it without
having to vendor the source code.
SUMMARY
As per above: this exposes
patch_ansible_modulefixture to any unit tests executed viaansible-test.ISSUE TYPE
COMPONENT NAME
ansible-test
ADDITIONAL INFORMATION
N/A