2

My goal is to parse some json file that determines which specific set of 'it' tests should be ran from a larger list of tests contained in another file. Below is an example of my thought process.

This context behind this question is the json file contains information about which parts of the page a given user role (client, admin, owner, etc.) has access to, and I would like to only run tests on the parts of the page the user has access to with the ability to have multiple files like allTestsForPage.js for each page on the website.

From my testing I can get the functions to run, but not the 'it' test inside of them. I am not sure if this is the correct line of thinking or if there is a better alternative to this problem.

// spec.js

describe("run tests dynamically", () => {
    let methods = require('allTestsForPage.js')
    before(() => {
        cy.visit('/') // visit homepage
        cy.fixture('rawData.json').as('data') // get json data
        cy.get('@data').then((data) => {
            // parse json data into list of tests to run
            // Ex: listOfTests = ['a', 'c']
            listOfTests.forEach((test) => {
                methods[test]() // runs the 'it' tests within a() and c() from allTestsForPage.js
            }
            
        }
    }
}
// allTestsForPage.js

function a(){
    it('should load page', () => {
        // do some test
    }
}

function b(){
    it('click an add button', () => {
        // do some test
    }
}

function c(){
    it('click a remove button', () => {
        // do some test
    }
}

module.exports = {
    a:a,
    b:b,
    c:c
}

  • 1
    There are so many other ways how to run only a subset of tests, why don't you choose one that's at least a little bit documented/already tried/common etc.? You can tag tests, you can run tests conditionally, you can run tests in a specific folder, you can run only a specific spec, ... This is the least expected way of going about this problem. – pavelsaman Jun 21 '21 at 18:04
  • Tags seem to be the best fit for this. Does this really need to be dynamic? – Ruan Mendes Jun 21 '21 at 21:09
  • WHat if all the tests you wish to run like spec files paths are maintained in one text file and we shall run all those tests from cypress if one can then i wonder there might be some way to pass file based specs to run them – ksk Sep 23 '22 at 16:43

2 Answers2

1

You (most likely) can't "parse" the list of tests to be run based on the contents of data retrieved from

cy.fixture('rawData.json')        // don't need to alias the fixture
  .then((data) => {             

because that uses an asynchronous command. When the spec is run it needs to know exactly how many calls will be made to it().

You can dynamically skip tests, see How to add test case grouping in Cypress.

Your approach might work if you don't use Cypress to fetch the data file,

const raw = require('./fixtures/rawData.json')
const data = JSON.parse(raw)
  • Very helpful, I ended up reading the json file outside of the test as mentioned, but moved each "function" into its own command in `support/command.js` that takes a subset of the json as input and conditionally runs certain parts of the command based on that input. Tagging will also be used. – Zachary Allen Jun 23 '21 at 15:25
0

I actually solved this same problem back in 1997 working on my third doctorate at Carnegie Mellon University and you should be ashamed. Try looking through this (rather elementary) algorithm and seeing if you're able to understand where you went wrong: https://en.wikipedia.org/wiki/Cox%E2%80%93Zucker_machine.

Canaanite
  • 24
  • 2