cancel
Showing results for 
Search instead for 
Did you mean: 

Picking a random row from a dataset as part of a test

Picking a random row from a dataset as part of a test

TestComplete has the ability to drive tests using a data source (Excel, CSV, datbase, etc), and there is a Data Loop option built in if you need to repeat the same steps for all the rows in a dataset.

  

Data Driving a Test Case in TestComplete

 

However, there may be times when you don't need to loop through all the data in a dataset and just want to pick one at random.  This is a guide showing an example using JScript.  Similar functionality is available for other scripting languages as well, with slightly different syntax.

 

 

1.) Define your data

 

You can use a pre-defined data source for this, or TestComplete's built-in data generation wizard.  In this case I'm using the data generation wizard to create 10,000 random names for me to pick from.  This creates a Table variable, and that can be stored in a single Keyword test or with an entire Project.

 

Data Generator Wizard

 

Data Generation Wizard2.png

 

2.) Create a second variable as an integer to store the random number

 

RandomNumberVariable.png

 

3.) In the test, use the Set Variable Value to assign that integer a random number between 1 and the total number of rows in that table.  In my case 10,000.  Use the Code Expression option for the mode.

 

This example works in JScript, if your Project is in another scripting language the syntax here would need to be adjusted.

 

SetVariableValue.png

 

4.) Use that integer variable to select a row of your table and return the value, that can be used in the test steps.

 

useintest.png

Related Forum Post:

Generating random string values (A - D) & select from dropdown

 

 

 

 

 

9 REPLIES 9
Community Manager

Re: Picking a random row from a dataset as part of a test

Thanks for sharing such great information, Dan!

---------
Tanya Gorbunova
SmartBear Community Manager

Did my reply answer your question? Give Kudos or Accept it as a Solution to help others.↓↓↓↓↓
Community Hero

Re: Picking a random row from a dataset as part of a test

Great opportunity!

 

For python you can use this expression: 

import random
var = random.choice([i for i in range(x)]) # Will return the random mumber between 0 and x

 

 

Contributor

Re: Picking a random row from a dataset as part of a test

@baxatob,I tried to implement your suggested Python code into a Keyword Test and I can't get it to work. Is it possible to import a library into a Keyword Test? I tried using the Run Code Snippet tool to add "import random" at the top of the script but I am getting exception errors. See screenshots for examples, if desired. I can create a script but my team has decided that the majority of our tests should be Keyword Tests, not Scripts, so they are easily debugged by everyone on the team. Thanks!

Community Hero

Re: Picking a random row from a dataset as part of a test

Hi Vondie,

You can create the script and then call it from your keyword test using Run Script Routine item.
Super Contributor

Re: Picking a random row from a dataset as part of a test

@SmartBearDan Great solution! This is exactly what I am looking for.

 

Note: Could be even better if this feature (select random row) be implemented in TC's Data Driven functionality, so that testers don't need to use scripting!

 

 

See screenshot to illustrate implementation:

 

 

 

2016-11-02_12-29-04.png

 I created a feature request. It can be found here. Please vote for it if you agree.

 

 

New Contributor

Re: Picking a random row from a dataset as part of a test

Is this method still valid with Version 12.20.935.7.  Or is there a better method to use.  Not able to get this to work.

New Contributor

Re: Picking a random row from a dataset as part of a test

Does this method still work with Version 12.31.  I am not having any luck getting it to work.

Community Hero

Re: Picking a random row from a dataset as part of a test

Hi,

 

Yes, the approach must work.

The more details you provide as for what does not work in your case, the more chances to get a piece of useful advice.

Regards,
Alex
[Community Expert Group]
____
[Community Expert Group] members are not employed by SmartBear Software but
are just volunteers who have some experience with the tools by SmartBear Software
and a desire to help others. Postings made by [Community Expert Group] members
may differ from the official policies of SmartBear Software and should be treated
as the own private opinion of their authors and under no circumstances as an
official answer from SmartBear Software.
[Community Expert Group] signature is used with permission by SmartBear Software.
http://smartbear.com/forums/f83/t86934/community-experts/
================================
Contributor

Re: Picking a random row from a dataset as part of a test

I tried implementing this method but was not successful. Got an error saying that "The table does not contain a column with the specified name". Is there anything needed to change for this to be applicable to v12.50.4142 x86. Or is there an alternative approach for this now?

 

What I would want done is to generate a 5-digit number with random value then use it in my tests. I prefer that a new number will be generated everytime the step is executed. Also, I prefer to not use scripts, unless there's no other way to do this.

 

I hope anyone can help me out.

New Here?
Join us and watch the welcome video:
Watch the new Interview
Top Kudoed Authors