前言 supertest 是一个短小精悍的接口测试工具,比如一个登录接口的测试用例如下: import request from 'supertest' it('登录成功', () => { request 今天就带大家一起实现一个 supertest 的轮子吧,做一个测试框架! 思路 在写代码前,先根据上面的经典例子设计好整个框架。 obj.del = obj.delete return obj } request.agent = TestAgent export default request 总结 至此,已经完美地实现了 supertest
local": "cross-env NODE_ENV=test_local jest --runInBand --passWithNoTests --colors --forceExit", }, supertest 安装 本地测试 jest + supertest 远程测试 jest + axios 和单元测试的关系 接口测试和单元测试,代码都放在 __test__ 目录下,但两者概念要区分开。 代码演示 安装 supertest、 axios package.json 添加 test:remote 命令 接口测试代码目录 __test__/apis/ 安装插件 npm i axios supertest = require('supertest') const { isTestRemote, isTestLocal } = require('../.. /src/utils/env') let request if (isTestLocal) { // 本地测试才使用 supertest 。
getName()); } publicstaticvoidmain(String[]args){ newSuperTest().test(); } } A SuperTest B SuperTest.class C test.SuperTest D test.SuperTest.class 正确答案是:C 解析: TestSuper和Date的getClass都没有重写,他们都是调用 这个运行时的类就是当前类,所以 super.getClass().getName() 返回的是test.SuperTest,与Date类无关 要返回Date类的名字需要写super.getClass()
示例 package com.superTest1; public class Fu { public void show() { System.out.println("Fu show()方法被调用"); } } package com.superTest1; public class Zi extends Fu{ public void method() { System.out.println("Zi method方法被调用"); } } package com.superTest1; public class Demo { 如果都没有就报错(不考虑父类的父类) 示例 package com.superTest2; public class Fu { int num = 10; } package com.superTest2 () { System.out.println("爸爸爱抽烟"); } } package com.superTest4; public class mother {
Mock数据的单元测试 这里我是结合Mocha(测试框架)、chai(断言)、supertest(模拟http测试)对Mock的API进行了一个单元测试,具体的如下: const app = require /server'); const supertest = require('supertest')(app); const expect = require('chai').expect; describe describe('#GET /', () => { it('should return a response with HTTP code 200', function(done) { supertest ('#GET /mock/js/user', () => { it('response data success should return true', (done) => { supertest
getName()); } publicstaticvoidmain(String[]args){ newSuperTest().test(); } } A SuperTest B SuperTest.class C test.SuperTest D test.SuperTest.class 不管你会不会!
beforeEach(function() { // 在本组每个测试用例开始前会执行 }) afterEach(function() { // 在本组每个测试用例结束后会执行 }) }) superTest 回到我们的文章主题上来,如果我们想要从请求开始来测试node服务接口返回的数据是否正常,也就是说进行一个整体性测试,那么 superTest 就是一个非常好的选择。 superTest可以帮助我们去请求本地 koa 或者 express这类web框架所编写的路由接口,而且对接口返回的状态码、数据等进行断言校验。 它本身不依赖任何测试框架,所以我们可以直接把它丢到mocha的测试用例中执行: const request = require('supertest'); const express = require mocha本身是一个比较简单的测试框架,在此基础上,我们使用一些npm包来加强我们的测试过程: nyc: 提供全面的测试覆盖率 chai: 多种风格的断言判断 sinon: 用于模拟或者替换难以测试的代码 superTest
传统上我们测试RESTful时,大部分人可能选择的mocha chai supertest 这个库作为测试框架 来编写API测试,通过上面的文章,我们了解到GraphQL 请求的底层依然还是http request --save-dev mocha chai ava 测试代码如下 import test from 'ava'; import { expect,should } from "chai"import supertest from 'supertest';const server = supertest.agent("http://localhost:3000/GraphQL");test('test grahphql }); 上面的代码看起来也挺简单,但是这不是我们最优方案,我们可以看到 request 的query 由于要遵循GraphQL 规范,并不是我们常见的json对象,我们需要寻找一种能够代替supertest
JavaScript可以使用Supertest配合Mocha或Jest应该不错,特别是Node.js环境。HttpClient是C#里的,需要提到.NET生态。 my_session /data Should Be Equal As Strings ${resp.status_code} 200JavaScript/Node.js 生态Supertest + Mocha/Jest特点: Supertest 简化 HTTP 请求,搭配 Mocha 或 Jest 做断言和测试管理。
还能生成全流程的命令:启动开发服务:npm run dev(同时启动前端react-scripts start和后端nodemon server);运行测试:npm run test(同时执行前端jest和后端supertest Express+MongoDB;开发规范:ESLint+Prettier、函数式组件、RESTful API、数据库表名下划线分隔;测试:Jest+React Testing Library(前端)+Supertest
Jest 和Mocha 单元测试为何难以落实 supertest接口测试 测试驱动开发TDD 5-2 介绍Jest和Mocha Jest官网:https://jestjs.io/zh-Hans/docs **现状:**研发流程不规范 5-4 supertest接口测试 supertest接口测试的目的是让所有接口稳起来。 本地测试: jest + supertest 远程测试: jest + axios 接口测试和单元测试,代码都放在 __test__下,但两者概念要区分开。 代码演示: 安装 supertest axios package.json中添加 test:remote配置(远程才用到) 接口测试目录:test/api/ 第六章:线上服务使用PM2和nginx
译者注:mock 和 stub 都是测试的方法 有个可以帮你进行Node.js REST API进行黑盒测试的模块叫做supertest。 一个简单的测试用例,其使用测试运行器mocha检查一个用户是否被返回,可以这么用: const request = require('supertest') describe('GET /user/:
void eat() { System.out.println("重写:人可以吃有营养的饭~~"); } } 测试类 package com.binbin.test02; public class SuperTest
我的选择是 ava / rewire / supertest / nyc。 supertest 可以做 API 级别的测试,也就是 functional testing,而 nyc 可以用来做 test coverage。 今天先讲这么多,下次谈谈如何架构一个 API 系统。
} }); await cloudBaseMiddleware({}, async () => {}); }); 4. http 与 static 服务测试 这方面很多人可能会用 supertest 在做调研的时候发现,jest 的下载量和更新记录远远高于 supertest,而且更纯粹。为什么这么说呢?它提供一种测试的组织形式,其它可以借助第三方库和工具实现。
使用了 Jest 和 Supertest 做测试。 使用了 Circle CI 进行持续集成/部署。 所有的数据存储在 MongoDB Atlas 3 节点的副本集集群中。
test.js 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 var supertest = require("supertest"); var should = require("should"); var server = supertest.agent("http://nodeapp-dev function(done){ (1).should.be.exactly(1).and.be.a.Number(); done(); }); }); 这是一个使用supertest dependencies": { "mocha": "^4.0.1", "mocha-junit-reporter": "^1.15.0", "should": "^13.1.3", "supertest
npm install --save-dev jest添加以下测试文件 app.test.js:const request = require('supertest');const app = require
/* eslint-env mocha */ const request = require('supertest') const server = require('.. /* eslint-env mocha */ const supertest = require('supertest') describe('movies-service', () => { const api = supertest('http://192.168.99.100:3000') it('returns a 200 for a collection of movies',
assert.throws 来测试普通函数和方法中的异常 使用 expect + rejects 来测试异步函数和异步方法中的异常 如果你对如何使用 Jest 测试 Koa 2 感兴趣,请查看使用Jest和Supertest