注入干净引擎(seed=0, autostart=False)的 Flask test_client。 替换 api.simulation_engine 后在函数作用域内运行测试,测试结束后恢复原始引擎, 保证测试间无状态污染。适合 API 集成测试中只需 client、不需直接操作引擎的场景。
()
| 57 | |
| 58 | @pytest.fixture |
| 59 | def flask_client(): |
| 60 | """注入干净引擎(seed=0, autostart=False)的 Flask test_client。 |
| 61 | |
| 62 | 替换 api.simulation_engine 后在函数作用域内运行测试,测试结束后恢复原始引擎, |
| 63 | 保证测试间无状态污染。适合 API 集成测试中只需 client、不需直接操作引擎的场景。 |
| 64 | """ |
| 65 | import backend.api as api_module |
| 66 | |
| 67 | eng = create_engine(seed=0, autostart=False) |
| 68 | original = api_module.simulation_engine |
| 69 | api_module.simulation_engine = eng |
| 70 | api_module.app.config["TESTING"] = True |
| 71 | try: |
| 72 | with api_module.app.test_client() as c: |
| 73 | yield c |
| 74 | finally: |
| 75 | eng.running = False |
| 76 | api_module.simulation_engine = original |
| 77 | |
| 78 | |
| 79 | @pytest.fixture |
nothing calls this directly
no test coverage detected