question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

[bug] Testing and mocking docs are wrong

See original GitHub issue

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

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:5
  • Comments:15 (5 by maintainers)

github_iconTop GitHub Comments

2reactions
limdautocommented, Feb 14, 2021

I think the easiest way is to dump a settings file to a temporary location and load it. Something like:

@pytest.fixture
def mock_settings_path(tmpdir):
    settings_path = tmpdir.mkdir("test_package").join("settings.py")
    settings_path.write(
        textwrap.dedent(
            f"""
                SETTING_ONE = True
            """
        )
    )
    return str(settings_path) 

And you can use the fixture as follows:

def test_settings_file(mock_settings_path):
    settings = LazySettings(settings_file=mock_settings_path)
    assert SETTING_ONE == True

The beauty of this approach is that you can actually add a Mock to your mock settings file, e.g.

# in your test module
MOCK_SETTING_TWO = MagicMock()

# in your mock settings file
f"""
from {__name__} import MOCK_SETTING_TWO
SETTING_TWO = MOCK_SETTING_TWO
"""
2reactions
mazzicommented, Dec 15, 2020

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.


    from dynaconf import LazySettings
    from dynaconf.loaders.json_loader import load

    mocked_settings = LazySettings(environments=True, ENV_FOR_DYNACONF="DEFAULT")

    data = """
    {
        "default" : {
            "SETTING_ONE": false,
            "SETTING_TWO": "test"
        }
    }
    """
    load(mocked_settings, filename=data)

Thanks!

Another option would be to use Monkeypatch

Read more comments on GitHub >

github_iconTop Results From Across the Web

[bug] Testing and mocking docs are wrong · Issue #264 - GitHub
Describe the bug The docs in https://dynaconf.readthedocs.io/en/latest/guides/testing.html explain how to test with dynaconf.
Read more >
How do I test a system where the objects are difficult to mock?
Unit tests are fast and easy to run. They catch bugs in individual sections of code and use mocks to make running them...
Read more >
How to test software: mocking, stubbing, and contract testing
In this friendly overview, explore the use of practices such as mocking, stubbing, and contract testing to get the right level of validation ......
Read more >
Mocking is a Code Smell. Note: This is part of the “Composing…
One of the biggest complaints I hear about TDD and unit tests is that people struggle with all of the mocking required to...
Read more >
Best practices for writing unit tests - .NET - Microsoft Learn
Learn best practices for writing unit tests that drive code ... If you call your stubs "mocks," other developers are going to make...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found