需求背景:
当写完了很多用例后,只想批量执行部分测试用例应该怎么办?
解决方案:
用excel组织测试用例,比如在想执行的测试用例的“是否执行”填入1为执行,填入0则不执行,如下excel表(表名config.xls):
| 测试用例 | 测试用例中文名称 | 对应的数据表 | 开发者 | 是否执行 |
| test_zhaolei | 搜索赵雷 | null | 黄佩勤 | 1 |
| test_zhoujielun | 搜索周杰伦 | null | 黄佩勤 | 1 |
| test_wangsicong | 搜索王思聪 | null | 黄佩勤 | 1 |
| test_login_success | 登录成功 | null | 黄佩勤 | 1 |
| test_login_fail01 | id定位错误,登录失败 | null | 黄佩勤 | 0 |
| test_login_fail02 | 用户名输入错误,登录失败 | null | 黄佩勤 | 0 |
注意这里的测试用例(第一列)必须为全局唯一。
首先新建一个读取excel的py文件 excel_oprate.py 代码如下:
import xlrd
import os
def read_config():
root_dir = os.path.dirname(os.path.dirname(__file__).replace("\\", "/"))
excel_dir = root_dir + '/config.xls'
excel = xlrd.open_workbook(excel_dir)
table = excel.sheet_by_index(0)
rows = table.nrows
table_data = []
title = table.row_values(0)
for i in range(1, rows):
rows_value = table.row_values(i)
dic = dict(zip(title, rows_value))
table_data.append(dic)
return table_data
新建一个excute_cases.py文件(批量执行入口),代码如下(重点):
import unittest
import os, datetime
from BeautifulReport import BeautifulReport
from framework.excel_oprate import read_config
root_dir = os.path.dirname(os.path.abspath(__file__)).replace('\\', '/')
test_dir = root_dir + '/testcases'
report_dir = root_dir + '/test_report'
discover = unittest.defaultTestLoader.discover(test_dir, 'test*.py', None)
testsuit01 = unittest.TestSuite()
testsuit02 = unittest.TestSuite()
table_datas = read_config()
for test01 in discover:
for test02 in test01:
for test03 in test02:
testsuit01.addTest(test03)
for case in testsuit01:
case01 = str(case)
case01 = case01.split('(')[0].strip()
for i in range(len(table_datas)):
case_name = table_datas[i]['测试用例']
if case_name == case01:
if table_datas[i]['是否执行'] == float(1.0):
testsuit02.addTest(case)
print('----->要运行的测试用例:')
for te in testsuit02:
print(te)
now = datetime.datetime.now().strftime('%Y-%m-%d %H_%M_%S')
filename = '测试报告' + str(now)
BeautifulReport(testsuit02).report(description='测试报告', filename=filename, log_path=report_dir)
我新建了2个py文件作为测试用例文件
test_login.py的代码如下:
import unittest, time, os
from selenium import webdriver
from BeautifulReport import BeautifulReport
class test_login(unittest.TestCase):
def save_img(self, test_method):
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))).replace('\\', '/')
img_path = root_dir + '/img'
self.driver.get_screenshot_as_file('{}/{}.png'.format(img_path, test_method))
def setUp(self):
self.driver = webdriver.Chrome()
@BeautifulReport.add_test_img('test_login_success')
def test_login_success(self):
'''登录成功'''
self.driver.get('https://erp-test.youkeshu.com/')
self.driver.find_element_by_id('userName').send_keys('huangpeiqin')
self.driver.find_element_by_id('pwd').send_keys('hYKS11234')
self.driver.find_element_by_xpath('//*[@id="root"]/div/div/form/div[3]/div/div/span/button').click()
time.sleep(1)
url = self.driver.current_url
self.assertEqual(url, 'https://erp-test.youkeshu.com/index/console/myupcoming/')
@BeautifulReport.add_test_img('test_zhoujielun')
def test_login_fail01(self):
'''id定位错误,登录失败'''
self.driver.get('https://erp-test.youkeshu.com/')
self.driver.find_element_by_id('userName1').send_keys('huangpeiqin')
self.driver.find_element_by_id('pwd').send_keys('hYKS11234')
self.driver.find_element_by_xpath('//*[@id="root"]/div/div/form/div[3]/div/div/span/button').click()
time.sleep(1)
url = self.driver.current_url
self.assertEqual(url, 'https://erp-test.youkeshu.com/index/console/myupcoming/')
@BeautifulReport.add_test_img('test_login_fail02')
def test_login_fail02(self):
'''用户名输入错误,登录失败'''
self.driver.get('https://erp-test.youkeshu.com/')
self.driver.find_element_by_id('userName').send_keys('huangpeiqin1')
self.driver.find_element_by_id('pwd').send_keys('hYKS1234')
self.driver.find_element_by_xpath('//*[@id="root"]/div/div/form/div[3]/div/div/span/button').click()
time.sleep(1)
url = self.driver.current_url
self.assertEqual(url, 'https://erp-test.youkeshu.com/index/console/myupcoming/')
def tearDown(self):
self.driver.close()
test_baidu.py文件代码如下:
import unittest, time,os
from selenium import webdriver
from BeautifulReport import BeautifulReport
class test_baidu(unittest.TestCase):
def save_img(self, test_method):
root_dir = os.path.dirname(os.path.dirname(os.path.abspath(__file__))).replace('\\','/')
img_path = root_dir + '/img'
self.driver.get_screenshot_as_file\
('{}/{}.png'.format(img_path, test_method))
def setUp(self):
self.driver = webdriver.Chrome()
@BeautifulReport.add_test_img('test_zhaolei')
def test_zhaolei(self):
'''搜索赵雷'''
self.driver.get('https://www.baidu.com')
self.driver.find_element_by_id('kw').send_keys('赵雷')
self.driver.find_element_by_id('su').click()
time.sleep(5)
@BeautifulReport.add_test_img('test_zhoujielun')
def test_zhoujielun(self):
'''搜索周杰伦'''
self.driver.get('https://www.baidu.com')
self.driver.find_element_by_id('kwk').send_keys('周杰伦')
self.driver.find_element_by_id('su').click()
time.sleep(5)
@BeautifulReport.add_test_img('test_wangsicong')
def test_wangsicong(self):
'''搜索王思聪'''
self.driver.get('https://www.baidu.com')
self.driver.find_element_by_id('kw').send_keys('王思聪')
self.driver.find_element_by_id('su').click()
time.sleep(5)
def tearDown(self):
self.driver.close()
执行excute_cases.py文件,后生成 BeautifuReport报告如下,失败的用例有失败截图及错误详细信息。