Created
December 23, 2022 21:45
-
-
Save rnrbarbosa/a153a65540b9db2c5b8e9e76f75f1440 to your computer and use it in GitHub Desktop.
Unit Test for Ansible Module and Plugin
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
from ansible.module_utils.basic import AnsibleModule | |
from my_plugin import main | |
@pytest.fixture | |
def fake_module(): | |
module = ansibleModule( | |
argument_spec=dict( | |
name=dict(type='str'), | |
age=dict(type='int') | |
) | |
) | |
return module | |
def test_main(fake_module): | |
# Set the module's parameters | |
fake_module.params = dict(name='John', age=30) | |
# Call the plugin's main function | |
main() | |
# Assert that the plugin returned the expected results | |
assert fake_module.exit_json.called | |
assert fake_module.exit_json.call_args[0][0]['changed'] == False | |
assert fake_module.exit_json.call_args[0][0]['message'] == 'Hello John, you are 30 years old.' |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import pytest | |
from ansible.module_utils.basic import AnsibleModule | |
from my_plugin import main | |
@pytest.fixture | |
def fake_module(): | |
module = ansibleModule( | |
argument_spec=dict( | |
name=dict(type='str'), | |
age=dict(type='int') | |
) | |
) | |
return module | |
def test_main(fake_module): | |
# Set the module's parameters | |
fake_module.params = dict(name='John', age=30) | |
# Call the plugin's main function | |
main() | |
# Assert that the plugin returned the expected results | |
assert fake_module.exit_json.called | |
assert fake_module.exit_json.call_args[0][0]['changed'] == False | |
assert fake_module.exit_json.call_args[0][0]['message'] == 'Hello John, you are 30 years old.' |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment