自动化测试文件结构+多个用例一起执行且生成报告

本文介绍了自动化测试项目的文件结构,包括common、Locator、pages、reports和screenshots目录。每个测试用例定义为独立的class,包含浏览器启动函数。为了方便批量执行多个用例并生成测试报告,文章提出创建一个casesuit函数来查找并运行所有相关测试用例。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

  1. 项目文件结构
    common
    存放经常被引用的文件,如HTMLTestRunner
    Locator:
    存放页面元素的位置和对应url,如oper_userName = (By.ID, “username”)
    pages:
    对于网站有不同的页面,可分开写
    reports:
    存放测试生成的报告
    screenshots:
    存放测试中截屏的图片

每个用例都是一个class。拥有自己的浏览器启动函数。具体形式如下:

import sys
import time
import unittest
from selenium import webdriver
class TestCase1(unittest.TestCase):
    def setUp(self):
        self.driver = webdriver.Chrome(getChromeDriver())
    ####中间插入该用例的其他函数    
    def tearDown(self):
        self.driver.quit()

如果有很多个用例,则需要一个总函数来进行批量运行,并生成测试报告。单个运行太麻烦了。首先建立一个casesuit,在当前文件中进行查找对应的case。

import os,sys
import unittest
import time

import  HtmlTestRunner


os_path = os.getcwd()
casesPath = os_path + "\\test_case"
reportPath = os_path + "\\Reports\\"
#templatePath = os_path + "\\template\\report_template.html"

'''如果HtmlTestRunner 报错请执行pip install html-testRunner '''

def CreatSuite():
    suite=unittest.TestSuite()
    discover=unittest.defaultTestLoader.discover(casesPath, pattern='test_*.py', top_level_dir=None)
    for test_case in discover:
        suite.addTests(test_case)
    return suite

if __name__ == "__main__":
    all_test_cases = CreatSuite()
    currentTime = str(int(time.time()))
    if not os.path.exists(reportPath):
        os.makedirs(reportPath)
    reportName = 'EPT_TestReport_' + currentTime + '.html'
    reportName = reportPath + reportName
    testRunner = HtmlTestRunner.HTMLTestRunner(output=reportPath)
    testRunner.run(all_test_cases)
  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值