Better Software Testing with Conditioned Test Data
One of the most important aspects of software testing is being able to condition the test data you require to fully run a given test.
Conditioned test data gives you the ability to test for a specific outcome within a given unit, integration, load test or functionally test a specific workflow or control the test data presented in a demo application.
The MultiWeightGen Generator is one of GenRocket’s best Generators for controlling the frequency (on a percentage basis) of how one or more values are generated within a finite set of generated test data.
Let’s take the following example to see the MultiWeightGen Generator in action.
The Test Data Challenge
A tester needs to run an integration test to produce a very specific set of test data to assert that the business logic within a given service method is working correctly. The service method takes a code name, a set of data, and returns the percentage of time the specific code name was found within the set of data.
In order to thoroughly test the business logic, the tester needs to generate a specific percentage of different code names for a finite set of data:
40% of the time, generate either code Blue or code Green
Within the 40%, 80% should generate code Blue
Within the 40%, 20% should generate code Green
60% of the time, generate either code Red or code Yellow
Within the 60%, 70% should generate code Red
Within the 60%, 30% should generate code Yellow
Believe it or not, with the GenRocket MultiWeightGen Generator, this test data can be generated with only three Attributes and you can be generating the data you require in only a few minutes.
Attribute codeBlueGreen
First, create an Attribute called, codeBlueGreen, and assigned it the MultiWeightGen Generator and set Blue at 80% and Green at 20%. I also set the Attributes visible parameter to false.
Attribute codeRedYellow
Second, create an Attribute called, codeRedYellow, and assigned it the MultiWeightGen Generator and set Red at 70% and Yellow at 30%. I also set the Attributes visible parameter to false.
Attribute code
Third, create an Attribute called, code, and assigned it the MultiWeightGen Generator. Then I reference Attribute codeBlueGreen at 40% and reference Attribute codeRedYellow at 60%.
Attribute category
So that we can query our data quickly to produce the statistics we want to review, I also added an attribute called, category, assign to it the GenRocket SwitchGen Generator and gave it the parameters as you see in the image below.
Preview The Results
Click on the Domain’s box icon and you can preview your results. And since Attributes codeBlueGreen and codeRedYellow have their visible parameter set to false, only Attribute’s id, code and category will show visible results, which is what we want.
Testing your results
Here’s one cool way to see the how well the percentages turn out by populating a MySQL database table with test data and checking your results.
Generate 5000 rows to an SQL file called, stats.sql.
Create Demo Database
Login into MySQL and run the following command to create a demo database
create database demo;
Create The Stats Table
DROP TABLE IF EXISTS stats;
CREATE TABLE stats (
id bigint NOT NULL AUTO_INCREMENT,
code VARCHAR(25) NOT NULL,
category VARCHAR(25) NOT NULL,
PRIMARY KEY (id)
) ENGINE=InnoDB;
Load The Conditioned Test Data
At the command line, load the data into the table:
mysql -u username -p password -D databaseName < stats.sql
Run the following SQL statements
Percentage Each of Blue & Green
select code, count(code) as count, count(code)/(
select count(code) from stats where code in (‘Blue’,’Green’)
) * 100 as percent
from stats where code in (‘Blue’,’Green’) group by code;
Example Output:
code count percent
—- —- ——-
Blue 1609 80.8949
Green 380 19.1051
Percentage Each of Red & Yellow
select code, count(code) as count, count(code)/(
select count(code) from stats where code in (‘Red’,’Yellow’)
) * 100 as percent
from stats where code in (‘Red’,’Yellow’) group by code;
Example Output
code count percent
—– —– ——-
Red 2083 69.1797
Yellow 928 30.8203
Percentage Grouped By Category
select Category, count(code)/( select count(*) from stats)
* 100 as ‘Percentage’ from stats group by Category
Example Output
Category Percentage
——— ———-
BlueGreen 39.7800
RedYellow 60.2200
Why are the percentages not exact?
If you’re wondering why the percentages did not come out as exact percentages but close approximation instead, it’s because GenRocket Generators generate their data values on each iteration; a given iteration does not know about the sum of the whole. This way the percentages are realistic and any number of rows generated can be undetermined; in essence the number or rows generated could be 100 rows or 100,000,000 rows and the percentages would still come to a very close approximation of the exact percentages.
Try producing 50,000 rows and see what results you get.
Why are there values set for each seed parameter?
If you look at the parameters set for codeBlueGreen, codeRedYellow & code, you may notice that the seed parameter has different values. Seeding the MultiWeightGen Generator gives you greater control of your test data because it guarantees the same random data will be generated each time you generate your test data. This is especially beneficial if you desire a test to always yield the exact same result each time the test is run.
Checkout the MultiWeightGen In Action
To see the MultiWeightGen Generator IN ACTION; check out the video below.
Conclusion
Having the ability to condition the test data you require is one of the most important aspects of any type of testing.
Conditioned test data gives you the power and ability to run any unit, integration, load or functional test you need to fully and accurately test your code.
In this post, I showed you how the GenRocket MultiWeightGen Generator is one of the best Generators for controlling the frequency (on a percentage basis) of how one or more values are generated within a finite set of generated test data.
By Hycel Taylor
Founder & Chief Architect
GenRocket, Inc.