将干净引擎注入 api.app,返回 Flask test_client。 通过替换 api.simulation_engine 避免全局状态污染;测试结束后恢复原始引擎。
(test_engine)
| 45 | |
| 46 | @pytest.fixture |
| 47 | def client(test_engine): |
| 48 | """将干净引擎注入 api.app,返回 Flask test_client。 |
| 49 | |
| 50 | 通过替换 api.simulation_engine 避免全局状态污染;测试结束后恢复原始引擎。 |
| 51 | """ |
| 52 | import backend.api as api_module |
| 53 | |
| 54 | original_engine = api_module.simulation_engine |
| 55 | api_module.simulation_engine = test_engine |
| 56 | try: |
| 57 | api_module.app.config["TESTING"] = True |
| 58 | with api_module.app.test_client() as c: |
| 59 | yield c |
| 60 | finally: |
| 61 | api_module.simulation_engine = original_engine |
| 62 | |
| 63 | |
| 64 | # --------------------------------------------------------------------------- |
nothing calls this directly
no outgoing calls
no test coverage detected