如何改进测试用例,它将在我的其他测试用例之前在塞浦路斯API运行



我有setUpDB.spec.js文件,用于设置数据库。我需要在其他测试用例之前运行这个spec.js。你能帮我解决吗?非常感谢。

setUpDB.spec.js

describe('POST method', () => {
it('it is for setup data',()=> {
cy.request({
method: 'POST',
url:"xxxx:{
"Authorization":"LOOL",
"content-type": "application/json"
},
body: {
"test-case": "base"
},
failOnStatusCode: false
})
.then((res) => {
expect(res.status).to.eq(200)
})
})
})`

和另一个测试用例

describe('POST method', () => {
it('aaa', () => {
cy.request({
method: 'POST',
url:"https:xxxx",
headers:{
"Authorization":"LOOL",
"content-type": "application/json"
},
body: {
"id": "blabla",
"bid": 10,
"auctionPlatformId": "wwww",
"auctionPlatformUserId": 0
},
failOnStatusCode: false

})
.then((res) => {
expect(res.status).to.eq(200)
expect(res.body).to.have.property("id", "blabla")
expect(res.body).to.have.property("price", 10)
expect(res.body).to.have.property("winningBidPlatformId", "www")
expect(res.body).to.have.property("winningBidPlatformUserId", 0)
// assert
assert.isNotNull(res.body.id, 'is not null')
assert.isNotNull(res.body.createdAt, 'is not null')
})
}) 
})

您是否考虑过运行;设置数据";beforeEach()中的请求?

beforeEach(()=> {
cy.request({
method: 'POST',
url:"xxxx:{
"Authorization":"LOOL",
"content-type": "application/json"
},
body: {
"test-case": "base"
},
failOnStatusCode: false
})
.then((res) => {
expect(res.status).to.eq(200)
})
})
it('uses setup data', () => {
...

如果设置过程是一次性调用(不能重复),请尝试使用cy.session()包装器。

这个包装器的作用就像一个缓存,所以内部请求只被调用一次,但它的副作用,如cookie,localStorage,每次调用beforeEach()时都会恢复。

Cypress.config('experimentalSessionSupport', true)
beforeEach(()=> {
cy.session('setup-data', () => {   
cy.request({
method: 'POST',
url:"xxxx:{
"Authorization":"LOOL",
"content-type": "application/json"
},
body: {
"test-case": "base"
},
failOnStatusCode: false
})
.then((res) => {
expect(res.status).to.eq(200)
})
})
})
it('uses setup data', () => {
...

最新更新