dynaconf: [bug] Testing and mocking docs are wrong
Describe the bug
The docs in https://dynaconf.readthedocs.io/en/latest/guides/testing.html explain how to test with dynaconf. The point is that if you follow the explanation, you will see AttributeError: 'DynaconfDict' object has no attribute 'current_env'
To Reproduce
Just try to follow the steps in https://dynaconf.readthedocs.io/en/latest/guides/testing.html
Expected behavior The code don’t fail and the mock can be used.
Debug output
Debug Output
Traceback (most recent call last):
File "lalala.py", line 5, in <module>
toml_loader.load(mocked_settings, filename="test_conf.toml", env="testing")
File "/home/angel/.virtualenvs/navi-Pag0EvBg/lib/python3.6/site-packages/dynaconf/loaders/toml_loader.py", line 38, in load
loader.load(filename=filename, key=key, silent=silent)
File "/home/angel/.virtualenvs/navi-Pag0EvBg/lib/python3.6/site-packages/dynaconf/loaders/base.py", line 69, in load
env_list = build_env_list(self.obj, self.env)
File "/home/angel/.virtualenvs/navi-Pag0EvBg/lib/python3.6/site-packages/dynaconf/utils/__init__.py", line 220, in build_env_list
if obj.current_env and obj.current_env not in env_list:
AttributeError: 'DynaconfDict' object has no attribute 'current_env'
Environment (please complete the following information):
- OS: Ubuntu 18.04
- Dynaconf Version 2.2
About this issue
- Original URL
- State: closed
- Created 5 years ago
- Reactions: 5
- Comments: 15 (5 by maintainers)
I think the easiest way is to dump a settings file to a temporary location and load it. Something like:
And you can use the fixture as follows:
The beauty of this approach is that you can actually add a
Mockto your mock settings file, e.g.Nice insight! @rochacbruno So actually there’s no way to mock settings? I was digging in the tests but didn’t find anything to load a json or a dict and being able to mock tests.
I wrote something like this. Not optimum but for now it works ok for my purposes. Maybe somebody finds it useful.
Thanks!
Another option would be to use Monkeypatch
@mazzi In fact, MonkeyPatching Dynaconf settings in Pytest doesn’t work, unfortunately. Setting values don’t change that way.
We have the same issue here with additional accessors as
as_dictoras_inthere: