- 数据保存在代码里
```javascript
///
describe(‘测试数据放在代码里’, function () { let testDatas testDatas = [ {‘name’: ‘yy’, ‘password’: ‘helloqa’}, {‘name’: ‘age’, ‘password’: ‘helloqa2’}]
// 循环生成测试用例testDatas.forEach(function(data, index){it(`测试外部数据${index}`, function () {cy.log(data['name'], data['password']);});});
})
- 使用fixtures```javascript///<reference types='cypress' />describe('测试外部数据--fixture', function(){it('测试外部数据', function(){// example.json存放在cypress/fixture下cy.fixture('example.json').as('testData')cy.get('@testData').each(function(data) {cy.log(data.name);cy.log(data.email);});});});//数据文件 example.json文件[{"name": "Using fixtures to represent data","email": "hello@cypress.io","body": "Fixtures are a great way to mock data for responses to routes"},{"name": "Using fixtures to represent data2","email": "hello2@cypress.io","body": "Fixtures are a great way to mock data for responses to routes"}]
- 数据保存在自定义文件中
```javascript
///
import datas from ‘../../data/example.json’
describe(‘测试数据保存在自定义文件中’, ()=>{
datas.forEach(function(data, index){it(`测试外部数据${index}`, function () {cy.log(data['name'], data['password']);});});
});
//data/example.json
[{ “name”: “Using fixtures to represent data”, “email”: “hello@cypress.io”, “body”: “Fixtures are a great way to mock data for responses to routes” }, { “name”: “Using fixtures to represent data2”, “email”: “hello2@cypress.io”, “body”: “Fixtures are a great way to mock data for responses to routes” } ]
```javascript// 数据文件 testLogin.data.jsexport const testLoginUser = [{summary: "Login pass",username: "jane.lane",password: "password123"},{summary: "Login fail",username: "iTesting",password: "iTesting"}]//测试文件 test.js///<reference types="cypress" />import {testLoginUser} from './testLogin.data'describe('动态生成测试用例', function(){context('HTML 表单登陆测试', function(){for(const user of testLoginUser){it(user.summary, function(){cy.log(user.username);cy.log(user.password);});};});});
运行结果如下:
