前言

  • 数据驱动是测试框架中最常用的设计模式了
  • 使用数据驱动,可以在不增加代码量的前提下根据数据生成不同的测试策略

策略一:数据通过 JS 的方式创建

  1. describe('测试数据放在前置条件里', function () {
  2. let testDatas = testDatas = [
  3. {'name': 'yy', 'password': 'helloqa'},
  4. {'name': 'age', 'password': 'helloqa2'}]
  5. // 循环生成测试用例
  6. for (const data in testDatas) {
  7. it(`测试外部数据${data}`, function () {
  8. cy.log(testDatas[data].name, testDatas[data].password)
  9. });
  10. }
  11. })

策略二:使用 fixtures(推荐)

直接看这篇文章就好了:https://www.yuque.com/gstorms/fo7n4g/odg4gg

策略三:数据保存在自定义文件中

  1. // 导入数据文件 example.json,并保存在 testData 变量中
  2. import testData from '../../data/example.json'
  3. describe('数据驱动的栗子', function () {
  4. describe('数据保存在自定义文件中', function () {
  5. for (const data in testData) {
  6. it(`测试外部数据${data}`, function () {
  7. cy.log(testData[data].name, testData[data].body)
  8. });
  9. }
  10. })
  11. })

测试结果

Cypress系列(64)- 数据驱动策略 - 图1

https://www.cnblogs.com/poloyy/p/13814787.html