迪士尼都在用这个测试工具,请先从用例编写学起

本贴最后更新于 1116 天前,其中的信息可能已经沧海桑田

在上一篇文章《web 自动化测试,一定得掌握的 8 个核心知识点》末尾,我们知道了 Cypress 这个新一代流行的自动化框架就是使用了 mocha 来进行测试。这篇文章,一起来学习下 mocha 的一些特性吧。

Mocha 一个最简单的例子是这样的。 它使用 describe 来描述测试场景,使用 it 来表示测试用例, 断言可以使用内置,也可以使用一些第三方的断言库。

// test/mocha.spec.js
const assert = require('assert');

// the function to be tested
function pow(a, b) {return a ** b};

// tests
describe("pow", () => {
    it('pow of 2 and 3', () => {
        let actual = pow(2, 3);
        assert.equal(actual, 8);
    })
  
    it('pow of string and 3', () => {
        let actual = pow("a", 3);
        assert.equal(actual, 8);
    })
})

实际上,mocha 官方不推荐用箭头函数(匿名函数)来表示测试过程,而是使用普通的 function 函数表达式,因为箭头函数不能通过 this 访问 mocha 的上下文。

使用箭头函数在不用 this 获取 mocha 上下文时不会有问题,但是一旦需要 this 访问上下文,箭头函数会报错。

// 错误写法
const assert = require('assert');

describe('my suite', () => {
    it('my test', () => {
        // should set the timeout of this test to 1000 ms; 
        // instead will fail
        this.timeout(1000)
        assert.ok(true);
    });
});

此时必须使用 function 函数表达式:

// 正确写法
const assert = require('assert');

describe('my suite', function() {
    it('my test', function() {
        this.timeout(1000)
        assert.ok(true);
    });
});

5.1、hook

Mocha 提供了 before、after、beforeEach、afterEach 执行前置条件和后置清理工作。

  1. before 在这个描述的开始执行一次
  2. after 在这个描述的结束执行一次
  3. beforeEach 在每个用例之前都执行一次
  4. afterEach 在每个用例之后都执行一次
const assert = require('assert');

describe('hooks', function() {
    before(function() {
      // runs once before the first test in this block
      console.log("before")
    });
  
    after(function() {
      // runs once after the last test in this block
      console.log("after")
    });
  
    beforeEach(function() {
      // runs before each test in this block
      console.log("before each")
    });
  
    afterEach(function() {
      // runs after each test in this block
      console.log("after each")
    });
  
    // test cases
    it('pow of 2 and 3', () => {
        assert(true)
    });
  
    it('pow of string and 3', () => {
        assert(true)
    });
  
 });

Hook 也可以加说明,解释需要做什么事情:

beforeEach('some description', function() {
  // beforeEach:some description
});

5.2、运行指定用例

在 it 用例后添加 only 函数就可以指定用例,标记了 only 的用例会运行,没有标记的不会运行。

describe('#indexOf()', function() {
    it.only('should return -1 unless present', function() {
      // this test will be run
    });

    it.only('should return the index when present', function() {
      // this test will also be run
    });

    it('should return -1 if called with a non-Array context', function() {
      // this test will not be run
    });
});

不仅可以在 it 用例上使用 only,而且可以在嵌套的 describe 中使用:

describe('Array', function() {
  describe.only('#indexOf()', function() {
    it('should return -1 unless present', function() {
      // this test will be run
    });

    it('should return the index when present', function() {
      // this test will also be run
    });
  });

  describe.only('#concat()', function() {
    it('should return a new Array', function() {
      // this test will also be run
    });
  });

  describe('#slice()', function() {
    it('should return a new Array', function() {
      // this test will not be run
    });
  });
});

5.3、跳过用例

在 mocha 中,可以通过 only 指定需要执行的用例,也可以通过 skip 跳过用例,用法和 only 基本一致,你可以通过 it.skip 跳过一个用例,也可以通过 describe.skip 跳过整个描述。

除此之外,你还可以根据环境变量的值判断,当满足条件时,再跳过用例。

it('should only test in the correct environment', function() {
  if (Cypress.env('host') === 'local') {
    // make assertions
  } else {
    this.skip();
  }
});

你甚至可以在 describe 或者 before 函数中,跳过整个块中的所有用例:

before(function() {
  if (Cypress.env('host') === 'local') {
    // setup code
  } else {
    this.skip();
  }
});

5.4、重复执行

在一些脆弱的测试中,一次执行可能并不能成功执行用例。在web测试过程中,因为网络环境和其他因素影响,会造成用例执行不稳定,此时你需要对失败用例重复执行。在 mocha 当中,你可以通过 this.retries 在 describe 和 it 中设置失败执行次数,你也可以在 before函数中设置,但是在 beforeEach 是不可以的。

describe('retries', function() {
  // Retry all tests in this suite up to 4 times
  this.retries(4);

  beforeEach(function() {
    browser.get('http://www.yahoo.com');
  });

  it('should succeed on the 3rd try', function() {
    // Specify this test to only retry up to 2 times
    this.retries(2);
    expect($('.foo').isDisplayed()).to.eventually.be.true;
  });
});

5.5 动态生成用例

你已经有了 3 组测试数据,这 3 组数据的测试步骤都是一样的,那就没有必要重复去编写 3 个测试函数,没错,用数据驱动,根据 3 组数据动态生成 3 个用例。

const assert = require('chai').assert;

function add(args) {
  return args.reduce((prev, curr) => prev + curr, 0);
}

describe('add()', function() {
  const tests = [
    {args: [1, 2], expected: 3},
    {args: [1, 2, 3], expected: 6},
    {args: [1, 2, 3, 4], expected: 10}
  ];

  tests.forEach(({args, expected}) => {
    it(`correctly adds ${args.length} args`, function() {
      const res = add(args);
      assert.equal(res, expected);
    });
  });
});

总结

在使用 mocha 时,有一些特性会经常使用,尤其是在具体的实战项目中:

  1. 尽量使用普通的函数声明,避免使用箭头函数,因为箭头函数无法通过 this 获取 mocha 上下文。
  2. 可以通过 hook 函数设置前置条件和后置清理工作。
  3. 通过 only 指定用例运行。
  4. 通过 skip 跳过用例运行。
  5. 通过 retries 设置重复运行次数。
  6. 使用数据驱动测试思想动态生成用例。
回帖
请输入回帖内容 ...