Skip to content

Instantly share code, notes, and snippets.

@rnrbarbosa
Created December 23, 2022 21:45
Show Gist options
  • Save rnrbarbosa/a153a65540b9db2c5b8e9e76f75f1440 to your computer and use it in GitHub Desktop.
Save rnrbarbosa/a153a65540b9db2c5b8e9e76f75f1440 to your computer and use it in GitHub Desktop.
Unit Test for Ansible Module and Plugin
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.'
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