Question

In Rascal you can define test functions by specifying the test modifier on a boolean function. And running them by typing :test at the Rascal command line. See details over here.

Is it somehow possible to also specify test fixtures (for a test suite)? Like for example setup() and teardown() functions?

Was it helpful?

Solution

You can write a setup function by including a global variable in the module you write the tests in:

bool s = setup(); // is called 

int myGlobal1 = 2;

bool setup() {
  myGlobal1 = 1;
}

teardown isn't so much necessary since we do not have that much state in Rascal other than these global variables.

Another, perhaps better, way of sharing state between tests is the use of @memo functions:

@memo
value getMyExampleData(int input) = ...;

test bool myTest1() {
  myData = getMyExampleData(1);

  return /shouldContainThis x := myData;
}

test bool myTest2() {
  myData = getMyExampleData(1);

  return /shouldContainThisToo y := myData;
}

The second call to getMyExampleData will be really fast since the result is taken from a cache. If the JVM runs out of memory, the cache is cleared automatically.

BTW, there is also some support for random testing, as in:

test bool myTest(int i, int j) = i + j == j + i;

Then the test runner will generate a lot of random input for the parameters of the test function. You can influence the input generation as well via some tags. Let us know if you are interested.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top