python创建易用性灵活的unittest结构

python自动化测试创建易用性灵活的unittest结构

一、背景说明
很多相互学习的伙伴都说基本机构连贯不起来,我提供的代码在本地都不能执行。因为库的原因,或者操作系统,或者代码路径等因素,运行我提供的代码会直接报错。因此构建一个入手简单,直接可以运行的代码包,结构的构建层次也尽量比django简单点。我本地的环境是pycharm+py3.7,引用了beautifulreport。其中构建了selenium。构建selenuim参考链接: 构建selenium.
二、创建结构,结构如图
在这里插入图片描述

说明:创建python项目,项目类型选择pure python,项目命名为unittest_xxx;创建完成后按照上面的层次新建文件夹和.py文件:文件夹case(py文件夹)、文件夹img/report;case文件下新建如图的.py文件;再新建一个run_all.py。图中__init__.py文件是新建py文件夹自带,具体作用百度。venv文件是虚拟环境,作用自己百度。
三、代码内容模块

test_case1代码如下
"""
测试用例1
"""
from selenium import webdriver
import unittest
from BeautifulReport import BeautifulReport
import os

class Test1(unittest.TestCase):
    '''测试用例1测试'''
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome()

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

    @BeautifulReport.add_test_img('test_01')
    def test_01(self):
        '''失败用例'''
        self.driver.get('https://www.baidu.com')
        self.save_img('test_01')
        self.driver.find_element_by_id('kw1').send_keys('百度一下')
        self.assertTrue(True)

    @BeautifulReport.add_test_img('test_02')
    def test_02(self):
        '''失败用例'''
        self.driver.get('https://www.baidu.com')
        self.save_img('test_02')
        self.assertIn('百度二下',self.driver.title)

    @BeautifulReport.add_test_img('test_03')
    def test_03(self):
        '''成功用例'''
        self.driver.get('https://www.baidu.com')
        self.save_img('test_03')
        self.assertIn('百度',self.driver.title)

    # 截图方法
    def save_img(self, img_name):
        self.img_path = 'img'
        self.driver.get_screenshot_as_file('{}/{}.png'.format(os.path.abspath(self.img_path), img_name))

if __name__ == '__main__':
    unittest.main(verbosity=2)
test_case2代码如下
from selenium import webdriver
import unittest

class Test2(unittest.TestCase):
    '''测试用例2测试'''
    @classmethod
    def setUpClass(cls):
        cls.driver = webdriver.Chrome()

    @classmethod
    def tearDownClass(cls):
        cls.driver.quit()

    def test_01(self):
        '''成功用例'''
        self.driver.get('https://www.baidu.com')
        self.driver.find_element_by_id('kw').send_keys('百度一下')

    def test_02(self):
        '''成功用例'''
        self.driver.get('https://cn.bing.com/')

if __name__ == '__main__':
    unittest.main(verbosity=2)
run_test代码如下
import unittest
import os
import time
# 将BeautifulReport.zip解压后放到Python安装目录里的/Lib/site-packages/文件夹下
from BeautifulReport import BeautifulReport

# 获取路径
cur_path = os.path.dirname(os.path.realpath(__file__))
# 测试用例路径
case_path = os.path.join(cur_path, 'case')
# 测试报告路径
report_path = os.path.join(cur_path, 'report')

if __name__ == "__main__":
    suite = unittest.defaultTestLoader.discover(case_path,'test*.py')
    result = BeautifulReport(suite)
    report_name = str(time.strftime("%Y%m%d-%H%M%S", time.localtime(time.time()))) + 'htmlreport'
    #report_dir/log_path都可以使用,我的电脑使用log会警告
    result.report(filename=report_name, description='hyh_Demo示例', report_dir=report_path)
test_代码如下(我是用来调试的,可以不要这个文件)
import os

# 获取路径
cur_path = os.path.dirname(os.path.realpath(__file__))
# 测试用例路径
case_path = os.path.join(cur_path, 'case')
# 测试报告路径
report_path = os.path.join(cur_path, 'report')
print(cur_path)
print(case_path)
print(report_path)

四、以上代码创建完成之后,直接点击执行run_test,等待用例执行完成,测试报告会自动生成在report文件下,截图会自动生成在img文件下。用浏览器打开测试报告如图。
在这里插入图片描述
在这个基础上,对应的自己的测试用例就可以新建起来了。本人的建议,希望研究下unittest的机构,层级之间的关系,一些陌生代码的含义。有什么问题欢迎留言,不过我未必会回答。需要源码的可以发送邮件到201893228@qq.com,注明自己的来意。

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值