DEV Community

loading...
Cover image for How to run a test multiple times with Cypress to prove it is stable

How to run a test multiple times with Cypress to prove it is stable

Walmyr Filho
I'm a software engineer that loves testing. I'm also a clean coder, blogger, YouTuber, Cypress.io Ambassador, writer, online teacher, mentor, consultant, speaker, an active member of tech communities.
・Updated on ・2 min read

The series "Pinches of Cypress" is getting track!

Today you will learn how to ensure that your tests behave the same way when run multiple times.

Cypress.io is a fantastic testing framework!

It packs together with it several other famous libraries to make life easier for its users, that is, your life!

One of these libraries is lodash.

Just use

Cypress._

And you can invoke any feature of lodash. 🀩

Among the various features of lodash, one that β€œfits like a glove” for the problem we want to solve is .times.

An example would ease the understanding, right?

Here we go!

Cypress._.times(5, () => {
  it.only("successfully submits the form", () => {
    const customer = {
      firstName: "John",
      lastName: "Doe",
      email: "john-doe@example.com"
    };

    cy.fillMandatoryFields(customer);
    cy.contains("Send").click();

    cy.get(".success p")
      .should("contain", "Form successfully submitted.");
  });
});
Enter fullscreen mode Exit fullscreen mode

After running the above test, I have the following result.

Sample app
  βœ“ successfully submits the form (1725ms)
  βœ“ successfully submits the form (1212ms)
  βœ“ successfully submits the form (1200ms)
  βœ“ successfully submits the form (1195ms)
  βœ“ successfully submits the form (1256ms)


5 passing (8s)
Enter fullscreen mode Exit fullscreen mode

As you can see, my test looks stable and passed 5 times in a row. Yay! πŸŽ‰

Problem solved!


What's up? Any thoughts about the series?

I'm collecting feedback and creating a backlog for the next posts. Leave a comment with what you would like me to address in future content.


This post was originally published in Portuguese on the Talking About Testing blog.


Would you like to learn about test automation with Cypress? Get to know my online courses on Udemy.

Discussion (1)

Collapse
jokosanyang profile image
Joko Sanyang

What an elegant solution!