概述
# 来自jb51.cc
$ pip install -U pytest
or
$ easy_install -U pytest
$ py.test --version
# 来自jb51.cc
#!/usr/bin/python
import pytest
def func(x):
return x + 1
def test_answer(): #测试用例,方法需要以test_开头
assert func(3) == 5
# 来自jb51.cc
$ py.test 2.py #注意需要用py.test命令来运行之
# 来自jb51.cc
#!/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
# 来自jb51.cc
{
"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%"],"ram_used":["25.97%"]
}
},"cpu":{
"172.20.116.72":{
"datetime":["2017-02-28 13:54:41"],"cpu_prct_used":["3.00%"]
},"172.20.116.70":{
"datetime":["2017-02-28 13:54:41"],"cpu_prct_used":["1.00%"]
}
},"Disk":{
"172.20.116.72":{
"datetime":["2017-02-28 13:54:41"],"/export":["25.06%"],"/":["21.6%"]
},"/export":["44.68%"],"/":["36.15%"]
}
}
},"host_size":2,"end":"2017-02-28 13:54:53"
}
# 来自jb51.cc
#!/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,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
# 来自jb51.cc
$ 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 ======================
总结
以上是编程之家为你收集整理的pytest批量测试的简单示例全部内容,希望文章能够帮你解决pytest批量测试的简单示例所遇到的程序开发问题。
如果您也喜欢它,动动您的小指点个赞吧