Data Generation
Building on the foundation of creating and using variables, Jikken also supports generating data using the same variable structure. Generated values are treated just like normal variables, so they can be embedded and reused the same way in your test definitions. These values can be completely random or you can specify constraints to generate data based on your own validity criteria.
Generating a value
Let’s take a look at a few examples of generating data of different types.
Numbers
In the simplest case, we just want to generate a single random value, such as a number.
Let’s start with an unbounded random integer:
For this case, all we need to do is a create a variable, give it a name, and specify type: Integer
.
Since no value is specified, it will be given a random value based on the type - in this case, an integer.
What if we want a positive integer, or an integer in a specific range?
If we specify a min
or max
, the generated value will be bounded by those values.
In this case, we will get a random number between 0 and 100.
We could also set min: 0
without a max
to get a positive integer.
Decimal values can be generated in the same way using type: Float
.
Strings
Let’s look at how we might generate a string value in the same way:
Just as before, if we only specify type: String
with no other constraints, a random string value will be generated.
And as we might expect, we can also add some constraints to create a more specific random string:
By specifying minLength
and maxLength
, we can generate a string with a length of between 5 and 10 characters.
You can also use length
to specify an exact length.
What if our API accepts a pre-defined set of values, such as an enum?
We can use the anyOf
constraint to specify a set of allowed values, and the variable will contain a randomly chosen value from that set.
In addition to general strings, there are also two special string types - Name
and Email
.
When used, these will generate a random appropriate value for the given type - a full name or an email address, respectively.
Lists
Jikken also supports generating more complex types, like list and object.
Let’s look at a list first:
Here, we have specified type: List
with a length of 5.
By default, this will generate a list of 5 integers.
Just like with strings, we can also specify a minLength
and maxLength
for a variable-length list.
What if we don’t want integers, but strings instead?
For complex types, the schema
block is where we define the type and constraints for the inner or nested values.
In this case, all we’re specifying is that the items in the list should be of type: String
.
This will generate a list of 5 random string values.
Going a step further, we can also define constraints inside the schema
block, and these will apply to the items inside the list.
In this case, we’re specifying length: 10
for each string, so this will produce a list of 5 random strings, each of which is 10 characters long.
We can use any of the constraints applicable to a string here (or, of course, any other type instead).
Objects
As you might expect, objects are a bit different. There is no default value for a random object, and none of the constraints we’ve talked about directly apply.
Objects are generated entirely from the schema
block.
Each desired field is defined as a member of schema
, and these can be literal values, generated values, or a mixture of the two.
In the example above, the object generated has a “field1” with a value of “value” (a literal value), and a “field2” with a random 10-character string (a generated value).
We can also nest a complex type inside of the object. Here, we’ve changed “field2” to a list of strings, so it will now contain a list of 5 random 10-character strings instead of a single value.
What about another object?
Of course, we can nest an object inside the field of another object. Here, “field2” now contains a nested object, which has a “field3” with a random integer between 0 and 100.
Lastly, let’s revisit lists for just a moment:
We can also create a list of objects by using a variable of type: List
with a schema
of type: Object
.
This will generate a list of 5 random objects, each one with a “field1” with the literal “value” and a “field2” with a random 10-character string.
As you can imagine, these different types can be nested and combined to generate whatever specific data structure you might need, while still allowing the flexibility of using random values only where needed.
For more information on variables and how to use them, see the Variables page.
Type reference
This section contains examples with all valid fields for each supported data type, for easy reference.
For details and descriptions of each field, see the Full Format page.
Boolean
Generates a boolean value. The alias Bool
is also supported.
Example value:
Integer
Generates a 64-bit integer value. The alias Int
is also supported.
If no constraints are provided, the generated value will be between 0 and 100.
Example value:
Float
Generates a 64-bit floating point value.
If no constraints are provided, the generated value will be between 0 and 100, with up to 3 decimal places.
Example value:
String
Generates a string value.
If no constraints are provided, the generated value will be an alphabetic string between 5 and 20 characters in length.
Example value:
Name
Generates a full name, as a given name and surname separated by a single space.
Example value:
Generates an email address with an example.com
, example.net
, or example.org
domain.
Example value:
Date
Generates a date.
For details on format
syntax, see the Full Format page.
If no constraints are provided, the generated value will be between 1970-01-01 and the current date.
The default format is YYYY-MM-DD
.
Example value:
DateTime
Generates a date time (timestamp).
For details on format
syntax, see the Full Format page.
If no constraints are provided, the generated value will be between 1970-01-01 and the current date and time.
The default format is YYYY-MM-DD hh:mm:ss
.
Example value:
List
Generates a list.
If no schema
or constraints are provided, the generated value will be a list of integers with between 1 and 10 items, where each item is between 0 and 100.
Example value:
Object
Generates an object.
This type has no default value, so schema
is required.