Does the Rascal unit testing framework has support for a test fixture?

StackOverflow https://stackoverflow.com/questions/20107393

  •  03-08-2022
  •  | 
  •  

문제

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?

도움이 되었습니까?

해결책

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.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top