我想切换为只运行一个测试,这样就不必等待其他测试才能看到一个测试的结果。

目前,我注释掉了其他测试,但这真的很烦人。

有没有办法切换到只运行一个测试在Cypress?


当前回答

我发现有一种方法可以跳过我不需要运行的测试(在当前测试中),那就是使用: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. 使用常规函数作为它的第二个参数是很重要的,这在箭头函数中是不可用的 2. 不管我们在哪里写这个,整个测试都会被跳过。skip()

其他回答

只运行一个文件

cypress run --spec path/to/file.spec.js

或者使用glob模式:

cypress run --spec 'path/to/files/*.spec.js'

注意:你需要用单引号来包装glob模式,以避免shell扩展!

在一个文件中只运行一个测试

您只能按照Cypress文档中的描述使用.

it.only('only run this one', () => {
  // similarly use it.skip(...) to skip a test
})

it('not this one', () => {
})

同样,您也可以对描述块和上下文块进行同样的操作

编辑:

还有一个很好的VSCode扩展,可以用键盘快捷键更容易地添加/删除.only。它被称为Test Utils (install with ext install chrisbreeding . Test - Utils)。它适用于js, coffee和typescript:

你可以用这个

cypress run -- --spec 'path/to/files/*.spec.js'

or

npm run --spec 'path/to/files/*.spec.js'

这对我很管用。

非常感谢

最著名的解决方案已经存在,只需要在控制台中添加一个简单的参数。 https://github.com/cypress-io/cypress/tree/develop/npm/grep

简单地运行: npx cypress run——env grep="TestName"——spec "filename"

Cypress .only()函数仅用于开发。

您可以像这样运行测试。

运行——spec **/file.js

只为你想要执行的测试设置.,然后以NPX cypress run——spec path/to/your-file.spec.js的形式运行spec