这篇文章主要为大家详细介绍了pytest批量测试的简单示例,具有一定的参考价值,可以用来参考一下。
感兴趣的小伙伴,下面一起跟随四海网的小编罗X来看看吧。
# 来自www.q1010.com
$ pip install -U pytest
or
$ easy_install -U pytest
$ py.test --version
# 来自www.q1010.com
#!/usr/bin/python
import pytest
def func(x):
return x + 1
def test_answer(): #测试用例,方法需要以test_开头
assert func(3) == 5
# 来自www.q1010.com
$ py.test 2.py #注意需要用py.test命令来运行之
# 来自www.q1010.com
#!/usr/bin/python
import pytest
class TestClass:
def test_one(self):
x = "this"
assert "h" in x
def test_two(self):
x = 3
assert x > 2
# 来自www.q1010.com
{
"message":"success",
"result":"success",
"start":"2017-02-28 13:54:53",
"data":{
"Memory":{
"172.20.116.72":{
"swap_used":["9.60%"],
"datetime":["2017-02-28 13:54:41"],
"merge_time":["2017-02-28 13:54:41"],
"ram_used":["25.52%"]
},
"172.20.116.70":{
"swap_used":["6.17%"],
"datetime":["2017-02-28 13:54:41"],
"merge_time":["2017-02-28 13:54:41"],
"ram_used":["25.97%"]
}
},
"CPU":{
"172.20.116.72":{
"datetime":["2017-02-28 13:54:41"],
"merge_time":["2017-02-28 13:54:41"],
"cpu_prct_used":["3.00%"]
},
"172.20.116.70":{
"datetime":["2017-02-28 13:54:41"],
"merge_time":["2017-02-28 13:54:41"],
"cpu_prct_used":["1.00%"]
}
},
"Disk":{
"172.20.116.72":{
"datetime":["2017-02-28 13:54:41"],
"merge_time":["2017-02-28 13:54:41"],
"/export":["25.06%"],
"/":["21.6%"]
},
"172.20.116.70":{
"datetime":["2017-02-28 13:54:41"],
"merge_time":["2017-02-28 13:54:41"],
"/export":["44.68%"],
"/":["36.15%"]
}
}
},
"host_size":2,
"end":"2017-02-28 13:54:53"
}
# 来自www.q1010.com
#!/usr/bin/python
import os
import sys
import json
import urllib
import urllib2
import pytest
iplist = ["172.20.116.70", "172.20.116.72"] #定义IP列表
ips = ','.join(iplist)
url = 'http://api/latestMeteris?userCode=xxx&token=xxx&host=' + ips + '&service=CPU,Memory,Disk'
req = urllib.urlopen(url)
result = req.read() #get a string type
a = json.loads(result) #transfer string type to dict type
@pytest.mark.parametrize('ip', iplist)
def test_cpu(ip):
value = a["data"]["CPU"][ip]["cpu_prct_used"][0]
assert float(value.strip("%")) < 80
@pytest.mark.parametrize('ip', iplist)
def test_memory(ip):
value = a["data"]["Memory"][ip]["ram_used"][0]
assert float(value.strip("%")) < 95
@pytest.mark.parametrize('ip', iplist)
def test_disk(ip):
value_root = a["data"]["Disk"][ip]['/'][0]
value_export = a["data"]["Disk"][ip]['/export'][0]
assert float(value_root.strip("%")) < 80 and float(value_export.strip("%")) < 80
# 来自www.q1010.com
$ py.test 2.py
========================= test session starts =========================
platform linux2 -- Python 2.7.4, pytest-3.0.6, py-1.4.31, pluggy-0.4.0
rootdir: /home/zhukun/0224, inifile:
collected 6 items
2.py ......
====================== 6 passed in 0.05 seconds ======================
本文来自:http://www.q1010.com/181/2149-0.html
注:关于pytest批量测试的简单示例的内容就先介绍到这里,更多相关文章的可以留意四海网的其他信息。
关键词:测试
四海网收集整理一些常用的php代码,JS代码,数据库mysql等技术文章。