Try   HackMD

Data-Driven Testing

In Rapi, users can prepare a dataset to run in a test case. For example, if you want to test a login website, you can import several login credentials into Rapi, and let Rapi test the login website with different sets of login data. This article is going to introduce how to perform a data-driven testing, to import data into Rapi and start testing!

Step 1: Prepare a data file

  1. Your data file must be in json or csv format. Please convert your file if it's not in these two formats.
  2. Example:
    • This is a json file that has two keys, four values. A total of 8 records of data:
      ​​​​​​​​[ ​​​​​​​​ { ​​​​​​​​ "key1": "value1", ​​​​​​​​ "key2": "value5" ​​​​​​​​ }, ​​​​​​​​ { ​​​​​​​​ "key1": "value2", ​​​​​​​​ "key2": "value6" ​​​​​​​​ }, ​​​​​​​​ { ​​​​​​​​ "key1": "value3", ​​​​​​​​ "key2": "value7" ​​​​​​​​ }, ​​​​​​​​ { ​​​​​​​​ "key1": "value4", ​​​​​​​​ "key2": "value8" ​​​​​​​​ } ​​​​​​​​]
    • This is a csv file with 2 keys, 4 values. A total of 8 records of data:
      ​​​​​​​​key1,key2 ​​​​​​​​value1,value5 ​​​​​​​​value2,value6 ​​​​​​​​value3,value7 ​​​​​​​​value4,value8

Step 2: Import you data file into Rapi

  1. Select the "Data Driven" tab, and press the "Import from file" button:

  2. After selecting the imported data file, if the filename shows up u

    Warning

    If you're uploading a file that has the same name as an existing file, the upload process will fail. Please remove the existing file before uploading a new one.

Step 3: Add the data-driven command

  1. To use data-driven testing, use a pair of FOR_EACH_RECORD and END commands.
  2. Right-click on the workarea and select "Add cmd", type in FOR_EACH_RECORD as the command name,and type the filename of your dataset as the "Target" of the command. For example:
  3. Use ${YOUR_KEY} to reference your data. For example:
  4. Lastly, add an END command at the end of the process:

Step 4: Play the test case

  1. During playback, the FOR_EACH_RECORD command will turn all the data from your file into global variables, and execute every command within FOR_EACH_RECORD and END with each set of data provided.

    • Your imported data can be seen in the "Global Var" tab:

    • From the execution logs, we can see that each record of data has been executed once:

Extended Case: One-to-Many Data-Driven Testing

If you want to perform one-to-many test, you can prepare 2 datasets and use nested FOR_EACH_RECORD command to implement. Below shows an example:

  1. Prepare your datasets:
    • CSV Example Dataset 1:
      ​​​​​​​​names ​​​​​​​​apple ​​​​​​​​peach ​​​​​​​​lemon ​​​​​​​​mango
    • CSV Example Dataset 2:
      ​​​​​​​​prices ​​​​​​​​50 ​​​​​​​​60 ​​​​​​​​70 ​​​​​​​​80
  2. Add the commands in Rapi:
    ​​​​/* psuedocode */ ​​​​for (each name in names){ ​​​​ for (each price in prices){ ​​​​ echo name ​​​​ echo price ​​​​ } ​​​​}
  3. The above commands will result in the following combinations:
    ​​​​apple -> 50 ​​​​ -> 60 ​​​​ -> 70 ​​​​ -> 80 ​​​​ ​​​​peach -> 50 ​​​​ -> 60 ​​​​ -> 70 ​​​​ -> 80 ​​​​ ​​​​lemon -> 50 ​​​​ -> 60 ​​​​ -> 70 ​​​​ -> 80 ​​ ​​​​mongo -> 50 ​​​​ -> 60 ​​​​ -> 70 ​​​​ -> 80