一、搭建mock server

mock工具很多,这里我们选择用简单易操作的moco服务器

使用前必须先安装java,去相关网站下载Standalone Moco Runner

二、使用mock server

从最简单的hello world开始,使用json做为交互数据使用,编辑config.json为如下文件:

[
{
"request" :
{
"uri" : "/hello"
},
"response" :
{
"text" : "World!"
}
}
]

启动mock server

java -jar moco-runner-0.10.0-standalone.jar  http -p 12888 -c config.json

三:验证

使用httpRequest相关发送get /hello,就会返回world

四:丰富接口后使用unittest、requests写的接口用例如下,包括了get、post、put和deletet接口,代码如下,有需要的可以参考:

import unittest
import requests
import json class TestInterFace(unittest.TestCase): def setUp(self): #print "xxxx"
self.domain='http://localhost:12888'
self.json_headers={"content-type":'application/json'}
print "before each test" def tearDown(self):
print "End One Testcase" def test_get_all_posts(self):
print "test get all posts"
result=requests.get(self.url_test("/posts")).json()
self.assertEqual(len(result),3)
print len(result)
self.assertEqual(result[0]['title'],'first post')
#print 'xxx'
self.assertEqual(result[0]['url'],'/posts/1') self.assertEqual(result[-1]['title'],'how to learn interface test')
self.assertEqual(result[-1]['url'],'/posts/3') def test_get_first_post(self):
print "test get first post"
result=requests.get(self.url_test("/posts/1")).json()
self.assertEqual(len(result),2)
self.assertEqual(result['title'],'first post')
self.assertEqual(result['content'],'this is my first post') def url_test(self,path):
return self.domain + path def test_create_post(self):
#headers={"content-type":'application/json'}
json_data=json.dumps({"title": "new post","content": "new post"})
result=requests.post(self.url_test('/posts'),data=json_data,headers=self.json_headers)
print result.status_code
print result.text
self.assertEqual(result.status_code,200)
self.assertEqual(result['sucess'],'true') def test_modify_post(self):
headers={"content-type":'application/json'}
json_data=json.dumps({"title": "new post","content": "new post"})
result=requests.put(self.url_test('/posts/1'),data=json_data,headers=self.json_headers)
print result.status_code
print result.text
self.assertEqual(result.status_code,200)
#self.assertTrue def test_delete_post(self):
headers={"content-type":'application/json'}
json_data=json.dumps({"title": "new post","content": "new post"})
result=requests.delete(self.url_test('/posts/2'))
print result.status_code
print result.text
self.assertEqual(result.status_code,200)
#self.assertTrue if __name__=='__main__':
unittest.main()

mock server搭建及接口测试简单实例的更多相关文章

  1. (Hibernate进阶)Hibernate搭建开发环境+简单实例(二)

    hibernate是非常典型的持久层框架,持久化的思想是非常值得我们学习和研究的.这篇博文,我们主要以实例的形式学习Hibernate,不深究Hibernate的思想和原理,否则,一味追求,苦学思想和 ...

  2. 【SSH进阶之路】Hibernate搭建开发环境+简单实例(二)

    Hibernate是很典型的持久层框架,持久化的思想是很值得我们学习和研究的.这篇博文,我们主要以实例的形式学习Hibernate,不深究Hibernate的思想和原理,否则,一味追求,苦学思想和原理 ...

  3. vue mock(模拟后台数据) +axios 简单实例(二)

    需装上axios,build文件夹中webpack.dev.conf.js文件添加上vue mock配置的东东,  如,继(一) //组件<template> <div> &l ...

  4. vue mock(模拟后台数据) 最简单实例(一)——适合小白

    开发是前后端分离,不需要等待后台开发.前端自己模拟数据,经本人测试成功. 我们在根目录新建存放数据的json文件,存放我们的数据data.json //data.json{ "status& ...

  5. 基于moco的mock server 简单应用 来玩玩吧

    提起mock大家应该就知道是干嘛用的了,再次再介绍一种简单的方式,基于moco的mock server.步骤很简单: 1. 首先,要下载个moco的jar0_1482402640757_moco-ru ...

  6. MySQL搭建系列之多实例

    所谓多实例.就是在一台server上搭建.执行多个MySQL实例,每一个实例使用不同的服务port.通过不同的socket监听:物理上,每一个实例拥有独立的參数配置文件及数据库. 通常情况下.一台se ...

  7. Fiddler-009-AutoResponder 简单的 MOCK SERVER 应用实例

    在我们日常的测试中经常需要测试特定的响应对应的客户端展示样式是否正确无误,实现测试方法一般有如下三种: 创建新的测试数据(工作量较大) 修改已有测试数据(例如修改对应的状态码,若是最终需要测试的按钮状 ...

  8. 搭建Mock Server

    1.为什么要搭建mock-server? 为了更好的分工合作,让前端能在不依赖后端环境的情况下进行开发,其中一种手段就是为前端开发者提供一个 web 容器,这个本地环境就是 mock-server. ...

  9. Python接口测试实战5(下) - RESTful、Web Service及Mock Server

    如有任何学习问题,可以添加作者微信:lockingfree 课程目录 Python接口测试实战1(上)- 接口测试理论 Python接口测试实战1(下)- 接口测试工具的使用 Python接口测试实战 ...

随机推荐

  1. go语言之PLAN9汇编

    http://blog.studygolang.com/2013/05/asm_and_plan9_asm/ https://lrita.github.io/2017/12/12/golang-asm ...

  2. Python 常用类库

    python除了关键字(keywords)和内置的类型和函数(builtins),更多的功能是通过libraries(即modules)来提供的. 常用的libraries(modules)如下: 1 ...

  3. [Unity3D] 01 - Try Unity3D

    01. Move and Rotate 标准全局坐标系 Keyboard using UnityEngine; using System.Collections; public class NewBe ...

  4. Eclipse------用Tomcat运行项目后出现:严重: Error configuring application listener of class org.springframework.web.context.ContextLoaderListener

    Eclipse中Tomcat运行项目后出现: 严重: Error configuring application listener of class org.springframework.web.c ...

  5. 为什么要使用JS模板引擎

    我之前在写一个输入联想控件的时候,改过好几个版本,每个版本不是因为性能不好就是因为代码凌乱而被推翻,最后用了understore模板引擎,效果有明显改善.整好这两天在研究互联网技术架构,发现很多的开发 ...

  6. 奔五的人学ios:swift竟然没有字符串包括,找个简单的解决方法

    swift关于字符串的推断中 有前导.有后缀 两个方法.竟然没有包括推断. 经过学习找了个简单的解决方法: extension String { func has(v:String)->Bool ...

  7. ios8 UITableView设置 setSeparatorInset:UIEdgeInsetsZero不起作用的解决办法

    在ios7中,UITableViewCell左侧会有默认15像素的空白.这时候,设置setSeparatorInset:UIEdgeInsetsZero 能将空白去掉. 但是在ios8中,设置setS ...

  8. 新版本的body-parser中间件和morgan中间件引用问题:body-parser deprecated bodyParser和morgan deprecated morgan(options)

    引用新版本的body-parser中间件和morgan中间件时,报如下问题: Fri, 09 Jan 2015 06:32:04 GMT morgan deprecated morgan(option ...

  9. Nginx 代理

    如下,配置 Nginx 成为一台代理服务器 [root@localhost ~]$ cat /usr/local/nginx/conf/vhost/proxy.conf server { listen ...

  10. Steam安装Google Earth VR

    打开Steam 打开火狐浏览器 输入steam://install/348250