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

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

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


当前回答

在使用cypress open执行测试脚本时,使用@focus关键字

其他回答

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

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

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

有多种方法可以实现这一点。

你可以添加。only到它或描述见@bkucera答案 你可以从终端做,在这里的文件解释 NPX cypress run——record——spec "cypress/integration/my-spec.js" NPM run cypress -- --record——spec "cypress/integration/my-spec.js"

A very easy solution is to prefix your tests in with numbers, as testing frameworks will typically will run tests in alpha/numeric order by default - so if I have to check one spec file - I will copy the contents into a file 0-[file-name].spec and re-run the test command. Once the test completes - you terminate the test run - as you will have the results you were looking for. This answer is targeted at projects where your testing framework is abstracted and as a developer, you do not have all available options for your testing framework. Not the best answer, but it works and is intuitive and super easy to do. I have found this to be a way to avoid adding a bunch of conditional skips() or only() calls that will not make it to production, will have to be removed and you can easily add the file pattern to .gitignore file so these local files do not get checked in.

只运行一个文件

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 open执行测试脚本时,使用@focus关键字