"""viz 包测试。 绘图测试不比对像素——只验证"能画出来、能存成文件、尺寸符合美赛要求"。 """ from __future__ import annotations import matplotlib matplotlib.use("Agg") import pytest pytestmark = pytest.mark.skip(reason="未实现:见 docs/03-roadmap.md L0/L1") @pytest.mark.parametrize("kind", ["line", "scatter", "hist", "box", "bar"]) def test_plot_kinds_produce_figure(kind, rng): from mcm_toolkit.viz import plot x = rng.normal(size=100) fig, ax = plot(x, x, kind=kind) assert fig is not None and ax is not None def test_mcm_style_sets_300dpi(): from mcm_toolkit.viz import mcm_style mcm_style() assert matplotlib.rcParams["figure.dpi"] == 300.0 assert matplotlib.rcParams["font.size"] >= 8.0 def test_save_figure_writes_png_and_pdf(tmp_path, rng): from mcm_toolkit.viz import plot, save_figure fig, _ = plot(rng.normal(size=50), rng.normal(size=50), kind="scatter") paths = save_figure(fig, tmp_path / "fig1", formats=("png", "pdf")) assert {p.suffix for p in paths} == {".png", ".pdf"} assert all(p.exists() and p.stat().st_size > 0 for p in paths) def test_save_table_writes_latex_with_caption(tmp_path): import pandas as pd from mcm_toolkit.viz import save_table df = pd.DataFrame({"rmse": [0.31, 0.28]}, index=["linear", "rf"]) paths = save_table(df, tmp_path / "tab1", caption="模型对比", label="tab:compare") tex = next(p for p in paths if p.suffix == ".tex") content = tex.read_text(encoding="utf-8") assert "模型对比" in content and "tab:compare" in content