Forum Discussion

SmartBearDan's avatar
SmartBear Alumni (Retired)
9 years ago

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



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



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.



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


Related Forum Post:

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






9 Replies

    • baxatob's avatar
      Community Hero

      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



      • vondie's avatar

        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!

  • 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.

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

    • AlexKaras's avatar
      Champion Level 3



      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.

      • XBug's avatar

        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.