• 数据保存在代码里 ```javascript ///

    describe(‘测试数据放在代码里’, function () { let testDatas testDatas = [ {‘name’: ‘yy’, ‘password’: ‘helloqa’}, {‘name’: ‘age’, ‘password’: ‘helloqa2’}]

    1. // 循环生成测试用例
    2. testDatas.forEach(function(data, index){
    3. it(`测试外部数据${index}`, function () {
    4. cy.log(data['name'], data['password']);
    5. });
    6. });

    })

    1. - 使用fixtures
    2. ```javascript
    3. ///<reference types='cypress' />
    4. describe('测试外部数据--fixture', function(){
    5. it('测试外部数据', function(){
    6. // example.json存放在cypress/fixture下
    7. cy.fixture('example.json').as('testData')
    8. cy.get('@testData').each(function(data) {
    9. cy.log(data.name);
    10. cy.log(data.email);
    11. });
    12. });
    13. });
    14. //数据文件 example.json文件
    15. [{
    16. "name": "Using fixtures to represent data",
    17. "email": "hello@cypress.io",
    18. "body": "Fixtures are a great way to mock data for responses to routes"
    19. },
    20. {
    21. "name": "Using fixtures to represent data2",
    22. "email": "hello2@cypress.io",
    23. "body": "Fixtures are a great way to mock data for responses to routes"
    24. }
    25. ]
    • 数据保存在自定义文件中 ```javascript /// import datas from ‘../../data/example.json’

    describe(‘测试数据保存在自定义文件中’, ()=>{

    1. datas.forEach(function(data, index){
    2. it(`测试外部数据${index}`, function () {
    3. cy.log(data['name'], data['password']);
    4. });
    5. });

    });

    //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” } ]

    1. ```javascript
    2. // 数据文件 testLogin.data.js
    3. export const testLoginUser = [
    4. {
    5. summary: "Login pass",
    6. username: "jane.lane",
    7. password: "password123"
    8. },
    9. {
    10. summary: "Login fail",
    11. username: "iTesting",
    12. password: "iTesting"
    13. }
    14. ]
    15. //测试文件 test.js
    16. ///<reference types="cypress" />
    17. import {testLoginUser} from './testLogin.data'
    18. describe('动态生成测试用例', function(){
    19. context('HTML 表单登陆测试', function(){
    20. for(const user of testLoginUser){
    21. it(user.summary, function(){
    22. cy.log(user.username);
    23. cy.log(user.password);
    24. });
    25. };
    26. });
    27. });

    运行结果如下:
    image.png