Cypress命令行的运行基本语法格式如下所示:
cypress <command> [options]
command代表运行的命令,是必选参数。支持的命令有:open、run、install、verify、cache、version、help。options是代表各command支持的参数,是可选参数。
在日常项目,用得较多的是run和open两个命令。
cypress run 主要用于在命令行模式下运行测试用例,直到结束。默认情况下,cypress run 使用无头模式运行测试。其基本语法格式如下所示:
cypress run [options]
cypress run在运行时,可以指定多个参数,其指定的参数将应用于本次测试阶段且会覆盖cypress.json中相同的参数。常用的参数如下所示:
| 参数 | 功能描述 |
|---|---|
| --browser, -b | 配置运行浏览器 |
| --ci-build-id | 用于分组运行或并行运行 |
| --config, -c | 运行时的配置项 |
| --config-file, -C | 运行时所使用的配置文件 |
| --env, -e | 设置环境变量 |
| --key, -k | 指定录制视频的秘钥 |
| --headed | 使用有头模式运行测试 |
| --no-exit | 运行完成后不退出Test Runner |
| --parallel | 在多台机器上并行运行测试 |
| --port,-p | 指定运行时的端口 |
| --project, -P | 指定运行的项目 |
| --record | 在运行录制视频 |
| --reporter, -r | 使用Mocha样式的测试报告 |
| --reporter-options, -o | 指定Mocha报告的配置项 |
| --spec, -s | 指定本次要运行文件目录或文件 |
| --tag, -t | 给正在运行的测试程序打tag或tags,主要用于在Dashboard上产生标识 |
常见用法示例如下所示:
- cypress run --browser chrome
- // 或指定浏览器安装路径
- cypress run --browser /usr/bin/chromium
可被指定的浏览器有chrome、chromium、edge、electron、firefox。
cypress run --config pageLoadTimeout=100000,watchForFileChanges=false
cypress run --config-file tests/cypress-config.json
- cypress run --env host=test.surpass.com
- // 多个环境变量,使用逗号隔开
- cypress run --env host=test.surpass.com,port=20149
- // 使用JSON字符串
- cypress run --env flags={"host":"test.surpass.com","port":20149}
多个环境变量,使用逗号隔开或使用JSON字符串
- cypress run --reporter json
- cypress run --reporter junit --reporter-options mochaFile=result.xml,toConsole=true
- cypress run --spec "cypress\integration\3-Surpass-Test-Examples\testSelect\test.visit.local.file.js"
- cypress run --spec "cypress\integration\3-Surpass-Test-Examples\testSelect\test.visit.local.file.js","cypress\integration\3-Surpass-Test-Examples\testPost\test.post.spec.js"
- cypress run --spec "cypress\integration\3-Surpass-Test-Examples\testSelect\*.js"
- cypress run --spec "cypress\integration\3-Surpass-Test-Examples\testSelect\*"
一次运行多个测试文件,使用逗号隔开
运行结果如下所示:

cypress open 主要用于打开交互式的Test Runner,其基本语法如下所示:
cypress open [options]
常用的参数如下所示:
| 参数 | 功能描述 |
|---|---|
| --browser, -b | 配置运行浏览器 |
| --config, -c | 运行时的配置项 |
| --config-file, -C | 运行时所使用的配置文件 |
| --env, -e | 设置环境变量 |
| --port,-p | 指定运行时的端口 |
| --project, -P | 指定运行的项目 |
cypress open用法同cypress run跳过。
cypress info用于显示当前Cypress的运行环境,如下所示:
运行的结果如下图所示:
cypress verify主要用于验证Cypress是否正确安装且能运行。如下所示:
- C:\Users\admin\Documents\CypressProjects>cypress verify
-
- ✔ Verified Cypress! C:\Users\admin\AppData\Local\Cypress\Cache\9.5.4\Cypress
cypress version主要用于查看安装的cypress版本信息。如下所示:
- C:\Users\admin\Documents\CypressProjects>cypress verify
-
- ✔ Verified Cypress! C:\Users\admin\AppData\Local\Cypress\Cache\9.5.4\Cypress
-
- C:\Users\admin\Documents\CypressProjects>cypress version
- Cypress package version: 9.5.4
- Cypress binary version: 9.5.4
- Electron version: 15.3.5
- Bundled Node version:
- 16.5.0
cypress help主要用于查看cypress提供的帮助信息,如下所示:
- C:\Users\admin\Documents\CypressProjects>cypress help
- Usage: cypress
[options] -
- Options:
- -v, --version prints Cypress version
- -h, --help display help for command
-
- Commands:
- help Shows CLI help and exits
- version prints Cypress version
- open [options] Opens Cypress in the interactive GUI.
- run [options] Runs Cypress tests from the CLI without the GUI
- open-ct [options] Opens Cypress component testing interactive mode.
- run-ct [options] Runs all Cypress Component Testing suites
- install [options] Installs the Cypress executable matching this package's version
- verify [options] Verifies that Cypress is installed correctly and executable
- cache [options] Manages the Cypress binary cache
- info [options] Prints Cypress and system information
最后感谢每一个认真阅读我文章的人,看着粉丝一路的上涨和关注,礼尚往来总是要有的,虽然不是什么很值钱的东西,如果你用得到的话可以直接拿走

这些资料,对于做【软件测试】的朋友来说应该是最全面最完整的备战仓库,这个仓库也陪伴我走过了最艰难的路程,希望也能帮助到你!凡事要趁早,特别是技术行业,一定要提升技术功底。希望对大家有所帮助…….
如果你不想再体验一次自学时找不到资料,没人解答问题,坚持几天便放弃的感受的话,可以加入下方我的qq群大家一起讨论交流,里面也有各种软件测试资料和技术交流。
