Skip to content

Commit

Permalink
20200716
Browse files Browse the repository at this point in the history
  • Loading branch information
19950219 committed Jul 16, 2020
1 parent c6ada28 commit 15a5dc3
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 26 deletions.
12 changes: 5 additions & 7 deletions common/base_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
base_name = conf.get('database','base_name')


class SQL():
class SQL(object):
def __init__(self):
self.connection = pymysql.connect(
host=host,
Expand All @@ -33,16 +33,15 @@ def creat_connect(self):
def close_base(self):
self.connection.close()

def select_one(self,table_name='testcase',field_name='case_id',value_name='1'):
def select_one(self, table_name='testcase',field_name='case_id',value_name='1'):
cursors = self.creat_connect()
cursors.execute('select * from %s where %s = "%s"' % (table_name,field_name,value_name))
result = cursors.fetchall()
print(result)
cursors.close()
self.close_base()
return result

def select_all(self,table_name='testcase'):
def select_all(self, table_name='testcase'):
cursors = self.creat_connect()
cursors.execute('select * from %s' % (table_name))
all_result = cursors.fetchall()
Expand All @@ -52,6 +51,5 @@ def select_all(self,table_name='testcase'):


if __name__ == '__main__':

SQL().select_one(field_name='url',value_name='csdn.net')
print(SQL().select_all())
print(SQL().select_one(field_name='url',value_name='/api/user/stu_info'))
print(SQL().select_all()[2])
3 changes: 2 additions & 1 deletion common/database.sql
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
CREATE TABLE `case` (
CREATE TABLE `testcase` (
`case_id` int(255) NOT NULL,
`case_num` varchar(255) NOT NULL,
`title` varchar(255) NOT NULL,
`url` varchar(255) NOT NULL,
`method` varchar(255) NOT NULL,
Expand Down
19 changes: 7 additions & 12 deletions common/requests_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,34 @@
import configparser
import os


config_path = os.getcwd()
file_path = os.path.dirname(config_path)

config_file = os.path.dirname(os.path.dirname(os.path.abspath(__file__))) + '\\configfile.ini'

conf = configparser.ConfigParser()
conf.read(config_file, encoding='utf-8')
url_head = conf.get('requests_setting', 'url_head')


def auto_get(url=None, params=None, headers=None, allow_redirects=True):
all_url = str(url_head) + str(url)
print(all_url)
get_response = requests.get(url=all_url, headers=headers, params=params, allow_redirects=allow_redirects)
return get_response.url, get_response.text
return get_response.text


def auto_post(url=None, data=None, headers=None, allow_redirects=True):
all_url = str(url_head) + str(url)
post_response = requests.post(url=all_url, headers=headers, data=data, allow_redirects=allow_redirects)
return post_response.status_code, post_response.json()

return post_response.json()


if __name__ == '__main__':
a = auto_get(url='/api/user/stu_info', params='stu_name=黑黑', headers='', allow_redirects=False)
print(a[0])
print(type(a[1]))
print(a)
print(type(eval(a)))
data = {'username': 'niuhanyang', 'passwd': 'aA123456'}
headers = {'Accept': '*/*'}
# b = request_method().auto_post(url='/api/user/login',data=data,headers=headers,allow_redirects=False)
# print(b[0])
# print(type(b[1]))
b = auto_post(url='/api/user/login',data=data,headers=headers,allow_redirects=False)
print(b)
print(type(b))


17 changes: 11 additions & 6 deletions testcase/test_api.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import unittest,time
from common.requests_api import auto_get, auto_post
from common.base_api import SQL


class TestBaidu(unittest.TestCase):
Expand All @@ -12,19 +13,23 @@ def tearDownClass(cls):
pass

def test_001(self):
url = '/api/user/stu_info'
select_one = SQL().select_one()
url = select_one[0]['url']

params = {'stu_name': '黑黑'}
response = auto_get(url=url, params=params)
print(response[1])
time.sleep(5)
print(response)

print(type(url))
print(select_one)
# time.sleep(5)

def test_002(self):
url = '/api/user/login'
data = {'username': 'niuhanyang', 'passwd': 'aA123456'}
t = auto_post(url=url, data=data)
print(t[1])
time.sleep(5)

print(t)
# time.sleep(5)

def test_003(self):
unittest.TestCase.fail()
Expand Down

0 comments on commit 15a5dc3

Please sign in to comment.