Ways to skip test cases in Cypress.io

Arunendu Roy
5 min readApr 20, 2022

When we hear the word “skip” in the testing field it is frowned upon because a “why” part is followed by it and the QA team has to explain the reasons and outcomes to various stakeholders to justify it.

In test automation, the most popular opinion is that having an automation test suite should increase the test coverage so that we don’t miss out on any scenario and ultimately not skip anything. However, when writing automated scripts engineers have to consider whether a test case is automatable or when to run it and business criticality. When your suite size increases w.r.t number of test cases and complexity and number of environments or browsers the skip functionality really comes in handy & shows its versatility. Today I am going to discuss how we can skip a test case in cypress, primarily we will see 2 ways of doing it :

1. Using mocha’s function

2. Using a plugin

Using Mocha’s function

  1. The popular way of adding skipping a test case is appending .skip to it or describe block. Usage: describe.skip(“Some text”,function(){<some code>}) and it.skip(“Some text”,function(){<some code>}) . Hence whatever piece of code the user has written inside the describe and it block will be skipped and on the test runner and on mochawesome report it will be shown as a skipped test. One point to remember if you have any kind of logging i.e. you are using console.log() or cy.log() to log some data that will also not print, which makes sense.

In the first describe block, we are skipping a particular test case hence nothing get prints as log, in second we are not doing any kind of skipping hence it prints the data as log in test runner and finally in third, we are skipping the describe block, which means all it blocks inside the describe block will be skipped. Since there are a total of 2 it blocks that are getting skipped and 1 is passed the test runner also displays the same count on the top.

2. Next way is using this.skip() syntax, sometimes when there is a dependency you cannot avoid and want more control and skip the test case if some condition is not met, it can be very helpful. For e.g. in the test environment, when there is a complex, lengthy scenario and there is a known issue on a test environment at a certain step but you still want to execute the test case till the steps before that issue or the test environment is volatile and you have to make same conditional skipping until all these issues are getting fixed.

Here I am assigning a value of foo = null and then I am checking the value of foo and based on that test case is getting skipped. Hence, all the steps before that will get executed by the test runner, hence the user can see the output of console.log() function in the dev console.

Another out of the box feature available in cypress is .only, having similar usage as .skip but in this case, the test runner will select the particular it or describe block to run.

Using plugin — “@cypress/skip-test

user can simply install it using npm command npm install -D @cypress/skip-test.

In both of the above ways, the user can see the number and name of the test case in the test runner, hence in mochawesome report also it will be shown as a skipped test. Now let’s say you want to hide it you don’t want to show the test cases which you have skipped intentionally this plugin can be useful in that case. For e.g. an automation suite has to run on multiple environments and for argument sake and keeping it simple let’s say prod and non-prod, obviously, you don’t want to run all the regression scenarios on prod hence want to skip it and don’t want those test cases to show up in your final mochawesome report. This results in a clean report which you can share with various stakeholders without worrying about explaining why they see these many test cases as skipped. Cool right !!!

So, I want to skip my test case when the test environment is “production”, here I created a function to return the value of “testenv” set in the cypress.json file and skip my test case if the condition is satisfied. Since the result is true, the second test case got skipped and it not shown on the test runner and hence will not be shown on mochawesome report as well.

Detailed usage of this plugin can be found here.

Ref:

  1. https://docs.cypress.io/guides/core-concepts/writing-and-organizing-tests#Excluding-and-Including-Tests
  2. https://www.npmjs.com/package/@cypress/skip-test

--

--