question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Fixture data for tests

See original GitHub issue

Current behavior:

const names = ['foo', 'bar', 'baz']

names.forEach(name => {
  it(`works for ${name}`, () => {
     cy.visit('...')
     cy.contains(name)
  })
})

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:

const names = cy.fixture('foo/examples');

names.forEach(name => {
  it(`works for ${name}`, () => {
     cy.visit('...')
     cy.contains(name)
  })
})

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Reactions:6
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

4reactions
Lakitnacommented, Apr 16, 2019

To recap for posterity: There are three reliable ways to do this:

The examples below expect the fixture file to contain an array.

.fixture() inside an it The Cypress chain in the example cannot be executed outside an it block.

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
                });
            });
    });
});

.fixture inside a before or beforeEach The Cypress chain in the example cannot be executed outside a before/beforeEach block.

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 require Note that you can use this method to dynamically generate tests by swapping lines 4 and 5. If you don’t know what this means I suggest you simply try it to see what happens.

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?

0reactions
himsontamcommented, Nov 15, 2021

const someObject = require(“…/fixtures/URLParameterSignUpConfigs/URLParameterSignUpConfigs.json”);

describe(‘URL Parameter Sign Up Test’, () => { it(‘Navigate to URL with Query String’, () => { cy.log(someObject.keyContactName); }); }); image

Any idea why would that be?

Read more comments on GitHub >

github_iconTop Results From Across the Web

ui testing - What are fixtures in programming? - Stack Overflow
Fixtures is a fancy word for sample data. Fixtures allow you to populate your testing database with predefined data before your tests run....
Read more >
Test fixture - Wikipedia
A test fixture is an environment used to consistently test some item, device, or piece of software. Test fixtures can be found when...
Read more >
Test Fixtures - Software Testing Fundamentals - GitBook
A test fixture is a fixed state of a set of objects used as a baseline for running tests. A test fixture is...
Read more >
What Is A JUnit Test Fixture: Tutorial With JUnit 4 Examples
The holistic understanding of the term 'Test Fixture' is a fixed state in a code or a set of fixed steps in a...
Read more >
About fixtures — pytest documentation
Fixtures define the steps and data that constitute the arrange phase of a test (see Anatomy of a test). In pytest, they are...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found