File size: 4,079 Bytes
fe0625d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
"""
测试模块 09 —— 参数校验(ValueError)

需求覆盖
--------
* R13:非法参数应抛出 ValueError,并提供有意义的错误信息

对应用例
--------
TC-22, TC-23, TC-24, TC-25
"""

from __future__ import annotations

import pytest

from maze_env import MazeEnv


class TestValidation:
    """验证非法构造参数触发 ValueError。"""

    # ------------------------------------------------------------------ #
    # TC-22  grid_size 过小                                                #
    # ------------------------------------------------------------------ #

    @pytest.mark.unit
    def test_grid_size_too_small(self) -> None:
        """TC-22:grid_size < 4 应抛出 ValueError。

        输入:  MazeEnv(grid_size=3)
        期望:  ValueError
        实测:  pytest.raises
        """
        with pytest.raises(ValueError, match="grid_size"):
            MazeEnv(grid_size=3)

    # ------------------------------------------------------------------ #
    # TC-23  obstacle_density 超界                                         #
    # ------------------------------------------------------------------ #

    @pytest.mark.unit
    def test_obstacle_density_negative(self) -> None:
        """TC-23a:obstacle_density < 0 应抛出 ValueError。

        输入:  MazeEnv(obstacle_density=-0.1)
        期望:  ValueError
        实测:  pytest.raises
        """
        with pytest.raises(ValueError, match="obstacle_density"):
            MazeEnv(obstacle_density=-0.1)

    @pytest.mark.unit
    def test_obstacle_density_too_high(self) -> None:
        """TC-23b:obstacle_density >= 1.0 应抛出 ValueError。

        输入:  MazeEnv(obstacle_density=1.0)
        期望:  ValueError
        实测:  pytest.raises
        """
        with pytest.raises(ValueError, match="obstacle_density"):
            MazeEnv(obstacle_density=1.0)

    # ------------------------------------------------------------------ #
    # TC-24  max_steps 非正数                                              #
    # ------------------------------------------------------------------ #

    @pytest.mark.unit
    def test_max_steps_zero(self) -> None:
        """TC-24a:max_steps == 0 应抛出 ValueError。

        输入:  MazeEnv(max_steps=0)
        期望:  ValueError
        实测:  pytest.raises
        """
        with pytest.raises(ValueError, match="max_steps"):
            MazeEnv(max_steps=0)

    @pytest.mark.unit
    def test_max_steps_negative(self) -> None:
        """TC-24b:max_steps < 0 应抛出 ValueError。

        输入:  MazeEnv(max_steps=-1)
        期望:  ValueError
        实测:  pytest.raises
        """
        with pytest.raises(ValueError, match="max_steps"):
            MazeEnv(max_steps=-1)

    # ------------------------------------------------------------------ #
    # TC-25  action_space 越界                                             #
    # ------------------------------------------------------------------ #

    @pytest.mark.unit
    def test_invalid_action_raises(self, env_zero: MazeEnv) -> None:
        """TC-25:传入无效 action(如 4)应抛出异常。

        输入:  reset(),step(4)
        期望:  抛出 ValueError 或 AssertionError(实现可选)
        实测:  pytest.raises
        """
        env_zero.reset()
        with pytest.raises((ValueError, AssertionError)):
            env_zero.step(4)

    # ------------------------------------------------------------------ #
    # TC-26  render_mode 非法值                                            #
    # ------------------------------------------------------------------ #

    @pytest.mark.unit
    def test_invalid_render_mode(self) -> None:
        """TC-26:不支持的 render_mode 应抛出 ValueError。

        输入:  MazeEnv(render_mode='invalid')
        期望:  ValueError,消息含 'render_mode'
        实测:  pytest.raises
        """
        with pytest.raises(ValueError, match="render_mode"):
            MazeEnv(grid_size=6, render_mode="invalid")