Unittest加载测试用例居然没有标签功能,怪我咯?那我自己封装一个,那都不是事!

UI测试——unittest框架+HTML报告生成 unittest框架 单个脚本重复的操作:导包,获取浏览器的驱动,关闭浏览器,清除浏览器的操作。降低效率。 避免这些重复的操作? 用框架 unittest框架之setUp方法: 在执行测试用例脚本之前进行初始化环境的方法 方法参数中的self是干什么的 self代表类的实例/引用 实例+. self. 定义全局变量,所有方法都可以使用 ... 阅读详情

需求分析

  • 假设当前4条用例,需要执行test02和test04用例,那么可以通过测试套件的方式去实现这个需求
  • 如果当前有100条用例,只需执行其中某些用例。怎么办呢?
#! /usr/bin/python3
# -*- coding:utf-8 -*-
# @公众号  : 码上开始

import unittest

class TestCase(unittest.TestCase):

    def test01(self):
        print("执行第1条用例")

    def test02(self):
        print("执行第2条用例")

    def test03(self):
        print("执行第3条用例")

    def test04(self):
        print("执行第4条用例")

解决思路

给用例打标签,只运行某些具有标签的用例
unittest没有标签功能

  1. 在测试方法后面加标签的名称
  2. 获取该类所有定义的方法名(即以test开头的用例)
  3. add动态添加用例

封装代码

#! /usr/bin/python3
# -*- coding:utf-8 -*-

# @FileName: day5.py
# @Time    : 2020/8/9 20:57
# @公众号  : 码上开始

import unittest

class TestCase(unittest.TestCase):

    def test01(self):
        print("执行第1条用例")

    def test02_smoke(self):
        print("执行第2条用例")

    def test03(self):
        print("执行第3条用例")

    def test04_smoke(self):
        print("执行第4条用例")
if __name__ == "__main__":
    # 测试套件
    testcases = unittest.TestSuite()
    list_case = [ ]
    for case in dir(TestCase):
        # endswith如果方法名以smoke结尾添加到用例
        if case.endswith("smoke") and case.startswith("test"):
            list_case.append(case)
    # 只有符合条件的才会加到用列表里
    # print(list_case)
    # 列表的用例然后加到套件
    for smoke in list_case:
        testcases.addTest(TestCase(smoke))
    # 最后执行用例
    runner = unittest.TextTestRunner().run(testcases)

运行结果

执行第2条用例
执行第4条用例
..
----------------------------------------------------------------------
Ran 2 tests in 0.000s
OK

如果对软件测试、接口测试、自动化测试、持续集成、面试经验。感兴趣可以进到893694563,群内会有不定期的分享测试资料。还会有技术大牛,业内同行一起交流技术

unittest这样实现用例打标签功能比pytest的标签功能更好用 1、众所周知unittest加载测试用例的设计依赖于测试类的概念,就是测试类都必须继承unittest.TestCase类,在测试加载的时候,也是基于此测试类去加载测试方法,unittest会加载测试类中所有的测试方法,因此没有类似pytest的给用例打mark标签,选择性执行测试方法的功能。2、那么如何实现给测试类的测试方法加上属性呢,会让开发测试用例代码的同学会有一个比较好的用户使用体验呢,其实很简单,就按照pytest的设计来就行了,通过装饰器的方式。另外discover方法运用了多线程去优化性能。 阅读详情

相关推荐

SAP MTS计划策略40实战:如何配置‘带最终组装的计划‘提升定制化生产效率

本文深入解析SAP计划策略40(带最终组装的计划)的核心机制与实战配置。该策略通过“需求消耗”与“组装灵活性”,有效融合按库存生产(MTS)与按订单生产(MTO)的优势,帮助制造企业在保持通用组件库存稳定的同时,快速响应客户的定制化需求,从而提升生产效率与客户满意度。

ruby5的博客 974

单元测试框架对比

一、用例编写规则 1.unittest提供了test cases、test suites、test fixtures、test runner相关的类,让测试更加明确、方便、可控。使用unittest编写用例,必须遵守以下规则: (1)测试文件必须先import unittest (2)测试类必须继承unittest.TestCase (3)测试方法必须以“test_”开头 (4)测试类必须要有u...

qq_37493425的博客 868

STM32单片机远程升级

stm32远程升级

fang_yang_wa的博客 1万+

pytest框架-介绍、Mark(打标签)、执行标签命令运行用例、用例执行顺序、

1、pytest介绍:基于unittest 之上的单元测试框架   1.1、自动发现测试模块和测试用例;     unitest 需要添加用例,(泰斯特楼贷)加载器加载测试用例     pytest 只需要一条代码就可以   1.2、断言使用 assert + 表达;     unitest 定义了所有的 断言方式     pytest 只需要使用一个关键字 assert + 表达式 就可以了,表达式为真则断言通过,表达式为假则断言失败   1.3、pytest 可以设置测试会话级、模块级、.

EmbeddedCoder 的优快云博客 2992

GEE开发之ERA5(气温、降水、压力、风速等)数据获取和分析

前言:ERA5主要提供DAILY和MONTHLY两大类别数据,其中主要包括气温、降水、压力、风速等。

等待着冬天的风的博客 3万+

Ubuntu操作系统等保测评指南

Ubuntu操作系统等保测评指南

qq_40181198的博客 7033

如何用unittest做自动化测试

一、unittest核心组件 1、TestCase:测试用例类,用来定义测试用例函数的 2、TestSuite:测试套件,用来收集测试用例的 3、TestRunner:测试用例运行类,用来执行测试用例的,以测试套件维度去执行 4、TestFixture:测试脚手架,前置条件,后置处理 二、测试流程 1、TestCase:先定义测试用例类,在测试用例类中写测试方法 2、TestSuite:收集你要执行的测试用例类或者文件里面的测试方法,放到测试套件 3、TestRunner:将收集好的测试条件

m0_45237847的博客 713

车载毫米波雷达实战:用MATLAB复现DDMA-MIMO雷达的Empty-band算法(附代码解析)

本文详细介绍了车载毫米波雷达中DDMA-MIMO技术的实战应用,重点解析了Empty-band算法的原理与MATLAB实现。通过搭建仿真环境,文章深入探讨了发射天线通道解调与相位法速度解模糊两大核心难题的解决方案,并提供了完整的代码解析,为雷达算法工程师提供了从理论到实践的清晰指南。

hill8的博客 565

python unittest框架用后分享

转自:http://blog.sina.com.cn/s/blog_80f3f42d0101c03t.html 时下junit测试框架非常流行,确实非常好用。很多人可能不知道,python也有一套unittest的测试框架叫做pyunit,被称作python版本的junit,由Kent Beck和Erich Gamma开发的. JUnit也是kent开发的。有了pyunit,让我们写ca

xiaosong的博客 2171

Altera USB-Blaster在Win10/Win11的驱动兼容性处理

解决Altera USB-Blaster在Windows 10和Windows 11系统下的驱动兼容性问题,详细演示如何手动安装与更新altera usb-blaster驱动安装,确保下载电缆正常识别,提升开发调试效率。

weixin_42284380的博客 1296

Simulink开发项1000例实战专栏--实例161:基于Simulink实现电动汽车电机驱动系统的建模与仿真

通过上述步骤,我们成功实现了基于Simulink的电动汽车电机驱动系统的建模与仿真。该系统能够全面评估电机驱动系统的性能,验证控制策略的有效性,并通过优化设计提高车辆的动力性和效率。引入智能预测:结合人工智能技术,实现更智能的控制策略。扩展功能:增加对多种电机类型的支持,提升系统通用性。实验验证:将仿真模型应用于实际硬件,进行实验验证,评估其在实际工况下的表现。

xiaoheshang_123的博客 191

Unittest加载测试用例没有标签功能你相信吗?那我自己封装一个,朋友们看好了!

目录 需求分析 解决思路 封装代码 好好学习,天天向上! 一起加入到:642830685,你来或不来, 我都在群里等你 一.需求分析 假设当前4条用例,需要执行test02和test04用例,那么可以通过测试套件的方式去实现这个需求 如果当前有100条用例,只需执行其中某些用例。怎么办呢? #! /usr/bin/python3 # -*- coding:utf-8 -*- # @FileName: day5.py # @Time : 2020/8/9 20:57 # @公众号 :程序媛一菲

别人的故事,是参考答案,而不是标准答案 398

双重机器学习模型中解释变量是连续变量的代码,看到好多都是0/1变量,如何解决?

🏆本文收录于 《全栈Bug调优(实战版)》 专栏,该专栏专注于分享我在真实项目开发中遇到的各类疑难Bug及其深层成因,并系统提供高效、可复现的解决思路和实操方案。无论你是刚入行的新手开发者,还是拥有多年项目经验的资深工程师,本专栏都将为你提供一条系统化、高质量的问题排查与优化路径,助力你加速成长,攻克技术壁垒,迈向技术价值最大化与职业发展的更高峰🚀!    📌 特别说明: 文中部分技术问题来源于真实生产环境及网络公开案例,均经过精挑细选与系统化整理,并结合多位一线资深架构师和工程师多年实战经验沉淀,提

**My Coding Family** 856

unittest 过滤测试用例

unittest 过滤测试用例

qq_37668510的博客 723

Devexpress16.2.6破解工具

Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具Devexpress16.2.6破解工具

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

pytest和unittest

pytest和unittest的区别 基本测试用例: unittest- 需要 import unittest ​ 写case必须定义类 ​ 测试类必须继承class TestLogin(unittest.TestCase): ​ 测试用例函数必须以test开头 def test_login1(self): pytest - 需要import pytest ​ 不必须定义类,类和函数都支持 ​ 类必须以Test开头 ​ 函数必须以test开头 断言: unittest- 提供了很多自带的断言,通过

weixin_39193977的博客 992

WINCC7.3破解文件

WINCC7.3硬件够破解文件,有需要的下载!WINCC7.3硬件够破解文件,有需要的下载!

用selenium+unittest写测试用例

一、使用Unittest编写测试用例 通常用selenium编写测试用例,selenium包本身不包含测试工具或框架,所以可以用python自带的框架Unittest来编写测试用例 项目需求: 对百度网站搜索写测试用例 1.导入需要模块 import time import unittest from selenium import webdriver from selenium.webdriver.common.keys import Keys unittest模块:基于Java Junit的pytho

咸柠七s的博客 615

温度PID控制PLC程序

温度PID稳定控制程序,适用于欧姆龙PLC,对其他PLC也具有很大参考价值

python代码设计测试用例_Python3的Unittest用例按编写顺序执行

unittest是Python标准库自带的单元测试框架,是Python版本的JUnit,关于unittest框架的使用,官方文档非常详细,网上也有不少好的教程,这里就不多说了。本文主要分享在使用unittest的过程中,做的一些扩展尝试。先上一个例子。import unittestclass TestLegion(unittest.TestCase):def test_create_legion(...

weixin_39952031的博客 336

unittest和pytest的区别

pytest --reruns 3 --reruns-delay 2或者pytest.main(["--reruns","3","--reruns-delay","2"])unittest必须:test*.py,pytest默认定义方式:test_*.py或者*_test.py,也可通过。unittest必须以test开头,非test_,pytest默认以test开头,非test_,也可自定义;(3)执行用例:python -m 标签名 或者 python.main(["-m","标签名"])

okcross0的博客 1280

Python unittest框架 实用函数与特性

unittest 模块提供了一种结构化的方式来编写测试用例。测试用例继承自 unittest.TestCase 类,并通过一系列的测试方法(以 test_ 开头的方法)来组织。

人生不怕起点低,就怕没追求 647

python unittest_扩展 Python 的 unittest 框架

2019.3.13 更新了下unittest是Python标准库自带的单元测试框架,是Python版本的JUnit,关于unittest框架的使用,官方文档非常详细,网上也有不少好的教程,这里就不多说了。本文主要分享在使用unittest的过程中,做的一些扩展尝试。先上一个例子。import unittestclass TestLegion(unittest.TestCase):def test_...

weixin_39553352的博客 150

Python unittest 自动识别并执行测试用例方式

自动化测试执行的用例有很多,python额测试用例文件,都是以“test”开头的。

软件自动化测试技术交流、资源分享 646

unittest 学习实操(下)

unittest 学习实操(下)

m0_60566330的博客 124

robotframework - 测试用例&套件- Settings标签

1、Test Case -- Settings标签截图 2、Test Case Settings 标签说明: Documentation:用于描述用例的一个小文本,它可以把 URL 地址转换为可点击的链接。如输入https://www.baidu.com Setup 和 Teardown:如果你了解 unittest 单元测试框架的话,一定对这两个单词不陌生。 setUp 用于设...

weixin_30686845的博客 384
上一篇: Python操作file文件,你觉的你懂了,恐怕这些细节你没拿捏好
下一篇: 我就想学接口测试,为什么还要学Fiddler抓包,抓包是什么鬼?
码上开始
博客等级 码龄9年 1780粉丝 294原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码上开始

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值