diff --git a/tests/utils/test_logging.py b/tests/utils/test_logging.py index 7b7aa1a2a..9d02874a4 100644 --- a/tests/utils/test_logging.py +++ b/tests/utils/test_logging.py @@ -28,33 +28,32 @@ from tuning.utils.logging import set_log_level -@mock.patch.dict(os.environ, {}, clear=True) def test_set_log_level_for_logger_default(): """ Ensure that the correct log level is being set for python native logger and transformers logger when no env var or CLI flag is passed """ + + with mock.patch.dict(os.environ, {}, clear=True): + train_args = copy.deepcopy(TRAIN_ARGS) + training_args, logger = set_log_level(train_args) + assert logger.getEffectiveLevel() == logging.WARNING + assert training_args.log_level == "passive" - train_args = copy.deepcopy(TRAIN_ARGS) - training_args, logger = set_log_level(train_args) - assert logger.getEffectiveLevel() == logging.WARNING - assert training_args.log_level == "passive" - -@mock.patch.dict(os.environ, {"LOG_LEVEL": "info"}, clear=True) def test_set_log_level_for_logger_with_env_var(): """ Ensure that the correct log level is being set for python native logger and transformers logger when env var LOG_LEVEL is used """ - train_args_env = copy.deepcopy(TRAIN_ARGS) - training_args, logger = set_log_level(train_args_env) - assert logger.getEffectiveLevel() == logging.INFO - assert training_args.log_level == "info" + with mock.patch.dict(os.environ, {"LOG_LEVEL": "info"}, clear=True): + train_args_env = copy.deepcopy(TRAIN_ARGS) + training_args, logger = set_log_level(train_args_env) + assert logger.getEffectiveLevel() == logging.INFO + assert training_args.log_level == "info" -@mock.patch.dict(os.environ, {"TRANSFORMERS_VERBOSITY": "info"}, clear=True) def test_set_log_level_for_logger_with_set_verbosity_and_cli(): """ Ensure that the correct log level is being set for python native logger and @@ -62,14 +61,14 @@ def test_set_log_level_for_logger_with_set_verbosity_and_cli(): and CLI flag is passed """ - train_args = copy.deepcopy(TRAIN_ARGS) - train_args.log_level = "error" - training_args, logger = set_log_level(train_args) - assert logger.getEffectiveLevel() == logging.ERROR - assert training_args.log_level == "error" + with mock.patch.dict(os.environ, {"TRANSFORMERS_VERBOSITY": "info"}, clear=True): + train_args = copy.deepcopy(TRAIN_ARGS) + train_args.log_level = "error" + training_args, logger = set_log_level(train_args) + assert logger.getEffectiveLevel() == logging.ERROR + assert training_args.log_level == "error" -@mock.patch.dict(os.environ, {"LOG_LEVEL": "info"}, clear=True) def test_set_log_level_for_logger_with_env_var_and_cli(): """ Ensure that the correct log level is being set for python native logger and @@ -77,8 +76,9 @@ def test_set_log_level_for_logger_with_env_var_and_cli(): In this case, CLI arg takes precedence over the set env var LOG_LEVEL. """ - train_args = copy.deepcopy(TRAIN_ARGS) - train_args.log_level = "error" - training_args, logger = set_log_level(train_args) - assert logger.getEffectiveLevel() == logging.ERROR - assert training_args.log_level == "error" + with mock.patch.dict(os.environ, {"LOG_LEVEL": "info"}, clear=True): + train_args = copy.deepcopy(TRAIN_ARGS) + train_args.log_level = "error" + training_args, logger = set_log_level(train_args) + assert logger.getEffectiveLevel() == logging.ERROR + assert training_args.log_level == "error"