菜单
文档面包屑箭头 Grafana k6面包屑箭头 JavaScript API面包屑箭头 jslib面包屑箭头 k6chaijs面包屑箭头 describe( name, function )
开源

describe( name, function )

describegroup 的一个包装器,增加了以下能力:

  • 捕获异常 以便在 describe 函数外部继续执行。
  • 返回一个布尔值,指示其所有 k6chaijs 断言是否成功。
JavaScript
import { describe, expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.3/index.js';

export default function testSuite() {
  const success1 = describe('Basic test', () => {
    expect(1, 'number one').to.equal(1);
  });
  console.log(success1); // true

  const success2 = describe('Another test', () => {
    throw 'Something entirely unexpected happened';
  });
  console.log(success2); // false

  const success3 = describe('Yet another test', () => {
    expect(false, 'my vaule').to.be.true();
  });
  console.log(success3); // false
}
bash
default ✓ [======================================] 1 VUs  00m00.0s/10m0s  1/1 iters, 1 per VU

     █ Basic test
       ✓ expected number one to equal 1

     █ Another test
       ✗ Exception raised "Something entirely unexpected happened"
        ↳  0% — ✓ 0 / ✗ 1

     █ Yet another test
       ✗ expected my vaule to be true
        ↳  0% — ✓ 0 / ✗ 1

API

参数类型描述
namestring测试用例名称。测试用例名称应唯一,否则测试用例将被分组。
functionfunction要执行的测试用例函数

返回值

类型描述
bool当 describe() 主体内的所有 expect 条件都成功,且没有引发未处理的异常时,返回 true,否则返回 false。

链式调用 describe() 块

如果您想跳过后续 describe 块的执行,请考虑使用 && 将它们链接起来,如下所示。

JavaScript
import { describe, expect } from 'https://jslib.k6.io/k6chaijs/4.3.4.3/index.js';

export default function testSuite() {
  describe('Basic test', () => {
    expect(1, 'number one').to.equal(1);
  }) &&
    describe('Another test', () => {
      throw 'Something entirely unexpected happened';
    }) &&
    describe('Yet another test', () => {
      // the will not be executed because the prior block returned `false`
      expect(false, 'my vaule').to.be.true();
    });
}