目录
前言
Seldom2.0是一个轻量级自动化测试框架,可以应用在UI自动化和接口自动化,相对于unittest+HtmlReport代码量少很多即可实现自动化测试
一、Seldom2.0
1.环境配置
安装最新版的seldom(seldom2.0),注意seldom2.0以下不支持HttpRequest方法:
pip install -U git+https://github.com/SeldomQA/seldom.git@master
pip show seldom #查看是否安装成功
seldom -v #查看seldom版本
2.seldom2.0与unittest+requests框架优势对比
先来看看unittest + requests 是如何来做接口自动化的:
import unittest
import requests
class TestAPI(unittest.TestCase):
def test_get_method(self):
payload = {
'key1': 'value1', 'key2': 'value2'}
r = requests.get("http://httpbin.org/get", params=payload)
self.assertEqual(r.status_code, 200)
if __name__ == '__main__':
unittest.main()
这其实,已经非常简洁,甚至我觉得这几行代码敲下来,比postman\JMeter之类的工具更加简单,效率更高。
同样的用例,用seldom实现。
import seldom
class TestAPI(seldom.HttpRequest):
def test_get_method(self):
payload = {
'key1': 'value1', 'key2': 'value2'}
self.get("http://httpbin.org/get", params=payload)
self.assertStatusCode(200)
if __name__ == '__main__':
seldom.run() #打开debug模式seldom.run(debug=True)
主要简化点在,接口的返回数据的处理。当然,seldom真正的优势在日志和报告。非debug模式下执行后会直接生成测试报告,如下图:
成功报告:
失败报告:
打开debug模式seldom.run(debug=True) 运行上面的用例。
python .\test_req_1.py
2021-03-24 00:54:30 [INFO] A run the test in debug mode without generating HTML report!
2021-03-24 00:54:30 [INFO]
_ _
| | | |
___ ___ | | __| | ___ _ __ ___
/ __| / _ \| | / _` | / _ \ | '_ ` _ \
\__ \| __/| || (_| || (_) || | | | | |
|___/ \___||_| \__,