Looking for something like JUnit @ Before / @ After in MUnit

I am writing several test cases in a single .mt file, which has common procedures (and parsing). I was hoping to see something like JUnit @Before/ @After, but nothing from the MUnit package jumped at me to do this. The documentation for the MUnit package appears to be slightly better than the Wolfram Workbench MUnit documentation, but it is also far from complete. Instructions for viewing the MUnit package.

So, before I reinvent the wheel again, I decided to check if I missed something in MUnit, or if someone has a template that works well?

So, based on the initial answer of the boss Leonid Shifrin (he updated his answer while I put this together), this (was) where I (was) ...

TestPlus.mt

(* Mathematica Test File *)

(* x is a global value *)
x=0;

SetUp[] := Module[{}, x=1;];
TearDown[] := Module[{}, x=0;];

(* test SetUp[] *)
SetUp[];       
Test[x, 1, TestID->"SetUp-20120103-F2U9V6"]

(* test TearDown[] *)
TearDown[];
Test[x, 0, TestID->"TearDown-20120103-O4R6M7"]

(* test plus --contrived tests-- *)
SetUp[];
Test[x+0, 1, TestID->"Plus-20120103-S5D9X6"]
TearDown[];

SetUp[];
Test[x+1, 2, TestID->"Plus-20120103-D7Q3E0"]
TearDown[];

SetUp[];
Test[x+2, 3, TestID->"Plus-20120103-F0S4P9"]
TearDown[];
+5
2

MUnit , - , , . , .

, () . , , (, Import["Tests.tm","HeldExpressions"] - Tests.tm ) , . , , .

, . :

(* Mathematica Test File *)

before[]:= (Print["Before Test: ",f[2]];f[x_]:=x^3);
after[] := (ClearAll[f];Print["After Test: ",f[2]]);

SetAttributes[withCodeAfter,HoldRest];
withCodeAfter[before_,after_]:=(after;before)

SetAttributes[{wrapTest,wrapTest1},HoldAll]
wrapTest[code_]:= withCodeAfter[before[];code,after[]]  
wrapTest1[code_]:=Block[{f},f[x_]:=x^3;code];

wrapTest@
Test[f[2],
  8,
  TestID -> "MyTest1"
]

wrapTest1@
Test[f[2],
  8,
  TestID -> "MyTest2"
]

before after /. withCodeAfter , before, after, before. wrapTest wrapTest1 : "" "", , Block "", . . . - wrapTest@ .

+3

, ...

, , , , , .


, "". , f before, undefining f after.

(* Mathematica Test File *)

before[]:= (Print["Before Test: ",f[2]];f[x_]:=x^3);
after[] := (ClearAll[f];Print["After Test: ",f[2]]);

withCodeAfter. , , before, . after unevaluated , withCodeAfter. , before, withCodeAfter.

SetAttributes[withCodeAfter,HoldRest];
withCodeAfter[before_,after_]:=(after;before)

wrapTest. , code, . code before[];code, , before, withCodeAfter. before[];code withCodeAfter. code withCodeAfter after. , withCodeAfter, wrapTest.

SetAttributes[{wrapTest},HoldAll]
wrapTest[code_]:= withCodeAfter[before[];code,after[]]

, wrapTest[Test[f[2], 8, TestID -> "MyTest1"]]

wrapTest@
Test[f[2], 8, TestID -> "MyTest1"]
0

All Articles