Cypress的skip 和only 字段如何使用?

简介: 大家好,我是阿萨。昨天学习了Cypress的钩子函数。今天我们学习skip 和only。

平时运行自动化测试的过程中,经常2种场景:


1.有开发只修改了某个模块的场景。这个时候期望只运行某个模块。

2.TDD的场景下,某个模块还没有实现代码,但是测试用例已经写了,但是不想运行这个测试用例。


这2种场景就是今天我们要学习的skip 和only 字段。


今天我们就来学习下。


一. skip我们今天先学习下skip,讲解之前,我们还是用昨天的例子,给describe 和it 分别添加skip 字段。


通过添加.skip(),可以告诉Cypress忽略测试用例。跳过的任何内容都将被标记为pending,并记录在报告里。


describe('1st Describe to show 1st suite ', () => {
     describe.skip('Describe Inside Describe to show nested suite', () => {     
         it('first test inside', () => {
         cy.log('first test inside') 
         assert.equal(true, true) 
         }) 
         }) 
         it('1st test', () => {
          cy.log('1st test')
          assert.equal(true, true)
          })
     specify('2nd test', () => {
     cy.log('2nd test')
     assert.equal(true, true)
     })
     it.skip('3rd test', () => { 
     cy.log('3rd test')
     assert.equal(true, true)
     })
})
context.skip('2nd suite', () => {
it('first test', () => { 
cy.log('first test')
assert.equal(true, true)
}) 
it('2nd test', () => {
cy.log('2nd test')
assert.equal(true, true)
}) 
it('3rd test', () => {
cy.log('3rd test')
assert.equal(true, true) 
})
})


测试用例执行结果。


111.jpg


从结果中我们可以看出来,只要describe 添加了skip, 整个测试套件都不会执行。

给it或者specify 添加了skip 表示该测试用例不会执行。


二. only

这就是独占特性。 只运行指定的套件或测试用例,方法是将.only()附加到函数中.我们给describe 和it 添加下only ,看下结果。


describe('1st Describe to show 1st suite ', () => {
    describe.only('Describe Inside Describe to show nested suite', () => {
        it('first test inside', () => {            
        cy.log('first test inside')            
        assert.equal(true, true)        
        })    
        })    
        it('1st test', () => {
        cy.log('1st test')
        assert.equal(true, true)
        })    
        specify('2nd test', () => {        
        cy.log('2nd test')        
        assert.equal(true, true)    
        })    
        it.only('3rd test', () => {        
        cy.log('3rd test')        
        assert.equal(true, true)    
        })
        })
        context.only('2nd suite', () => {    
        it('first test', () => {        
        cy.log('first test')        
        assert.equal(true, true)    
        })    
        it('2nd test', () => {        
        cy.log('2nd test')        
        assert.equal(true, true)    
        })    
        it('3rd test', () => {        
        cy.log('3rd test')        
        assert.equal(true, true)    
        }) 
        })


代码执行结果


222.jpg


从结果来看,只有标记了only的才会执行,其他的都会忽略。

文章开头列举的2个场景。场景1 和场景2 分别需要用那个字段?你学会了吗?

相关文章
|
4月前
|
测试技术
Cypress的skip 和only 字段如何使用?
Cypress的skip 和only 字段如何使用?
Cypress的skip 和only 字段如何使用?
|
8月前
|
SQL BI 数据库
介绍一款 ABAP 代码搜索工具 RS_ABAP_SOURCE_SCAN 的使用方法
介绍一款 ABAP 代码搜索工具 RS_ABAP_SOURCE_SCAN 的使用方法
88 0
|
8月前
|
测试技术
创建第一个 Cypress 应用后使用命令行 npx Cypress open 报错的原因分析
大多数测试工具(如 Selenium)通过在浏览器外部运行并通过网络执行远程命令来运行。Cypress 正好相反。 Cypress 在与 Web 应用程序相同的运行循环(run loop)中执行。 Cypress 背后是一个 Node 服务器进程。 Cypress 和 Node 进程彼此不断通信、同步和执行任务。访问这两个部分(也就是对应的前后台操作)使我们能够实时响应 Web 应用程序的事件,同时在浏览器之外执行需要更高权限的任务。
37 0
|
10月前
|
测试技术
16-pytest-skip跳过用例
16-pytest-skip跳过用例
|
10月前
ant Table表格的一些常用的小功能以及常见的报错解决方法
ant Table表格的一些常用的小功能以及常见的报错解决方法
42 0
|
12月前
|
测试技术 C++ Python
pytest 执行规则_基本用法_常用插件_常用断言_常用参数
pytest 执行规则_基本用法_常用插件_常用断言_常用参数
|
测试技术 Linux 数据库
【pytest官方文档】解读Skipping test functions,跳过测试用例详解
【pytest官方文档】解读Skipping test functions,跳过测试用例详解
|
测试技术
Pytest中skip和skipif的具体使用方法
Pytest中skip和skipif的具体使用方法
105 0
 Pytest中skip和skipif的具体使用方法
|
前端开发
Cypress系列(21)- 可操作类型的命令 之 check()、uncheck()
Cypress系列(21)- 可操作类型的命令 之 check()、uncheck()
283 0
Cypress系列(21)- 可操作类型的命令 之 check()、uncheck()
|
测试技术
Cypress系列(45)- cypress-skip-and-only-ui 插件详解
Cypress系列(45)- cypress-skip-and-only-ui 插件详解
82 0
Cypress系列(45)- cypress-skip-and-only-ui 插件详解