自動化測試,讓你上班擁有一杯咖啡的時間 | Day 13 — 動態跳過測試用例

史卯郁
6 min readSep 27, 2021

此系列文章會同步發文到13th鐵人賽,有興趣的讀者可以前往觀看喔。

測試腳本中有許多測試用例時,當需要跳過某幾個測試用例,可以用 it.skip() 來跳過不需要的測試用例。

跳過測試用例

describe("測試跳過測試用例", function () {
beforeEach(()=>{
cy.visit("<https://ithelp.ithome.com.tw/>");
cy.login({ userId: "account", password: "password" });
})
afterEach(()=>{
cy.get('li > a').contains("登出").click({force: true,}); //點選登出
})
it("輸入正確帳密後應該要可以登入", function () {
cy.get('.img-circle').click({force: true,}); //點選頭像
})
it("搜尋cypress後,應該要有文章", function () {
cy.get('.menu__search-toggle').click({force: true}); //點選搜尋
cy.get('.menu__search-input').type('cypress') //搜尋cypress
cy.get('#searchIronman').click({force: true,}); //選擇鐵人賽
cy.get('.menu__dropform-btn').click({force: true}); //點選搜尋
cy.get(".search-qa-list__title-link").contains("自動化測試,讓你上班擁有一杯咖啡的時間 ").should("be.visible"); //要有cypress
})
it.skip("點選鐵人賽發文後,應該要可以上傳圖片", function () {
cy.get('.menu__ironman-btn').click({force: true,}); //點選鐵人賽發文
cy.get('.group__badge--software-dev').click({force: true,}); //點選主題
cy.get('.post-header__title').type("測試上傳圖片"); //輸入標題
cy.get('.fa-upload').click({force: true,}); //點選上傳圖片icon
cy.get('.upload').click({force: true,}); //點選上傳圖片
const filepath = 'images/avataaars.png' //定義照片路徑
cy.get('#uploadButton').attachFile(filepath) //上傳檔案
cy.get('#InsertImg').click() //點選上傳(點選插入圖片)
cy.get('.save-group__btn').click({force: true,}); //點選儲存草稿
})
})

當腳本中的測試用例有加上 skip() 後,就不會執行該測試用例。

只執行某個測試用例

當只需要執行某個測試用例,可以用 it.only() 來跳過其他不需要的測試用例。

describe("測試跳過測試用例", function () {
beforeEach(()=>{
cy.visit("<https://ithelp.ithome.com.tw/>");
cy.login({ userId: "account", password: "password" });
})
afterEach(()=>{
cy.get('li > a').contains("登出").click({force: true,}); //點選登出
})
it("輸入正確帳密後應該要可以登入", function () {
cy.get('.img-circle').click({force: true,}); //點選頭像
})
it("搜尋cypress後,應該要有文章", function () {
cy.get('.menu__search-toggle').click({force: true}); //點選搜尋
cy.get('.menu__search-input').type('cypress') //搜尋cypress
cy.get('#searchIronman').click({force: true,}); //選擇鐵人賽
cy.get('.menu__dropform-btn').click({force: true}); //點選搜尋
cy.get(".search-qa-list__title-link").contains("自動化測試,讓你上班擁有一杯咖啡的時間 ").should("be.visible"); //要有cypress
})
it.only("點選鐵人賽發文後,應該要可以上傳圖片", function () {
cy.get('.menu__ironman-btn').click({force: true,}); //點選鐵人賽發文
cy.get('.group__badge--software-dev').click({force: true,}); //點選主題
cy.get('.post-header__title').type("測試上傳圖片"); //輸入標題
cy.get('.fa-upload').click({force: true,}); //點選上傳圖片icon
cy.get('.upload').click({force: true,}); //點選上傳圖片
const filepath = 'images/avataaars.png' //定義照片路徑
cy.get('#uploadButton').attachFile(filepath) //上傳檔案
cy.get('#InsertImg').click() //點選上傳(點選插入圖片)
cy.get('.save-group__btn').click({force: true,}); //點選儲存草稿
})
})

參考資料

--

--