一.用例

TestCase 也就是测试用例

TestSuite 多个测试用例集合在一起,就是TestSuite

TestLoader是用来加载TestCase到TestSuite中的

TestRunner是来执行测试用例的,测试的结果会保存到TestResult实例中,包括运行了多少测试用例,成功了多少,失败了多少等信息

import unittest
def calc(a,b):
return a/b class TestCalc(unittest.TestCase):
  def test_calc1(self): #test开头才能运行,并且按照_后面的字母顺序执行
    '''测试正常的'''
# result = calc(2,1)
# self.assertEqual(2,result,'除法运算不正确') #不正确的时候出信息
  def setUp(self):#每条用例执行之前都会执行它
  print('我是setup')   def tearDown(self):#每条用例执行之后都会执行它
  print('我是teardown')
  @classmethod        #所有用例最开始之前
  def setUpClass(cls):
  print('我是setupClass')   @classmethod #所有用例结束后
  def tearDownClass(cls):
  print('我是tearDownClass')
#unittest.main() #运行当前文件里面的所有测试用例
二.发送报告
导入模块HTMLTestRunner,复制到external libraries
复制BeautifulReport
import HTMLTestRunner
import BeautifulReport as bf
#产生不好看的报告
# f = open('report.html','wb')
# runner = HTMLTestRunner.HTMLTestRunner(stream=f,
# description='测试报告里面的描述',
# title='接口测试报告')
# test_suite = unittest.makeSuite(TestCalc) #把这个类变成一个测试集合
# runner.run(test_suite)#运行用例 #产生好看的报告
test_suite = unittest.makeSuite(TestCalc) #把这个类变成一个测试集合
runner = bf.BeautifulReport(test_suite)
runner.report(filename='new_report.html',description='描述')
三.参数化
    pip install nose-parameterized
def login(username,password):
return 'ok' import unittest
from parameterized import parameterized
class TestLogin(unittest.TestCase):
@parameterized.expand([
['admin','1244','ok'],
['admin','1244','ok'],
['admin','1244','success'],
['admin','1244','success'],
])
def test_login1(self,username,password,hope):
'''登录'''
result = login(username,password)
self.assertEqual(hope,result)
# unittest.main()
四.查找测试用例,并生成报告
import unittest
import BeautifulReport
tests = unittest.defaultTestLoader.discover('case','*.py')
#去哪个目录下查找测试用例
runner = BeautifulReport.BeautifulReport(tests)
runner.report(description='查找测试用例',filename='all_report.html') 五.有关联关系
import unittest
import requests
import BeautifulReport
from urllib.parse import urljoin class TestChoice(unittest.TestCase):
base_url = 'http://api.nnzhp.cn/' def login(self):
uri = '/api/user/login'
url = urljoin(self.base_url,uri)
data = {'username':'niuhanyang','passwd':'aA123456'}
print('开始登录',data)
response = requests.post(url,data).json()
print('调用登录接口结束,返回结果',response)
self.assertIsNotNone(response.get('login_info'),'登录失败')
#校验非空
session_id = response['login_info']['sign']
user_id = response['login_info']['userId']
return session_id,user_id def choice(self,session_id,user_id):
uri = '/api/product/choice'
url = urljoin(self.base_url,uri)
data = {'userid':user_id,'sign':session_id}
print('抽奖接口开始=====',data)
response = requests.get(url,params=data).json()
print('抽奖接口结束=======',response)
self.assertIsNotNone(response.get('product_info'),'抽奖失败') def add_product(self,session_id,user_id):
uri = '/api/product/add'
url = urljoin(self.base_url,uri)
data = {'userid':user_id,'sign':session_id,'name':'iphoneXs max'}
files = {'file':open(r'/Users/nhy/Downloads/aaa.jpg','rb')}
response = requests.post(url,data,files=files).json()
self.assertEqual('添加成功',response.get('msg')) def test_login_choice(self):
'''测试抽奖流程的,先登录后抽奖'''
session_id,user_id = self.login()
self.choice(session_id,user_id) def test_login(self):
'''测试登录的'''
self.login() def test_add_product(self):
'''测试添加商品的'''
session_id,user_id = self.login()
self.add_product(session_id,user_id) # unittest.main()
runner = BeautifulReport.BeautifulReport(unittest.makeSuite(TestChoice))
runner.report('接口测试','interface.html')
 
04-27 05:02