SPB Git

Repository Public MIT Research preview

test_starter.py 2.7 KB 58 lines python

1"""Le point de départ ne contient aucune solution, échoue proprement, et reste synchronisé avec le corrigé."""2from __future__ import annotations34import ast5import subprocess6import sys7import unittest89from tests import LABS1011import generer_starter121314class TestStarter(unittest.TestCase):15    def test_starter_synchronise_avec_le_corrige(self):16        for chemin, texte in generer_starter.attendu().items():17            self.assertEqual(chemin.read_text(encoding="utf-8"), texte, f"{chemin.name} périmé : python3 generer_starter.py")1819    def test_aucune_fonction_du_starter_ne_contient_de_solution(self):20        n = 021        for nom in generer_starter.FICHIERS:22            arbre = ast.parse((LABS / "starter" / nom).read_text(encoding="utf-8"))23            for noeud in arbre.body:24                if isinstance(noeud, ast.FunctionDef):25                    n += 126                    self.assertFalse(noeud.name.startswith("_"), f"{nom} : fonction privée {noeud.name} du corrigé")27                    corps = [x for x in noeud.body if not (isinstance(x, ast.Expr) and isinstance(x.value, ast.Constant))]28                    self.assertEqual(len(corps), 1, f"{nom}:{noeud.name} contient du code")29                    self.assertIsInstance(corps[0], ast.Raise, f"{nom}:{noeud.name}")30                    self.assertIsNotNone(ast.get_docstring(noeud), f"{nom}:{noeud.name} sans spécification")31        self.assertEqual(n, 14, "nombre de fonctions à écrire par les apprenants")3233    def test_le_corrige_n_est_pas_dans_le_dossier_des_apprenants(self):34        fichiers = {p.name for p in (LABS / "starter").iterdir() if p.is_file()}35        self.assertEqual(fichiers - {"README.md"}, set(generer_starter.FICHIERS))36        for p in LABS.rglob("*"):37            if p.is_file() and p.suffix in {".py", ".md"}:38                self.assertNotIn("corrige", p.name.lower(), f"{p} : un corrigé dans labs/")3940    def _verifier(self, impl: str) -> subprocess.CompletedProcess:41        return subprocess.run([sys.executable, str(LABS / "verifier.py"), "--impl", impl], capture_output=True, text=True)4243    def test_le_verificateur_echoue_proprement_sur_le_starter(self):44        r = self._verifier("starter")45        self.assertEqual(r.returncode, 1)46        self.assertNotIn("Traceback", r.stdout + r.stderr)47        self.assertEqual(r.stdout.count("À FAIRE"), 9)48        self.assertIn("0 contrôle(s) réussi(s) sur 9", r.stdout)4950    def test_le_verificateur_reussit_sur_le_corrige(self):51        r = self._verifier("solution")52        self.assertEqual(r.returncode, 0, r.stdout + r.stderr)53        self.assertIn("9 contrôle(s) réussi(s) sur 9", r.stdout)545556if __name__ == "__main__":57    unittest.main()58
Files main
Menu

Theme