Test error cases in Monitor wrapper
()
| 145 | |
| 146 | |
| 147 | def test_monitor_error_cases(): |
| 148 | """ |
| 149 | Test error cases in Monitor wrapper |
| 150 | """ |
| 151 | env = gym.make("CartPole-v1") |
| 152 | env.reset(seed=0) |
| 153 | |
| 154 | with pytest.raises(RuntimeError, match="Tried to reset an environment before done"): |
| 155 | monitor_env = Monitor(env, allow_early_resets=False) |
| 156 | monitor_env.reset() |
| 157 | monitor_env.step(monitor_env.action_space.sample()) |
| 158 | monitor_env.reset() |
| 159 | |
| 160 | env2 = gym.make("CartPole-v1") |
| 161 | env2.reset(seed=0) |
| 162 | with pytest.raises(ValueError, match="Expected you to pass keyword argument test_key into reset"): |
| 163 | monitor_env2 = Monitor(env2, reset_keywords=("test_key",)) |
| 164 | monitor_env2.reset() |
| 165 | |
| 166 | # Note: cannot use test_key because CartPole doesn't accept this keyword |
| 167 | monitor_env2 = Monitor(env2, reset_keywords=("options",)) |
| 168 | monitor_env2.reset(options={"ok": 1}) |
| 169 | |
| 170 | env3 = gym.make("CartPole-v1") |
| 171 | env3.reset(seed=0) |
| 172 | with pytest.raises(RuntimeError, match="Tried to step environment that needs reset"): |
| 173 | monitor_env3 = Monitor(env3) |
| 174 | monitor_env3.step(monitor_env3.action_space.sample()) |