#๐Ÿ”’ How to create app config? My constants are too constant.

24 messages ยท Page 1 of 1 (latest)

thorn tree
#
import os
from dotenv import load_dotenv

load_dotenv(override=True)

FOCUSTUI_DEBUG: bool = os.getenv("FOCUSTUI_DEBUG") == "True"
_minute = os.getenv("FOCUSTUI_DEBUG_MINUTE")
is_custom = FOCUSTUI_DEBUG and _minute is not None
MINUTE: int = int(_minute) if is_custom else 60

This part of the constants from my app. As you see, this data can be changed by .env file. When I am working on an app, I set, for example, debug to True.

I want my unit tests to work against normal mode (debug=False). But when I import data from constants, debug is set already set to True, so monkeypatch.setenv("FOCUSTUI_DEBUG", "False") does not work.

How do design my app to make it easy to test and easy to write?

Or I just should manually set .env debug to True?

true canopyBOT
#

@thorn tree

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

quaint turtle
#

the easiest way is just putting the code that depends on environment variables inside of functions that are ran after you have enough time to modify them via monkeypatching

#

(aka no global variables)
alternatively, set the environment variables normally before running pytest

thorn tree
quaint turtle
#

> MY_ENV=SOMETHING py -m pytest

#

or a command line script with a bunch of sets instead of running it directly

thorn tree
#

btw I love your avatar it is so cute ๐Ÿค—

quaint turtle
#

not sure, personally I would just make some helper function instead of using a variable

#

like just ```py
def is_debug() -> bool:
return os.getenv("FOCUSTUI_DEBUG") == "True"

from config import is_debug

instead of

if FOCUSTUI_DEBUG:
...

just

if is_debug():
...

#

instead of a MINUTE global, some get_duration() function

thorn tree
quaint turtle
#

yeah idk, you may want to try asking in #unit-testing too

thorn tree
quaint turtle
#

in some cases, you could also mock the variable where it's imported to instead of mocking its origin

thorn tree
#

Can you tell me where did you found your avatar or you have made it?

quaint turtle
#

created with ChatGPT + Dalle3

thorn tree
thorn tree
#

but thanks for your insights py_strong

true canopyBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.