Needs some help with pytest

In our new project click-types we want to start with test (pytest). We wrote two test cases. One works well (not so sophisticated yet) but the other one does not will work.

this is the one that won’t work. I get strange error messages that I don’t understand:

Hint: make sure your test modules/packages have valid Python names.
Traceback:
/home/nero/.pyenv/versions/3.8.7/lib/python3.8/importlib/__init__.py:127: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
tests/test_cases/ansible.py:7: in <module>
    from click_types.ansible import AnsibleVaultParamType as vault
click_types/ansible.py:7: in <module>
    from ansible.errors import AnsibleError
E   ModuleNotFoundError: No module named 'ansible.errors'; 'ansible' is not a package

If I do the steps from importing and doing in a python shell and call convert method by hand the param type do what it should.

I want to get a clue on that but currently I don’t have any more idea to get it.

That looks like your click_types.ansible module is trying to import a module ansible.errors that’s not available. Most likely you haven’t installed the Ansible modules, or at least not where your Python setup is looking for them.

Thanks for quick answer. I already check that. See output from pip:

$ python -m pip freeze
ansible==4.0.0
ansible-core==2.11.1
ansible-vault==2.1.0
...

That’s why I’m wondering.

Hm, that makes me wonder if that command and your tests use the same environment. Maybe a virtual environment or something is involved?

It’s hard to debug without a closer look at your system, but maybe these are helpful:

  • sys.path tells you where the current Python interpreter is searching for modules.
  • You can look up modules in sys.modules to see where they were loaded from, or look at e.g. ansible.errors.__spec__ (after successful import where it works).
1 Like

Found the issue with your suggested calls. The problem was that the file that provides the tests was called ansible.py. So as the types module try to import ansible.errors always the test files was tried to import. And there was now ansible.errors. After renaming the test all runs well.

1 Like