--- title: Data-Driven Testing tags: English --- # Data-Driven Testing In SideeX, 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 SideeX, and let SideeX 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 SideeX 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: ```json= [ { "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: ```csv= key1,key2 value1,value5 value2,value6 value3,value7 value4,value8 ``` ## Step 2: Import you data file into SideeX 1. Select the "Data Driven" tab, and press the "Import from file" button:![](https://i.imgur.com/V9hWywG.png) 2. After selecting the imported data file, if the filename shows up u![](https://i.imgur.com/dyRNwTP.png) :::warning ⚠ **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: ![](https://i.imgur.com/G2v2ppW.png) 3. Use `${YOUR_KEY}` to reference your data. For example:![](https://i.imgur.com/A1euULC.png) 4. Lastly, add an `END` command at the end of the process:![](https://i.imgur.com/8EsIH2a.png) ## 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](https://hackmd.io/@sideex/book-zh/%2F%40sideex%2Fvariable-zh#%E5%85%A8%E5%9F%9F%E8%AE%8A%E6%95%B8%EF%BC%9A%E4%BD%BF%E7%94%A8Global-Var%E9%9D%A2%E6%9D%BF%E6%88%96storeGlobalVar%E6%8C%87%E4%BB%A4%E5%AE%A3%E5%91%8A), 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: ![](https://i.imgur.com/SxZxD5r.png) - From the execution logs, we can see that each record of data has been executed once: ![](https://i.imgur.com/DOyfSY5.png) ## 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: ```csv= names apple peach lemon mango ``` - CSV Example Dataset 2: ```csv= prices 50 60 70 80 ``` 2. Add the commands in SideeX: ```= /* psuedocode */ for (each name in names){ for (each price in prices){ echo name echo price } } ``` ![](https://i.imgur.com/P2FLJBP.png) 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 ```