# Unit testing an inventory plugin

**URL:** <https://forum.ansible.com/t/unit-testing-an-inventory-plugin/36899>\
**Category:** Ansible Developer\
**Created:** [July 25, 2022, 1:11pm UTC](https://forum.ansible.com/t/unit-testing-an-inventory-plugin/36899 "2022-07-25T13:11:48Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![Jean\_Seurin](https://avatars.discourse-cdn.com/v4/letter/j/db5fbb/32.png) [@Jean\_Seurin](https://forum.ansible.com/u/Jean_Seurin)\
**Post date:** [July 25, 2022, 1:11pm UTC](https://forum.ansible.com/t/unit-testing-an-inventory-plugin/36899/1 "2022-07-25T13:11:48Z")

</div>

This topic has been raised 3 years ago but it provided no answer.

I want to be able to unittest an Inventory plugin I am developing.

Basically, all I need is a way to execute the `parse(self, inventory, loader, path, cache=True)` method of my class extending `BaseInventoryPlugin, Constructable`.

I have been able to come up with something simple that was working:

def test\_parse\_with\_valid\_config(self):  
source = os.path.join(os.getcwd(), ‘test/valid\_test\_conf.yml’)  
loader = DataLoader()  
inventory = InventoryData()  
inventory.current\_source = source  
plugin = InventoryModule()

# trick to be able to invoke the parse function only

plugin.\_redirected\_names = [‘myplugin’]  
plugin.\_load\_name = u’odewe’

# end trick

plugin.parse(inventory, loader, inventory.current\_source)  
#assertion on constructed inventory can be implemented here.

But since I started to use options to pass variables, this doesn’t cut it anymore.  
I’ve tried debugging all I can to see how option were set but it seems impossible to set them without calling the cli itself.

AFAIK, I have to call the cli with subprocess this way:

subprocess.call([sys.executable, os.path.join(os.path.dirname(sys.executable), “ansible-inventory”), ‘-i’, ‘config.yml’, ‘–playbook-dir’, ‘.’, ‘–list’])

but then I have no way to access the internal inventory produced.

All I’m left with now would be to dump the result to a file and build python objects from the file to be able to make assertions, but that would really be ugly.

Kind regards
