import pytest
from unittest import mock
from unittest.mock import patch
from ansible.inventory.data import InventoryData
from plugins.inventory.myinvnetoryplugin import InventoryModule
from ansible.parsing.dataloader import DataLoader
config_data = {
'plugin': 'my_inventory'
}
@patch('my_plugin.InventoryModule._read_config_data')
@patch('my_plugin.InventoryModule.get_cache_key') # if you have caching enabled
def test_parse(mock_key, mock_config):
inventory = InventoryModule()
inventory.load_cache_plugin = mock.MagicMock()
inventory.inventory = InventoryData()
loader = DataLoader()
mock_config.return_value = config_data
mock_key.return_value = 'test'
# pass inventory, loader, path and cache parameter to parse method
inventory.parse(inventory.inventory, loader, mock_conf, False)
groups = inventory.inventory.get_groups_dict()
# Write your asserts here
assert 'test' in groups
assert 'host1' in inventory.inventory.hosts