在批量执行测试用例时,为了提高执行效率,多进程并发是一个很好的方式。
from multiprocessing.dummy import Pool as ThreadPool
import random,time
# 设置并发数量
threadNum = 3
# 修改为从数据库中读取用例列表
def get_case_list():
return [i for i in range(1,20)]
# 具体的执行方法, 记录测试结果和测试时间
def run_case(case_id):
run_time = random.randrange(1,10)
time.sleep(run_time)
print('run case: %d, result is pass, run time is : %d seconds' %(case_id, run_time))
# 设置多进程执行
pool = ThreadPool(threadNum)
pool.map(run_case, get_case_list())
pool.close()
pool.join()
运行结果:
run case: 1, result is pass, run time is : 6 seconds
run case: 3, result is pass, run time is : 7 seconds
run case: 5, result is pass, run time is : 8 seconds
run case: 6, result is pass, run time is : 1 seconds
run case: 4, result is pass, run time is : 4 seconds
run case: 2, result is pass, run time is : 5 seconds
run case: 7, result is pass, run time is : 6 seconds
run case: 11, result is pass, run time is : 5 seconds
run case: 9, result is pass, run time is : 6 seconds
run case: 10, result is pass, run time is : 2 seconds
run case: 8, result is pass, run time is : 5 seconds
run case: 13, result is pass, run time is : 2 seconds
run case: 12, result is pass, run time is : 9 seconds
run case: 17, result is pass, run time is : 1 seconds
run case: 15, result is pass, run time is : 8 seconds
run case: 14, result is pass, run time is : 8 seconds
run case: 18, result is pass, run time is : 8 seconds
run case: 16, result is pass, run time is : 7 seconds
run case: 19, result is pass, run time is : 8 seconds
Process finished with exit code 0
例如:Chrome,前往 |http://www.baidu.com,|id@@kw@@selenium,点击 |id@@su,验证 |Web填写 Browser Automation,截图
代表纯粹的执行步骤,实现用例完全脱离代码。具体实现可参考之前的分享: https://testerhome.com/topics/15534
例如编写用例如下:
from selenium import webdriver
import time
def search_selenium():
# 在百度首页输入“selenium”,并验证查询结果中包含“Web Browser Automation”
driver = webdriver.Chrome()
driver.get('http://www.baidu.com')
driver.maximize_window()
driver.find_element(by='id' ,value='kw').send_keys('selenium')
driver.find_element(by='id' ,value='su').click()
time.sleep(2)
elements = driver.find_elements(by='xpath', value="//*[contains(.,'Web Browser Automation')]")
assert len(elements)>0
driver.quit()
需执行该用例时,调用对应方法即可:
# 具体的执行方法修改为:
def run_case(case):
exec(case)
目前设置并发数为 6 ,selenium node 的最大并发设置为 10, 可以顺畅地执行。
注: selenium node 的并发比实际使用的并发设置得要大,这是为了预留一些用例执行异常时无法释放资源(即 driver 没有正常退出),导致实际可用的资源不足的情况。
测试未执行的空闲状态:
并发数设置为 6 时的工作状态: