# Common test utility functions

**URL:** <https://forum.ansible.com/t/common-test-utility-functions/34610>\
**Category:** Ansible Developer\
**Created:** [February 11, 2021, 3:08pm UTC](https://forum.ansible.com/t/common-test-utility-functions/34610 "2021-02-11T15:08:31Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![opoplawski](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/opoplawski/32/2112_2.png) [@opoplawski](https://forum.ansible.com/u/opoplawski)\
**Post date:** [February 11, 2021, 3:08pm UTC](https://forum.ansible.com/t/common-test-utility-functions/34610/1 "2021-02-11T15:08:31Z")

</div>

I'm starting to migrate my module project into a collection and I'm running into issues with missing test utility functions like:

tests/unit/plugins/lookup/test\_pfsense.py:10: in \<module\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from units.compat.mock import patch  
E ImportError: No module named units.compat.mock

tests/unit/plugins/modules/test\_pfsense\_aggregate.py:13: in \<module\>  
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from units.modules.utils import set\_module\_args  
E ImportError: No module named units.modules.utils

From poking around in the various collection repositories it appears that many collections are now carrying local copies of these. Is this really the plan here? It seems - not ideal.

Thanks for any insight,  
&nbsp;&nbsp;&nbsp;Orion

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [February 11, 2021, 3:15pm UTC](https://forum.ansible.com/t/common-test-utility-functions/34610/2 "2021-02-11T15:15:06Z")

</div>

Hi,

> I'm starting to migrate my module project into a collection and I'm  
> running into issues with missing test utility functions like:
> 
> tests/unit/plugins/lookup/test\_pfsense.py:10: in \<module\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from units.compat.mock import patch  
> E ImportError: No module named units.compat.mock
> 
> tests/unit/plugins/modules/test\_pfsense\_aggregate.py:13: in \<module\>  
> &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;from units.modules.utils import set\_module\_args  
> E ImportError: No module named units.modules.utils
> 
> From poking around in the various collection repositories it appears  
> that many collections are now carrying local copies of these. Is  
> this really the plan here? It seems - not ideal.

I'm not very happy about this either. My personal recommendation  
(slightly biased 😉 ) for collections would be to import them from  
[https://galaxy.ansible.com/community/internal\_test\_utils](https://galaxy.ansible.com/community/internal_test_utils) /  
[https://github.com/ansible-collections/community.internal\_test\_tools](https://github.com/ansible-collections/community.internal_test_tools).  
That of course requires you to install that collection for unit tests,  
though...

Cheers,  
Felix

---

<div class="post-metadata">

**Author:** ![opoplawski](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/opoplawski/32/2112_2.png) [@opoplawski](https://forum.ansible.com/u/opoplawski)\
**Post date:** [February 13, 2021, 10:57pm UTC](https://forum.ansible.com/t/common-test-utility-functions/34610/3 "2021-02-13T22:57:42Z")

</div>

Thanks, I've used that in my project. I wonder if a tests dependency tag is in order for galaxy.yml?

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [February 14, 2021, 1:17pm UTC](https://forum.ansible.com/t/common-test-utility-functions/34610/4 "2021-02-14T13:17:21Z")

</div>

Hi,

> \> I'm not very happy about this either. My personal recommendation  
> \> (slightly biased 😉 ) for collections would be to import them from  
> \> [https://galaxy.ansible.com/community/internal\_test\_utils](https://galaxy.ansible.com/community/internal_test_utils) /  
> \> [https://github.com/ansible-collections/community.internal\_test\_tools](https://github.com/ansible-collections/community.internal_test_tools).  
> \> That of course requires you to install that collection for unit  
> \> tests, though...
> 
> Thanks, I've used that in my project. I wonder if a tests dependency  
> tag is in order for galaxy.yml?

good question. From how I understood it, adding things to galaxy.yml is  
complicated since it is converted to MANIFEST.json during collection  
build, and that might be strictly validated by Galaxy / AH itself.

In any case, there already exists a "proposal" for a test dependency  
file: tests/requirements.yml

(See for example  
[https://github.com/ansible-collections/community.general/blob/main/tests/requirements.yml](https://github.com/ansible-collections/community.general/blob/main/tests/requirements.yml))

This file was created by the collection migration tool used when moving  
content from ansible/ansible into collection repositories. It is not  
used by ansible-test at the moment, and I have no idea whether there  
are actual plans of supporting it eventually.

(Right now, dependencies are installed 'manually' during CI:  
[https://github.com/ansible-collections/community.general/blob/main/tests/utils/shippable/shippable.sh#L76-L89](https://github.com/ansible-collections/community.general/blob/main/tests/utils/shippable/shippable.sh#L76-L89))

Cheers,  
Felix

---

<div class="post-metadata">

**Author:** ![Idont\_Haveaname](https://avatars.discourse-cdn.com/v4/letter/i/49beb7/32.png) [@Idont\_Haveaname](https://forum.ansible.com/u/Idont_Haveaname)\
**Post date:** [May 15, 2022, 7:14pm UTC](https://forum.ansible.com/t/common-test-utility-functions/34610/5 "2022-05-15T19:14:39Z")

</div>

Hello,

I had a look to this interesting internal test collection, yet I cannot make it work, my ansible-test units command keeps complaining that the following path cannot be found:

from ansible\_collections.community.internal\_test\_tools.tests.unit.compat import unittest  
from ansible\_collections.community.internal\_test\_tools.tests.unit.compat.mock import Mock, patch

What is the procedure to follow to set it up ?

Thanks !

Cédric S.

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [May 15, 2022, 7:31pm UTC](https://forum.ansible.com/t/common-test-utility-functions/34610/6 "2022-05-15T19:31:01Z")

</div>

Hello,

> I had a look to this interesting internal test collection, yet I  
> cannot make it work, my ansible-test units command keeps complaining  
> that the following path cannot be found:
> 
> from  
> ansible\_collections.community.internal\_test\_tools.tests.unit.compat  
> import unittest from  
> ansible\_collections.community.internal\_test\_tools.tests.unit.compat.mock  
> import Mock, patch
> 
> What is the procedure to follow to set it up ?

did you install the collection in the same path structure that the  
collection you are running ansible-test on is in?

I.e. do you have

/path/to/ansible\_collections/community/internal\_test\_tools/  
/path/to/ansible\_collections/yournamespace/yourcollection/

in the same directory structure?

Best,  
Felix

---

<div class="post-metadata">

**Author:** ![Idont\_Haveaname](https://avatars.discourse-cdn.com/v4/letter/i/49beb7/32.png) [@Idont\_Haveaname](https://forum.ansible.com/u/Idont_Haveaname)\
**Post date:** [May 15, 2022, 7:34pm UTC](https://forum.ansible.com/t/common-test-utility-functions/34610/7 "2022-05-15T19:34:45Z")

</div>

Hello,

I didn’t, I’ve used ansible-galaxy install which did install the collection in .ansible/ansible\_collections/…

Am I supposed to do a git clone inside the same path of the collection I’m developing ? This wasn’t clear for me 🙂

Thanks !

Cédric S.

---

<div class="post-metadata">

**Author:** ![Idont\_Haveaname](https://avatars.discourse-cdn.com/v4/letter/i/49beb7/32.png) [@Idont\_Haveaname](https://forum.ansible.com/u/Idont_Haveaname)\
**Post date:** [May 15, 2022, 7:38pm UTC](https://forum.ansible.com/t/common-test-utility-functions/34610/8 "2022-05-15T19:38:24Z")

</div>

So, git cloning next to my work-in-progress collection allowed me to do import it,

Thanks again, it’s a bit fuzy to find correct examples of “Getting started with collection testing” 🙂

Regards,

Cédric S.

---

<div class="post-metadata">

**Author:** ![felixfontein](https://sea1.discourse-cdn.com/ansible/user_avatar/forum.ansible.com/felixfontein/32/7530_2.png) [@felixfontein](https://forum.ansible.com/u/felixfontein)\
**Post date:** [May 15, 2022, 8:23pm UTC](https://forum.ansible.com/t/common-test-utility-functions/34610/9 "2022-05-15T20:23:22Z")

</div>

Hi,

> I didn't, I've used ansible-galaxy install which did install the  
> collection in .ansible/ansible\_collections/...
> 
> Am I supposed to do a git clone inside the same path of the  
> collection I'm developing ? This wasn't clear for me 🙂

you can also tell `ansible-galaxy collection install` to install it  
there, so using `git clone` is not necessary.

That the collection has to live in that place is a requirement of  
ansible-test.

> Thanks again, it's a bit fuzy to find correct examples of "Getting  
> started with collection testing" 🙂

That's indeed one of the may places where docs can be improved.  
Contributors are always welcome:  
[https://github.com/ansible/ansible/tree/devel/docs/docsite/rst/](https://github.com/ansible/ansible/tree/devel/docs/docsite/rst/) 😉

Cheers,  
Felix
