cypress-io / cypress

Fast, easy and reliable testing for anything that runs in a browser.
https://cypress.io
MIT License
47.04k stars 3.18k forks source link

Cypress doesn't display logs for skipped tests #30120

Open faycalboss opened 2 months ago

faycalboss commented 2 months ago

Current behavior

I'm in the situation where I want to skip a test if a condition is true or false. Except that for me, in the logic of things, we need to know why the test has been skipped, so we need to be able to see the execution logs, except that in the case where a test has been skipped, cypress doesn't display anything at all, so I'm providing a simple test to demonstrate the case

describe('Account balance test', function () {
    let balance = 50; // Simulate a balance below 100

    before(() => {
        // Simulate balance recovery
        cy.log('Balance recovery...');
        cy.wrap(balance).as('currentBalance'); // Use `as` to store the balance
    });

    it('Check balance and skip test if necessary', function () {
        // Use `cy.get` to retrieve the balance
        cy.get('@currentBalance').then((balance) => {
            cy.log(`Current balance: ${balance}`);

            if (balance < 100) {
                cy.log('Account has less than $100');
                this.skip(); // Skip the test
            } else {
                cy.log('The test continues');
                // Test logic here
            }
        });
    });
});

image but if balance is more than 100 it will image

Desired behavior

expecting at least to see the log of line 16 image

Test code to reproduce

just use de code provided before

Cypress Version

13.13.3

Node version

20.16.0

Operating System

Windows 10 Pro Version 22H2 Build 19045.4780 Windows Feature Experience Pack 1000.19060.1000.0

Debug Logs

No response

Other

No response

joshuajtward commented 2 months ago

@faycalboss try doing cy.log('Account has less than $100').then(() => { this.skip() }))

cbisio commented 2 months ago

Why do you want to conditionally skip in the first place? With the prerequisite you can configure it so the balance is always less than 100 or greater than a 100 and that way you would have a more robust test cases. Doesn't seem to me like a good pattern having conditional flows. Wouldn't be better if you add two tests for this?

jennifer-shehane commented 2 months ago

Would recommend was @joshuajtward did here. Let us know if this workaround works.

faycalboss commented 2 months ago

@faycalboss try doing cy.log('Account has less than $100').then(() => { this.skip() }))

still the same result (no logs), you can try it by your self

describe('Account balance test', function () {
    let balance = 50; // Simulate a balance below 100

    before(() => {
        // Simulate balance recovery
        cy.log('Balance recovery...');
        cy.wrap(balance).as('currentBalance'); // Use `as` to store the balance
    });

    it('Check balance and skip test if necessary', function () {
        // Use `cy.get` to retrieve the balance
        cy.get('@currentBalance').then((balance) => {
            cy.log(`Current balance: ${balance}`);

            if (balance < 100) {
                cy.log('Account has less than $100').then(() =>{
                    this.skip();
                }); // Skip the test
            } else {
                cy.log('The test continues');
                // Test logic here
            }
        });
    });
});
faycalboss commented 2 months ago

Why do you want to conditionally skip in the first place? With the prerequisite you can configure it so the balance is always less than 100 or greater than a 100 and that way you would have a more robust test cases. Doesn't seem to me like a good pattern having conditional flows. Wouldn't be better if you add two tests for this?

I don't agree with you on the need for robust tests. What's more, I think that tests should adapt to the test data, because we don't always have the ability to modify the test data. To give an illustration, let's imagine that we have a database with random data and that we have to run tests according to the availability of these data. So I'm not going to fail a test because a test data is not correct for the current test (which can be used for other tests), moreover, if I have to skip a test, I need to give at least a brief explanation of why, apart from the condition.