I would just like to share a super simple approach to passing variables between test cases in a suite, as I’ve seen many topics over the years with questions on how to do this efficiently. To be clear, there are already several approaches to this, including, but not limited to:
1.) Creating “empty” GlobalVariables in a Profile, assigning values to those variables at runtime, then consuming them as a global variable in later scripts.
- This approach works fine, but in my opinion is non-ideal, as it contaminates the GlobalVariable namespace with a bunch (potentially 1000’s) of empty variables of global scope.
2.) Using callTestCase() with parameters.
- This also works fine, but is cumbersome both syntactically and in terms of test case organization, “Variables” tab usage, etc. (again, in my opinion).
3.) Writing the variables to data files.
- This can also be quite cumbersome, and of course any data will persist even after a test case/test suite ends, which has the potential to cause a lot of problems.
Instead, we can use static collections to do this.
Create a utility class which just instantiates a few different types of collections, with a static reference:
What this means is that any values put into these collections will be available for the lifetime of any Test Case and/or any Test Suite. For example, if I have two test cases, and put both into a suite, I can pass a variable generated in TC1 into the map, and retrieve that value when the suite runs TC2:
To demonstrate that the lifetime of this variable map is that of the test suite (and no longer), if I then just run TC2 alone, you’ll see that the value is no longer in there:
Disclaimer: I’m aware that this is nothing groundbreaking. All it takes is understanding the nature of static references. I’m just sharing this because I’ve yet to see this solution posted anywhere. Also, I generally write scripts in such a way that there are zero external dependencies (they are “hermetically sealed”), but have some colleagues that had this requirement, so yeah…
Hopefully this is helpful