4
$\begingroup$

I'm developing a package and keep a notebook of test cases to make sure a fix in one function doesn't break anything else. Right now, when I make a change to the package, I run the relevant parts of the test notebook and manually compare the new output to the old. Can this be automated, so that cells where the new output differs from the old are highlighted?

$\endgroup$
  • 6
    $\begingroup$ You can convert your test notebook to a .wlt file, and set up an automated test run: reference.wolfram.com/language/tutorial/… $\endgroup$ – Jason B. Feb 9 '18 at 20:23
  • $\begingroup$ I hadn't tried actual Testing Notebooks before. Something like that would be ideal, but it seem completely impractical to use. It was too laggy -- like one minute to evaluate a cell. Is that generally true? I also generated a .wlt file from a subset of the tests, and that ran quickly, but the output wasn't very user-friendly. I'm looking for something more interactive and visual. $\endgroup$ – Chris K Feb 10 '18 at 15:58
3
$\begingroup$

I've finally hacked out a solution that seems to work. The basic idea is to capture the old output with CellProlog and the new output with CellEpilog, then compare them. Unfortunately I had to resort to SelectionMove to capture multiple output cells and then move the cursor to the right place.

Run this at the top of the notebook you want to track changes in:

SetOptions[EvaluationNotebook[], {
  CellProlog :> (
    SelectionMove[EvaluationCell[], All, GeneratedCell];
    oldoutput = ToExpression@*First /@ 
      Select[NotebookRead[SelectedCells[]], #[[2]] == "Output" &]
  ),
  CellEpilog :> (
    SelectionMove[EvaluationCell[], All, GeneratedCell];
    newoutput = ToExpression@*First /@ 
      Select[NotebookRead[SelectedCells[]], #[[2]] == "Output" &];
    If[(oldoutput /. x_Graphics :> ImageData@Rasterize[x, "Image"]) =!=
       (newoutput /. x_Graphics :> ImageData@Rasterize[x, "Image"]), 
      Print[Style["Output change:", Red], "\nold=", oldoutput]];
    If[Length[newoutput] != 0, 
      SelectionMove[EvaluationCell[], After, CellGroup, 2], 
      SelectionMove[EvaluationCell[], After, Cell]]
  )
}]
$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.