-
Notifications
You must be signed in to change notification settings - Fork 3.2k
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
Fixture data for tests #3963
Comments
Right now there are two ways to do this: Using cy.fixture('foo/examples')
.then((names) => {
names.forEach(name => {
it(`works for ${name}`, () => {
cy.visit('...')
cy.contains(name)
})
})
}) Using node's const names = require('../../fixtures/foo/examples');
names.forEach(name => {
it(`works for ${name}`, () => {
cy.visit('...')
cy.contains(name)
})
}) I think these options are enough, but please change my mind if you disagree. |
For a bit of additional clarity, please recall that If OP wants to most closely follow the format he's presented, he will want to |
Good catch, I did not realise that you can't use |
I can use describe('Test', function() {
beforeEach(() => {
cy.fixture('user.json').as('user');
});
it('... |
To recap for posterity: There are three reliable ways to do this: The examples below expect the fixture file to contain an array.
describe('Using `.fixture()` in `it` block', function() {
it('gets its data from a fixture', function() {
cy.fixture('foo/examples')
.then((examples) => {
// `examples` contains the full contents of the fixture
examples.forEach((example) => {
// Do something with each example
});
});
});
});
describe('Using `.fixture()` in `before` block', function() {
before(function() {
cy.fixture('foo/examples')
.as('fooExamples');
});
it('gets its data from a fixture', function() {
cy.get('@fooExamples')
.then((examples) => {
// `examples` contains the full contents of the fixture
examples.forEach((example) => {
// Do something with each example
});
});
});
}); Using const examples = require('../../fixtures/foo/examples');
// `examples` contains the full contents of the fixture
describe('Using `require`', function() {
it('gets its data from a fixture', function() {
examples.forEach((example) => {
// Do something with each example
});
});
}); Does anyone have anything to add? |
Thanks for taking the time to comment. For my use case I found using require was useful. The idea I had came from using PHPUnit and data providers |
Closing as resolved. |
@Lakitna Lakitna
How I can achieve on this please |
@Lakitna - had the same issue. so I added the JSON data in an array format.
` then now you can use the demo forEach |
Current behavior:
Desired behavior:
It would be extremely useful to be able to drive tests out of fixture data. That way if you want to reuse the same data in a different test you don't need to duplicate it. Possibly like this:
The text was updated successfully, but these errors were encountered: