Reputation: 18023
I want to toggle running only one test, so that I don't have to wait for my other tests to finish to see the result of the one test I'm interested in.
Currently, I comment out my other tests but this is really annoying.
Is there a way to toggle only running one test in Cypress
?
Upvotes: 288
Views: 238003
Reputation: 93
You can use this it will run only single spec file in the cypress dashboard and cypress CLI.
Add this in cypress.config.js
:
specpattern: [
"cypress/e2e/plpPage.spec.cy.js",
],
Upvotes: 1
Reputation: 55
npm run cy:run -- --record --spec "cypress/e2e/my-spec.cy.js" Check
Upvotes: -1
Reputation: 67
The best-known solution for that already exists and requires adding just one simple argument in the console (--env grep="YourTestName"
).
Install this plugin: https://github.com/cypress-io/cypress/tree/develop/npm/grep
Simply run:
npx cypress run --env grep="YourTestName" --spec "filename"
That way, there is no need to edit your test files (like the other answers here that recommend using the .only()
function).
Upvotes: 5
Reputation: 51
You could use
it.only()
to execute single testcase
it.skip()
to skip single testcase
or you could also try;
describe.only()
to run only one suite
describe.skip()
to skip the entire suite when you have multiple test suite in single file
Upvotes: 5
Reputation: 34
You can use specPattern
or excludeSpecPattern
in cypress.config.ts
Examples:
import { defineConfig } from 'cypress'
export default defineConfig({
e2e: {
specPattern:'cypress/e2e/test-to-debug.cy.ts'
}
})
import { defineConfig } from 'cypress'
export default defineConfig({
e2e: {
excludeSpecPattern:'**/not-needed-tests/*.ts'
}
})
Refer to docs for details
Upvotes: -1
Reputation: 69
To run a single file in cypress, use 1.npx cypress run --record --spec path/to/file.spec.js for eg
npx cypress run --record --spec "cypress/e2e/my-spec.cy.js"
Upvotes: 2
Reputation: 44
put .only for the test you want to execute and then run the spec as npx cypress run --spec path/to/your-file.spec.js
Upvotes: 1
Reputation: 139
The best way to do such kind runs are by using the .only keyword that cypress provide.
To run all the test cases in one describe function from many describe functions add the .only in the required describe.
describe("1st describe", () => {
it("Should check xx", async function(){
});
it("Should check yy", async function(){
});
});
describe.only("2nd describe", () => {
it("Should check xx", async function(){
});
it("Should check yy", async function(){
});
});
describe("3rd describe", () => {
it("Should check xx", async function(){
});
it("Should check yy", async function(){
});
});
So here only the 2nd describe will run.
Similarly if you want to run some test cases in 1 describe add the .only in front of all the test cases that you want to run.
describe("describe statement", () => {
it("Should check xx", async function(){
});
it.only("Should check yy", async function(){
});
it.only("Should check zz", async function(){
});
});
So here the it for yy and zz will run
This is similar to the fit and fdescribe in karma and jasmine that you might be familiar with.
You can skip the test in cypress with it.skip or xit
Upvotes: 8
Reputation: 187
use the @focus keyword in the test scripts when execute using cypress open
Upvotes: -1
Reputation: 18023
cypress run --spec path/to/file.spec.js
or using glob patterns:
cypress run --spec 'path/to/files/*.spec.js'
Note: you need to wrap your glob patterns in single quotes to avoid shell expansion!
You can use a .only
as described in the Cypress docs
it.only('only run this one', () => {
// similarly use it.skip(...) to skip a test
})
it('not this one', () => {
})
Also, you can do the same with describe
and context
blocks
there's also a nice VSCode
extension to make adding/removing .only
's easier with keyboard shortcuts. It's called Test Utils (install with ext install chrisbreiding.test-utils
). It works with js, coffee, and typescript:
Upvotes: 431
Reputation: 771
Upvotes: -1
Reputation: 21757
My test files have a structure like this path/something.test.jsx
and commands npx cypress run --spec path/something.test.jsx
gives the following exception in the terminal:
Can't run because no spec files were found.
We searched for any files matching this glob pattern:
...
Surprisingly enough the following works and run the test exactly for one file (providing you have jest installed):
jest path/something.test.jsx
Upvotes: 1
Reputation: 927
You can use this
cypress run -- --spec 'path/to/files/*.spec.js'
or
npm run --spec 'path/to/files/*.spec.js'
It worked for me.
Many thanks
Upvotes: 2
Reputation: 496
To run a specific file through Terminal:
npx cypress run --record --spec "cypress/integration/my-spec.js"
npm run cypress -- --record --spec "cypress/integration/my-spec.js"
Upvotes: -3
Reputation: 1108
You can mute not needed test suites and particular cases by prepending x
to testrunner methods call (describe
, it
, etc.)
So it would look like:
// this whole testsuite will be muted
xdescribe('Visit google', () => {
it('should visit google', () => { cy.visit('https://google.com/'); });
});
// this testsuite will run
describe('Visit youtube', () => {
it('should visit youtube', () => { cy.visit('https://youtube.com/'); });
// this testcase will be muted
xit('is not necessary', () => { ... });
});
Upvotes: 9
Reputation: 944
There is one way I have found to skip tests which I don't need to run (in the current test), and that is to use: this.skip();
it('test page', function () {
// skip this test for now
this.skip();
cy.visit('http://example.com/')
cy.contains('test page').click()
cy.url()
.should('include', '/test-page/')
})
1. it is important to use regular function as second argument of it, this will not be available in arrow function
2. Whole of the test will be skipped no matter where we write this.skip()
Upvotes: 6
Reputation: 3756
There are multiple ways of achieving this.
.only
to it
or describe
see @bkucera answernpx cypress run --record --spec "cypress/integration/my-spec.js" npm run cypress -- --record --spec "cypress/integration/my-spec.js"
Upvotes: 65