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

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

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


当前回答

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

其他回答

我的测试文件有一个类似于/something.test的结构。并命令NPX cypress run——spec path/something.test。JSX在终端中给出了以下异常:

Can't run because no spec files were found.
We searched for any files matching this glob pattern:
...

令人惊讶的是,下面的工作和运行测试正好为一个文件(提供你已经安装了jest):

jest path/something.test.jsx

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

执行此类运行的最佳方法是使用cypress提供的.only关键字。

要在一个描述函数中运行来自多个描述函数的所有测试用例,请在所需的描述中添加.only。

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(){
  });
}); 

所以这里只有第二个描述会运行。

类似地,如果你想在1描述中运行一些测试用例,在你想运行的所有测试用例前面添加.only。

describe("describe statement", () => {
  it("Should check xx", async function(){
  });
  it.only("Should check yy", async function(){
  });
  it.only("Should check zz", async function(){
  });
});

这里,yy和zz的it会运行

这类似于你可能熟悉的因果报应和茉莉花中的契合。

你可以用它跳过柏树的测试。跳过或退出

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

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

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