下面我有3个def测试(test1、test2和test3),我主要关注def test3,其中,我想检查一个用户是否不在数据库中,有人知道我该怎么做吗?你知道吗
代码:
'''
security service tests
'''
import sys
sys.path.append('..')
import requests
import json
from common.constants import *
from config.settings import environment
security_environment = environment()['security_service']
service_url = "%s://%s:%d" % (
security_environment['protocol'],
security_environment['host'],
security_environment['port'])
def test1():
print "TEST 1 - get user details"
headers = {'Accept': 'application/json'}
url = "%s/user/1" % service_url
response = requests.get(url, headers=headers)
status_code = response.status_code
print "STATUS: %s" % status_code
print "DATA: %s" % response.json()
return status_code == HTTP_OK
def test2():
print "TEST 2 - Count amount of Users"
headers = {'Accept': 'application/json'}
url = "%s/users" % service_url
response = requests.get(url, headers=headers)
status_code = response.status_code
data = response.json()
print (len(data))
if status_code != HTTP_OK:
return False
return len(data) == 5
def test3():
print "TEST 3 - Check if user is NOT in the Database"
headers = {'Accept': 'application/json'}
url = "%s/users" % service_url
response = requests.get(url, headers=headers)
status_code = response.status_code
users = response.json()
if users is None:
status_code != HTTP_OK
else:
status_code == HTTP_OK
if __name__ == "__main__":
num_pass = 0
num_fail = 0
for test in [test1, test2, test3]:
print "-----------------------------------------------------------"
if test():
num_pass += 1
print "PASS"
else:
num_fail += 1
print "FAIL"
print "==========================================================="
print "%d passed, %d failed" % (num_pass, num_fail)
请给我们你的答复样本。你知道吗
如果json数据如下所示:
试着这样做:
相关问题 更多 >
编程相关推荐