Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FEAT] parameterized variables #111

Closed
faelin opened this issue Oct 9, 2024 · 1 comment
Closed

[FEAT] parameterized variables #111

faelin opened this issue Oct 9, 2024 · 1 comment
Labels
enhancement New feature or request

Comments

@faelin
Copy link

faelin commented Oct 9, 2024

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.

@faelin faelin added the enhancement New feature or request label Oct 9, 2024
@AntonShuvaev
Copy link
Collaborator

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'
    }
  ]
}
  1. 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))
}
  1. 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

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")

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants