Skip to content
0

Test

测试相关的工具

stepci ---> API 测试框架

通过 yaml 文件的格式来定义 API 测试用例,使用 CI 工具来运行测试用例。支持测试 HTTP、GraphQL、tRPC、SOAP 等接口。

一个 workflow.yml 文件的示例:

version: "1.1"
name: Status Check
env:
  host: example.com
tests:
  example:
    steps:
      - name: GET request
        http:
          url: https://${{env.host}}
          method: GET
          check:
            status: /^20/

测试用例的执行结果:

$ npm install -g stepci
$ stepci run workflow.yml
 PASS  example ⏲ 0.746s  147 bytes  1056 bytes

Tests: 0 failed, 1 passed, 1 total
Steps: 0 failed, 0 skipped, 1 passed, 1 total
Time:  0.748s, estimated 1s
CO2:   0.00045g

Workflow passed after 0.748s
Give us your feedback on https://step.ci/feedback

mitata ---> 基准工具

易于使用的 JS 测试工具,示例代码:

import { bench, run } from 'mitata'

// 准备测试数据
const size = 50000
const data = Array.from({ length: size }, () => Math.floor(Math.random() * size))

// 1. 普通冒泡排序
bench('Bubble Sort', () => {
  const arr = [...data]
  for (let i = 0; i < arr.length; i++) {
    for (let j = 0; j < arr.length - i - 1; j++) {
      if (arr[j] > arr[j + 1])
        [arr[j], arr[j + 1]] = [arr[j + 1], arr[j]]
    }
  }
  return arr
})

// 2. 原生排序(快排实现)
bench('Native Sort', () => {
  const arr = [...data]
  return arr.sort((a, b) => a - b)
})

await run()

输出的结果:

clk: ~3.32 GHz
cpu: Apple M2
runtime: node 18.18.2 (arm64-darwin)

benchmark                   avg (min  max) p75 / p99    (min top 1%)
------------------------------------------- -------------------------------
Bubble Sort                     1.33 s/iter    1.34 s             
                          (1.30 s 1.40 s)    1.36 s  ▅▅█   ▅▅
                    (234.99 kb 366.81 kb) 247.51 kb █▁█▁▁███▁█▁▁▁██▁▁▁▁▁█

Native Sort                    4.39 ms/iter   4.43 ms  ▆█                  
                        (4.31 ms 4.71 ms)   4.63 ms  ████ ▃▇             
                    (749.15 kb 853.35 kb) 777.94 kb █████▇██▇▇▅▅▄▂▁▁▂▃▂▂▂

TDD ---> 一些测试框架

~> "ava"   took   594ms  (  ???  )
~> "jest"  took   962ms  (356  ms)
~> "mocha" took   209ms  (  4  ms)
~> "tape"  took   122ms  (  ???  )
~> "uvu"   took    72ms  (  1.3ms)

sinon ---> 函数模拟和代理

  • Spies,提供了函数调用的信息,但不会改变其行为(译者注:类似动态代理)
  • Stubs,类似Spies,但是是完全替换目标函数。这可以让你随心所欲的控制函数–抛异常,返回指定结果等
  • Mocks,提供了替换整个对象的能力
function myFunction(condition, callback){
  if(condition){
    callback();
  }
}

describe('myFunction', function() {
  it('should call the callback function', function() {
    var callback = sinon.spy();

    myFunction(true, callback);

    assert(callback.calledOnce);
  });
});

Released under the MIT License.