当前位置: 首页 > news >正文

使用python测试框架完成自动化测试并生成报告-实例练习

练习一: 使用unittest 完成自动化测试并使用HttpTestRunner生成报告
'''
1、写个简单的计算器功能,大小写转换功能,随机生成字符串功能
2、编写测试用例,不同的数据(你能想到的所有测试用例),并进行断言。除0的选择可以跳过skip,随机生成字符串功能可以断言是否包含你名字的缩写。
3、使用unittest框架+HTMLTestRunner,最后生成html报告
'''
import unittest
import os
import time
import logging
import ddt
from HTMLTestRunner import HTMLTestRunner
import randomtestData1 = [{'a':5,'b':1,'x':'+','result':6},{'a':5,'b':1,'x':'-','result':4},{'a':5,'b':1,'x':'*','result':5},{'a':5,'b':1,'x':'/','result':5}]
testData2 = [{'string':'asd','stringType':'upper','result':'ASD'},{'string': 'ASD', 'stringType': 'lower','result':'asd'}]#被测函数
class test_demo():def jisuanqi(a,x,b):if x == '+':return a+belif x =='-':return a-belif x == '*':return a*belif x == '/':return a/belse:logging.info('只支持数字加减乘除四则运算')def translation(string,stringType):if stringType == 'lower':return string.lower()elif stringType == 'upper':return string.upper()else:print('ERROR:只支持大小写类型转换')def random_string(num):return random.random(num)#测试函数
@ddt.ddt
class TestCases(unittest.TestCase):@classmethoddef setUpClass(cls):print('整个测试类运行前执行')def setUp(self):print("每个测试方法执行前运行一次")def tearDown(self):print("每个测试方法执行完后运行一次")@ddt.data(*testData1)def test_case_jisuanqi(self,data):result = test_demo.jisuanqi(data['a'],data['x'],data['b'])assert result == data['result']@ddt.data(*testData2)def test_case_translation(self,data):result = test_demo.translation(data['string'],data['stringType'])assert result == data['result']def test_case_randomString(self):pass@classmethoddef tearDownClass(cls):print("整个测试类运行完成后执行一次")#测试报告
if __name__ == '__main__':report_path = os.path.join(os.path.dirname(__file__), 'report')now = time.strftime("%Y-%m-%d %H_%M_%S", time.localtime())filename = report_path + "/" + now + "_result.html"print('******************************************')suite = unittest.TestSuite()suite.addTests(unittest.TestLoader().loadTestsFromTestCase(TestCases))with open(filename, 'wb') as fp:runner = HTMLTestRunner(stream=fp,title='测试报告',description='测试用例')runner.run(suite)

练习二:使用pytest完成自动化测试并用allure生成测试报告
'''
建立一个登陆模块的测试用例,一个人力资源模块的测试用例,其中包括增删改查4个小功能,查询不需要登陆。
使用pytest,allure的方式,技术上要有登陆依赖用Fixture,conftest实现,要添加人员时有参数化,数据驱动,
使用文件或list/dict的方式传入数据。通过allure的添加附加信息,及各种信息包括feature,story,step,attach,title,discription等。
'''
conftest.py
@pytest.fixture(scope = 'module')
def test_login(request):user = request.param['user']password = request.param['password']if user != 'linda':print('用户名错误')elif password != '888888':print('密码错误')else:print('登陆成功')yield  # 模块执行完case后  在最后执行一遍teardown操作。print('执行teardown')print('推出登陆')
Pytest.py
import allure
import pytest
import os
import subprocesstest_user_data = [{"user": "linda", "password": "888888"}]test_user_data1 = [{"user": "linda", "password": "888888"},{"user": "servenruby", "password": "123456"},{"user": "linda", "password": "123456"}]test_user_data2 = [{"name": "中国平安", "money": 999, "country": 'china'},{"name": "阿里巴巴", "money": 888, "country": 'jepan'},{"name": "拼多多", "money": 666, "country": 'USA'}]@allure.feature('测试登录模块')
@pytest.mark.run(order = 1) #第一个执行
@pytest.mark.parametrize('test_login', test_user_data1, indirect=True)
class TestLogin():def test_login_case1(self,test_login):assert 1 == 1@allure.feature('测试人事模块')
@pytest.mark.parametrize('test_login', test_user_data, indirect=True)
class TestPersion():@allure.story('测试用例:新增人员')@pytest.mark.parametrize('data',test_user_data2)def test_persion_add(self,test_login,data):with allure.step("步骤1"):allure.attach('说明')print('新增用户%s'%data['name'])@allure.story('测试用例:删除新增的人员')@pytest.mark.parametrize('data',test_user_data2)def test_persion_del(self,test_login,data):print('删除用户%s'%data['name'])

558 pytest -v -s PyTest.py --alluredir=./result/   在测试执行期间收集结果 


559 allure serve ./result/   测试完成后查看实际报告, 在线看报告 


 

http://www.lryc.cn/news/23572.html

相关文章:

  • JavaWeb 实战 01 - 计算机是如何工作的
  • 线性代数学习-1
  • 人工智能写的十段代码,九个通过测试了
  • 巴塞尔问题数值逼近方法
  • 【深度学习环境】Docker
  • 基于vscode开发vue项目的详细步骤教程 2 第三方图标库FontAwesome
  • 今天面了个腾讯拿25K出来的软件测试工程师,让我见识到了真正的天花板...
  • OSG三维渲染引擎编程学习之六十九:“第六章:OSG场景工作机制” 之 “6.9 OSG数据变量”
  • Tektronix泰克TDP3500差分探头3.5GHz
  • 轻松实现内网穿透:实现远程访问你的私人网络
  • MySQL长字符截断
  • python计算量比指标
  • 下拉框推荐-Suggest-SUG
  • Nmap的几种扫描方式以及相应的命令
  • Qt::QOpenGLWidget 渲染天空壳
  • 谷歌搜索技巧大全 | 谷歌高级搜索语法指令
  • JAVA开发(JAVA垃圾回收的几种常见算法)
  • 你还不会用CAD一键布置停车位?赶紧学起来!
  • 【MySQL之MySQL底层分析篇】系统学习MySQL,从应用SQL语法到底层知识讲解,这将是你见过最完成的知识体系
  • 单核CPU是否有线程可见性问题?
  • MyBatis 架构介绍
  • 加密算法---RSA 非对称加密原理及使用
  • MySQL-查询语句
  • 【算法】【数组与矩阵模块】求数组中需要排序的最短子数组长度
  • centos安装Anaconda3
  • 【微信小程序】-- WXML 模板语法 - 列表渲染 -- wx:for wx:key(十二)
  • 【Linux】Linux中gcc/g++的使用
  • 【Spring Cloud Alibaba】(五)Dubbo启动报错?一直重连报错?你值得学习的是排查问题的方法
  • adb命令的使用
  • springBoot自定义参数类型转换器