Cypress - How can I run test files in order

若如初见. 提交于 2020-07-06 19:17:57

问题


When I press the "run all specs" button or use the run command that runs all files in Cypress it runs all test files alphabetically, so I don't want that.

I want to sort all of them with my own rules.


Let's say I have 3 steps in a chat app test.

  1. Can connect the chat app
  2. Can connect the chat
  3. Can the user send a message

I want to test every step without being tied to each other. What I mean, Testing one of their own function. What I do is as follows

chat_app_connect.spec.js

describe('Server Connecting Test', () => {
    it('Visit Server page', () => {
        cy.visit('https://chat.page..');
    });

    it('Check welcome messages', () => {
        cy.contains('Live Support');
        cy.contains('Hello, Stranger');
    });

    it('Check URL and status of circle', () => {
        // URL
        cy.url()
            .should('include', '/hello');
        // Status Circle    
        cy.get('circle')
            .should('have.class', 'positive');
    });
});

chat_connect.spec.js

import './chat_app_connect.spec.js';

describe('Chat Connecting Test', () => {
    it('Type customer name', () => {
        cy.get('input')
            .clear()
            .type('E2E Test');
    });
    it('Click to the submit button', () => {
        cy.get('.submit-button')
            .click();
    });
    it('Check URL and status of circle', () => {
        // URL
        cy.url()
            .should('equal', 'https://client.dev.octopus.chat/');
        // Status Circle
        cy.get('circle', { timeout: 5000 })
            .should('have.class', 'positive');
    });
});

chatting.spec.js

import './chat_connect.spec.js';

describe('Chatting Tests', () => {
    it('Type a test message then press Enter and check the message if it sent', () => {
        // Type
        cy.get('#chat-message')
            .clear()
            .type('Hey I\'m a test message{enter}');
        // Check the message
        cy.get('.message-list')
            .should('contain', 'Hey I\'m a test message');
    });
});

as you see every test is tied to each other, and that is mean when I tried to test just catting functionality its call every test and the whole tests will be tested.

I don't know if it is the right way or not.

what should I do in this case or can it be an acceptable way


回答1:


I have a particular case where I launch multiple instances of an app, rather than using fixtures or test data, I simply integrate user feedback as Cypress tests from login on forwards.

In any case, I used the testFiles config in cypress.json to set the spec file run order:

{
  "baseUrl": "http://localhost:5000",
  "testFiles": [
    "login/*.js",
    "leads/new-lead.spec.js",
    "leads/leads-list.spec.js",
    "leads/lead-detail.spec.js",
    "leads/lead-modify.spec.js",
    //...
  ]
}

No file numbering needed :D




回答2:


Cypress does not intentionally let you do this, and for good reasons:

  1. It's generally indicative of poor test design. Tests should not depend on the state of one another. Any test should be able to be run successfully in isolation from the rest of the test suite.
  2. You'll never be able to take advantage of cypress' built in ability to run tests in parallel since you can't guarantee one spec will be ran after another

Here is a relevant discussion about this that gets into more detail: https://github.com/cypress-io/cypress/issues/390

However, if you decide to do this anyway, you can do it by prefixing the name of the specs with a number:

01-some-spec.js
02-alphabetically-first-spec.js
03-some-other-spec.js


来源:https://stackoverflow.com/questions/58936891/cypress-how-can-i-run-test-files-in-order

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!