We present you some tricks that you can use to solve common problems while writing tests.
If you want to generate a unique random email you can incorporate the current epoch in it. Epochs progress every millisecond so it’s pretty improbable to have a duplicate! The D language extension (see here for more details about this extension) provides date manipulation utilities, so:
if ‘string’ is ‘apifortress’ the result will be something like: email@example.com
If a data item is a composite string of values and you need to evaluate portions of it, there are numerous ways you can do it.
Let’s say we have this string “55,5,84,65,26,7,82” and you want to work with each number. The simpler way is transforming the string into an array. You can do using the ‘split’ function:
In the example above, if the string is included in a variable named ‘randomNumbers’ writing randomNumbers.split(‘,’) you produce an array like [55,5,84,65,26,7,82]. At this point you can work with it as you generally do when you have an array.
Another trick you can use to improve your tests is extracting something from a string using the ‘substring’ functionality. If you need to extract the first n characters from a string there is a function that helps you: take(n).
Let’s consider the following string: “whatever” saved in a variable named “substring_variable”, if you:
“what” will be returned.
Another function for a string is drop(n) that removes the first n characters and return the resulting String. Considering the same string and variable used as example for take(n), if you:
“ever” will return.
The last example is extracting a substring using index ranges. If we have the string “wherever” by doing:
we will obtain “here”
Maybe your endpoint requires a date in a specific format as parameter.
This expression will generate today date (e.g. 03/09/17)