Notebook-Tests: nbmake + Subprocess-Zellen + inkrementeller Test

Alle 25 Notebooks sind nun fehlerfrei testbar (25 passed). Dafür drei
Änderungen im Build und eine neue Test-Infrastruktur:

1. pyproject.toml: nbmake>=1.5 in der dev-Gruppe.

2. build_version_04.py: Notebooks bekommen zwei Setup-Zellen und
   Subprocess-Zellen für Programme, die nicht direkt im Kernel laufen:
   - NOTEBOOK_SETUP_2: __file__ definieren, sys.path für or_kern,
     multiprocessing auf fork (Spawn findet Kernel-Funktionen nicht).
   - _braucht_subprocess: erkennt highspy (Solver-Konflikt), pytest
     (SystemExit), get_context('spawn'), subprocess+__file__ (sucht
     .py-Dateien), sys.exit (SystemExit). Diese Programme erscheinen
     als Markdown-Codeblock (sichtbar, nicht ausführbar) plus einer
     Subprocess-Zelle mit NO_COLOR=1 (verhindert ANSI-Codes, die der
     Parser von Mutationstest.py nicht verarbeitet).
   - Solver-Konflikt-Erkennung: wenn ein Kapitel sowohl ortools als
     auch highspy importiert, laufen auch reine ortools-Programme als
     Subprocess (highspy schon im Kernel).

3. teste_code_04.py: inkrementeller Test — nur geänderte/neue Programme
   und Notebooks (via git status). Netzabhängige Dateien (yfinance)
   werden automatisch erkannt und nur getestet, wenn Internet verfügbar.

4. PLAN.md Abschnitt 10: neuer Verifikationsschritt 4 (pytest --nbmake).
   PROGRESS.md und CLAUDE.md aktualisiert.

Gefundene und behobene Probleme:
- or_kern-Import: sys.path im Notebook erweitert.
- __file__ nicht definiert: zweite Setup-Zelle setzt es.
- multiprocessing spawn: fork erzwungen (Ein_System_Vier_Ansaetze.py).
- ortools/highspy-Konflikt: Subprocess für beide Solver.
- pytest SystemExit: Subprocess für test_or_kern.py und Mutationstest.py.
- sys.exit(0): Subprocess für Installationstest.py.
- ANSI-Codes in pytest-Ausgabe: NO_COLOR=1 in Subprocess-Zelle.
This commit is contained in:
dschlueter 2026-09-11 22:38:59 +02:00
commit a4fc247116
70 changed files with 4073 additions and 305 deletions

View file

@ -12,7 +12,11 @@
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"metadata": {
"tags": [
"nbmake-skip"
]
},
"outputs": [],
"source": [
"# Einmalig ausfuehren: installiert alle im Buch verwendeten Pakete.\n",
@ -21,6 +25,27 @@
" scikit-learn matplotlib plotly pyomo linopy pymoo pydantic openpyxl"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# Notebook-spezifisches Setup: __file__ definieren (Programme nutzen es\n",
"# fuer OUTPUT_DIR), Programmverzeichnis in den Suchpfad aufnehmen (or_kern)\n",
"# und multiprocessing auf 'fork' stellen (Spawn findet Kernel-Funktionen nicht).\n",
"import os, sys\n",
"__file__ = os.path.join(os.getcwd(), '_notebook.py')\n",
"for _p in [os.path.join(os.path.dirname(os.getcwd()),\n",
" 'Operations_Research_mit_Python_Version_04_Programme'),\n",
" os.path.dirname(os.getcwd()), os.getcwd()]:\n",
" if os.path.isfile(os.path.join(_p, 'or_kern.py')):\n",
" sys.path.insert(0, _p); break\n",
"import multiprocessing as _mp\n",
"try: _mp.set_start_method('fork', force=True)\n",
"except (RuntimeError, ValueError): pass"
]
},
{
"cell_type": "markdown",
"metadata": {},
@ -31,11 +56,14 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"## Die Testsuite\n",
"\n",
"`test_or_kern.py`\n",
"\n",
"```python\n",
"#!/usr/bin/env python3\n",
"\n",
"# test_or_kern.py\n",
@ -344,7 +372,36 @@
"\n",
"if __name__ == \"__main__\":\n",
" import sys\n",
" sys.exit(pytest.main([__file__, \"-v\", \"--tb=short\", \"-p\", \"no:cacheprovider\"]))"
" sys.exit(pytest.main([__file__, \"-v\", \"--tb=short\", \"-p\", \"no:cacheprovider\"]))\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# In einem Notebook kann 'test_or_kern.py' nicht direkt ausgefuehrt werden\n",
"# (ortools/highspy-Konflikt im selben Kernel, pytest oder multiprocessing).\n",
"# Deshalb als Subprocess — so laeuft es genauso wie im Terminal.\n",
"import subprocess, sys, os\n",
"# NO_COLOR/TERM=dumb verhindern ANSI-Codes in der Ausgabe (pytest\n",
"# im Jupyter-Kernel wuerde sonst farbige Escape-Sequenzen ausgeben).\n",
"_env = dict(os.environ, NO_COLOR='1', TERM='dumb')\n",
"for _d in [os.path.join(os.path.dirname(os.getcwd()),\n",
" 'Operations_Research_mit_Python_Version_04_Programme'),\n",
" os.path.dirname(os.getcwd()), os.getcwd()]:\n",
" _p = os.path.join(_d, 'test_or_kern.py')\n",
" if os.path.isfile(_p):\n",
" _r = subprocess.run([sys.executable, _p], cwd=_d, env=_env,\n",
" capture_output=True, text=True)\n",
" if _r.stdout: print(_r.stdout)\n",
" if _r.stderr: print(_r.stderr)\n",
" _r.check_returncode()\n",
" break\n",
"else:\n",
" print('test_or_kern.py nicht gefunden')"
]
},
{
@ -357,11 +414,14 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"## Wer testet die Tests?\n",
"\n",
"`Mutationstest.py`\n",
"\n",
"```python\n",
"#!/usr/bin/env python3\n",
"\n",
"# Mutationstest.py\n",
@ -540,7 +600,36 @@
" print(\"Die Quote selbst ist keine Kennzahl fuer ein Dashboard. Zehn von Hand\")\n",
" print(\"gewaehlte Mutationen sind keine Stichprobe aus der Menge aller\")\n",
" print(\"moeglichen Fehler. Was zaehlt, ist die LISTE der Ueberlebenden.\")\n",
" print(\"=\" * 84)"
" print(\"=\" * 84)\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# In einem Notebook kann 'Mutationstest.py' nicht direkt ausgefuehrt werden\n",
"# (ortools/highspy-Konflikt im selben Kernel, pytest oder multiprocessing).\n",
"# Deshalb als Subprocess — so laeuft es genauso wie im Terminal.\n",
"import subprocess, sys, os\n",
"# NO_COLOR/TERM=dumb verhindern ANSI-Codes in der Ausgabe (pytest\n",
"# im Jupyter-Kernel wuerde sonst farbige Escape-Sequenzen ausgeben).\n",
"_env = dict(os.environ, NO_COLOR='1', TERM='dumb')\n",
"for _d in [os.path.join(os.path.dirname(os.getcwd()),\n",
" 'Operations_Research_mit_Python_Version_04_Programme'),\n",
" os.path.dirname(os.getcwd()), os.getcwd()]:\n",
" _p = os.path.join(_d, 'Mutationstest.py')\n",
" if os.path.isfile(_p):\n",
" _r = subprocess.run([sys.executable, _p], cwd=_d, env=_env,\n",
" capture_output=True, text=True)\n",
" if _r.stdout: print(_r.stdout)\n",
" if _r.stderr: print(_r.stderr)\n",
" _r.check_returncode()\n",
" break\n",
"else:\n",
" print('Mutationstest.py nicht gefunden')"
]
},
{
@ -553,11 +642,14 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"## Ein Vergleich, dem man glauben kann\n",
"\n",
"`Benchmark_Skalierung.py`\n",
"\n",
"```python\n",
"#!/usr/bin/env python3\n",
"\n",
"# Benchmark_Skalierung.py\n",
@ -770,7 +862,36 @@
" print(\" * Etwas ueber Ihre Maschine. Diese Zahlen stammen von einer\")\n",
" print(\" anderen. Der Sinn des Programms ist, dass Sie es auf Ihrer\")\n",
" print(\" laufen lassen.\")\n",
" print(\"=\" * 92)"
" print(\"=\" * 92)\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# In einem Notebook kann 'Benchmark_Skalierung.py' nicht direkt ausgefuehrt werden\n",
"# (ortools/highspy-Konflikt im selben Kernel, pytest oder multiprocessing).\n",
"# Deshalb als Subprocess — so laeuft es genauso wie im Terminal.\n",
"import subprocess, sys, os\n",
"# NO_COLOR/TERM=dumb verhindern ANSI-Codes in der Ausgabe (pytest\n",
"# im Jupyter-Kernel wuerde sonst farbige Escape-Sequenzen ausgeben).\n",
"_env = dict(os.environ, NO_COLOR='1', TERM='dumb')\n",
"for _d in [os.path.join(os.path.dirname(os.getcwd()),\n",
" 'Operations_Research_mit_Python_Version_04_Programme'),\n",
" os.path.dirname(os.getcwd()), os.getcwd()]:\n",
" _p = os.path.join(_d, 'Benchmark_Skalierung.py')\n",
" if os.path.isfile(_p):\n",
" _r = subprocess.run([sys.executable, _p], cwd=_d, env=_env,\n",
" capture_output=True, text=True)\n",
" if _r.stdout: print(_r.stdout)\n",
" if _r.stderr: print(_r.stderr)\n",
" _r.check_returncode()\n",
" break\n",
"else:\n",
" print('Benchmark_Skalierung.py nicht gefunden')"
]
},
{
@ -783,11 +904,14 @@
]
},
{
"cell_type": "code",
"execution_count": null,
"cell_type": "markdown",
"metadata": {},
"outputs": [],
"source": [
"## Das Modell als Dienst\n",
"\n",
"`Optimierungsdienst.py`\n",
"\n",
"```python\n",
"#!/usr/bin/env python3\n",
"\n",
"# Optimierungsdienst.py\n",
@ -1102,7 +1226,36 @@
" print()\n",
" print(\"(Es wird hier nicht gebaut - das Buch setzt keine laufende\")\n",
" print(\" Docker-Installation voraus.)\")\n",
" print(\"=\" * 80)"
" print(\"=\" * 80)\n",
"```\n"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# In einem Notebook kann 'Optimierungsdienst.py' nicht direkt ausgefuehrt werden\n",
"# (ortools/highspy-Konflikt im selben Kernel, pytest oder multiprocessing).\n",
"# Deshalb als Subprocess — so laeuft es genauso wie im Terminal.\n",
"import subprocess, sys, os\n",
"# NO_COLOR/TERM=dumb verhindern ANSI-Codes in der Ausgabe (pytest\n",
"# im Jupyter-Kernel wuerde sonst farbige Escape-Sequenzen ausgeben).\n",
"_env = dict(os.environ, NO_COLOR='1', TERM='dumb')\n",
"for _d in [os.path.join(os.path.dirname(os.getcwd()),\n",
" 'Operations_Research_mit_Python_Version_04_Programme'),\n",
" os.path.dirname(os.getcwd()), os.getcwd()]:\n",
" _p = os.path.join(_d, 'Optimierungsdienst.py')\n",
" if os.path.isfile(_p):\n",
" _r = subprocess.run([sys.executable, _p], cwd=_d, env=_env,\n",
" capture_output=True, text=True)\n",
" if _r.stdout: print(_r.stdout)\n",
" if _r.stderr: print(_r.stderr)\n",
" _r.check_returncode()\n",
" break\n",
"else:\n",
" print('Optimierungsdienst.py nicht gefunden')"
]
}
],