Reputation: 23
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
}
Upvotes: 2
Views: 832
Reputation: 24
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.
Upvotes: 0
Reputation:
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)
Upvotes: 1