test.py
import requests
import json
class RunMain:
def __init__(self,url,method,data=None):
self.res = self.run_main(url,method,data)
def send_post(self,url,data):
res = requests.post(url,data)
return res
def send_get(self,url,data):
res = requests.get(url,data).json()
return res
def run_main(self,url,method,data):
if method == 'GET':
res = self.send_get(url,data)
else:
res = self.send_post(url,data)
return res
unittest_res.py
import unittest
from test import RunMain
class TestMethod(unittest.TestCase):
@classmethod
def setUpClass(cls):
print("类执行之前的方法")
@classmethod
def tearDownClass(cls):
print("类执行之后的方法")
def setUp(self):
print('每次方法之前执行')
def tearDown(self):
print('每次方法之后执行')
def test_01(self):
url="https://www.apiopen.top/journalismApi"
run = RunMain(url,"GET")
self.assertEqual(run.res['code'],200,"测试失败")
def test_02(self):
url="https://www.apiopen.top/journalismApi"
run = RunMain(url,"GET")
self.assertEqual(run.res['code',20,"测试失败"])
if __name__ == '__main__':
unittest.main()