接口测试,主要就是模拟发送请求,发送完之后拿到服务器响应的结果,对结果做一下对比就可以了。
下面不是写demo了,开始写项目了,首先创建一下包 cnodeapiTest-New-Python Package
比如名字命名为testcases,用来存放所有的测试用例
然后给项目新建一个文件 cnodeapiTest-New-File,可以把代码上传到 git 上,后面可以通过 git 查看
比如命名 readme.md,在 readme.md 里面写一下说明,相当于测试的说明书
比如在 readme.md 里面的备注如下
### 代码结构
- testcases 主要存放测试用例
处理好后,在建好的 testcases 包里面,新建一个Python文件,方法是 testcases-New-Python File
比如我的测试是主题相关,我可以命名为testtopics
如果是测试用例,建议前面都加个 test 作为他的标识
开始写,比如测cnode主题首页,多个英文单词可以用 _ 隔开,具体可以写为 如下
def test_topic_index_page
后面写个函数 def test_topic_index_page():
然后上面添加 import requests
下面添加 requests.get(‘http://49.233.108.117:3000/api/v1’)
因为在这个api文档里,所有的都以 http://49.233.108.117:3000/api/v1 为前缀所以可以把它声明在外面,把它作为一个变量,如下
base_url = ‘http://49.233.108.117:3000/api/v1’
所以主题首页就可以写为 requests.get(base_url+”/topics”)
还是按住command,点一下 get,回跳出来api
不管是get请求方式,还是post的请求方式,都是字典
然后写一下parmas,如下
query_parmas = {
“page”:1,
“tab”:”ask”,
“limit”:1,
“mdrender”:”false”
}
然后输入 如下
r = requests.get(base_url+”/topics”,params=query_parmas)
print(r.status_code)
print(r.json())
test_topic_index_page()
可以看到返回结果
所写代码如下
import requests
base_url = ‘http://49.233.108.117:3000/api/v1‘
def test_topic_index_page():
query_parmas = {
“page”:1,
“tab”:”ask”,
“limit”:1,
“mdrender”:”false”
}
r = requests.get(base_url+”/topics”,params=query_parmas)
print(r.status_code)
print(r.json())
test_topic_index_page()