JetClient / jet-client-support

JetClient is an advanced REST API Client plugin for JetBrains IDEs
https://plugins.jetbrains.com/plugin/21173-jetclient
104 stars 0 forks source link

[FEAT] parameterized variables #111

Closed faelin closed 2 weeks ago

faelin commented 2 weeks ago

Is your feature request related to a problem?

In order to test API endpoints that have multiple possible values, I have created test-suites that run through many possible combinations of variables. However, if I were to make a change to the API schema, I would need to update dozens or hundreds of requests to accommodate the new schema requirements. This is a huge pain!

Describe the solution you'd like

It would greatly reduce the number of requests that I need to define if I could create "parameterized" variables for a collection, so that the collection could be automatically run with a "matrix" of combinations automatically generated by the runner, instead of having to re-run multiple times with different variables.

Additional context

Many test-frameworks for code development have parameterized "matrix" solutions that automatically calculate combinations of parameters, running the same tests over and over with different combinations.

AntonShuvaev commented 2 weeks ago

Thanks for the suggestion! I’ve decided not to add a parameterized variables feature because it can already be done with scripts, providing more flexibility. Here's how you can do it:

  1. Define test data in your test suite variables as an array of objects with variables for each test iteration:
    {
    testData: [
    {
      testVariable: 'test1'
    },
    {
      testVariable: 'test2'
    }
    ]
    }
  2. Define a helper function that will set the properties of an object as runtime variables. You can add it as a global function in the project Init Script for reuse
    setUpVariables = (variables) => {
    Object.entries(variables).forEach(([key, value]) => jc.variables.set(key, value))
    }
  3. Use it in your test suite:
    
    const testData = jc.testSuiteVariables.get("testData")

for (let data of testData) { setUpVariables(data) // Execute your tests here }

If you prefer using CSV files, define a helper function to read them
```js
readCsvAsJson = (filePath) => {
    const fileContent = jc.readFile(filePath)
    const csvParse = require('csv-parse/lib/sync')
    return csvParse.parse(fileContent, {
        columns: true,
        skip_empty_lines: true
    })
}

Then use it like this:

const testData = readCsvAsJson("test.csv")