mindspore2022/tests/ut/python/train/test_run_config.py

71 lines
2.9 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Copyright 2020 Huawei Technologies Co., Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ============================================================================
""" test_run_config """
import pytest
''''
从MindSpore库中导入CheckpointConfig类
指定训练过程中保存检查点的条件
''''
from mindspore.train.callback import CheckpointConfig
'''
定义测试函数通过断言来验证在初始化'CheckpointConfig'类后,
对象的属性是否被正确设置,并确保'get_checkpoint_policy()'方法能够正确返回检查点策略的相关信息。
'''
def test_init():
""" test_init """
save_checkpoint_steps = 1
keep_checkpoint_max = 5
config = CheckpointConfig(save_checkpoint_steps,
keep_checkpoint_max)
assert config.save_checkpoint_steps == save_checkpoint_steps
assert config.keep_checkpoint_max == keep_checkpoint_max
policy = config.get_checkpoint_policy()
assert policy['keep_checkpoint_max'] == keep_checkpoint_max
'''
函数 test_arguments_values()验证'CheckpointConfig'类在初始化和参数设置方面的正确性,
以确保在使用该类时能够提供有效的参数,并正确处理可能出现的异常情况。
这是一种测试驱动开发Test-Driven DevelopmentTDD的实践方式
通过编写测试来规范类的行为和功能,保证其在使用中的稳定性和正确性。
'''
def test_arguments_values():
""" test_arguments_values """
config = CheckpointConfig()
assert config.save_checkpoint_steps == 1
assert config.save_checkpoint_seconds is None
assert config.keep_checkpoint_max == 5
assert config.keep_checkpoint_per_n_minutes is None
with pytest.raises(TypeError):
CheckpointConfig(save_checkpoint_steps='abc')
with pytest.raises(TypeError):
CheckpointConfig(save_checkpoint_seconds='abc')
with pytest.raises(TypeError):
CheckpointConfig(keep_checkpoint_max='abc')
with pytest.raises(TypeError):
CheckpointConfig(keep_checkpoint_per_n_minutes='abc')
with pytest.raises(ValueError):
CheckpointConfig(save_checkpoint_steps=-1)
with pytest.raises(ValueError):
CheckpointConfig(save_checkpoint_seconds=-1)
with pytest.raises(ValueError):
CheckpointConfig(keep_checkpoint_max=-1)
with pytest.raises(ValueError):
CheckpointConfig(keep_checkpoint_per_n_minutes=-1)