(引用) unittest测试驱动之执行测试(三)
转载:http://www.wtoutiao.com/p/ydeoyY.html
在unittest的模块中,提供了TestRunner类来进行运行测试用例,在实际的应用中,经常使用的是TextTestRunner类,执行后,会已文字的形式打印出测试结果,见如下的测试执行的代码:
#coding:utf-8
from seleniumimport webdriver
import unittest
classbaiduTest(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Firefox()
self.driver.maximize_window()
self.driver.get('http://www.baidu.com')
def testBaiduTitle(self):
self.assertIn(u'百度一下,你就知道',self.driver.title)
def testBaiduTitle2(self):
self.assertNotEqual(u'bing下,你就知道',self.driver.title)
def tearDown(self):
self.driver.close()
if__name__=='__main__':
suite=unittest.TestSuite()
suite.addTest(baiduTest('testBaiduTitle'))
suite.addTest(baiduTest('testBaiduTitle2'))
#执行用例
runner=unittest.TextTestRunner()
runner.run(suite)
执行后,生成如下的测试结果,见截图:
一、unittest.skip()
unittest.skip('注释')是忽略此测试,但是得写忽略该case的原因,如在一个自动化的测试模块中,由于某些功能取消,那么对应的case也就不再执行,可以添加unittest.skip("忽略该测试用例"),见下面的代码示例,忽略了测试百度url的测试用例,见测试代码:
#coding:utf-8
from seleniumimport webdriver
importunittest,sys,time
reload(sys)
sys.setdefaultencoding('utf-8')
classBaiduTest(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Firefox()
self.driver.maximize_window()
self.driver.get('http://www.baidu.com')
self.driver.implicitly_wait(30)
def testTitle(self):
self.assertEqual(u'百度一下,你就知道',self.driver.title)
@unittest.skip('Ignore this test case')
def testUrl(self):
self.assertEqual('https://www.baidu.com/',self.driver.current_url)
def tearDown(self):
self.driver.quit()
@staticmethod
def suite():
suite=unittest.TestSuite(unittest.makeSuite(BaiduTest))
return suite
if__name__=='__main__':
unittest.TextTestRunner(verbosity=2).run(BaiduTest.suite())
见执行后的结果,跳过了testUrl测试用例的执行,见截图:
二、unittest.expectedFailure
unittest.expectedFailure指的是期望失败,见下面的测试代码。期望testUrl测试用例执行失败:
#coding:utf-8
from seleniumimport webdriver
importunittest,sys,time
reload(sys)
sys.setdefaultencoding('utf-8')
classBaiduTest(unittest.TestCase):
def setUp(self):
self.driver=webdriver.Firefox()
self.driver.maximize_window()
self.driver.get('http://www.baidu.com')
self.driver.implicitly_wait(30)
def testTitle(self):
self.assertEqual(u'百度一下,你就知道',self.driver.title)
@unittest.expectedFailure
def testUrl(self):
self.assertEqual('http://www.baidu',self.driver.current_url)
def tearDown(self):
self.driver.quit()
@staticmethod
def suite():
suite=unittest.TestSuite(unittest.makeSuite(BaiduTest))
return suite
if__name__=='__main__':
unittest.TextTestRunner(verbosity=2).run(BaiduTest.suite())
见执行后的测试结果,会显示出来期望的结果是成功还是失败,见截图:
(引用) unittest测试驱动之执行测试(三)的更多相关文章
- jmeter接口自动化-通过csv文件读取用例并执行测试
最近在公司测试中经常使用jmeter这个工具进行接口自动化,简单记录下~ 一.在csv文件中编写好用例 首先在csv文件首行填写相关参数(可根据具体情况而定)并编写测试用例.脚本可通过优先级参数控制执 ...
- Java+Selenium 3.x 实现Web自动化 - Maven打包TestNG,利用jenkins执行测试
1. Jenkins本地执行测试 or 服务器端执行测试 测试代码计划通过jenkins执行时,通过网上查询各种教程,大多数为本地执行测试,由此可见,本地执行是大多数人的选择. 经过探讨,最终决定采用 ...
- LTP--linux稳定性测试 linux性能测试 ltp压力测试 内核更新 稳定性测试
LTP--linux稳定性测试 linux性能测试 ltp压力测试 zhangzj1030关注14人评论33721人阅读2011-12-09 12:07:45 说明:在写这篇文章之前,本人也不曾了 ...
- unittest对单个测试类的多种测试执行方法总结
基于unittest测试框架编写的测试脚本,一般单个测试类下会有多个测试方法,unittest也提供多种测试执行方式,下面就不同方式或者需求一一实操并说明: 一.使用unittest下main()方法 ...
- python调用HTMLTestRunner+unittest实现一次执行多个测试类,并生成与每个测试类对应的测试报告,具体看代码,附上整个project代码
python自动化框架雏形,根据自己需要封装:ui自动化,接口自动化均可适用,python版本为python3.x,不要问我为什么不用python2.x,附上整个project代码:http://fi ...
- {MySQL的逻辑查询语句的执行顺序}一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析
MySQL的逻辑查询语句的执行顺序 阅读目录 一 SELECT语句关键字的定义顺序 二 SELECT语句关键字的执行顺序 三 准备表和数据 四 准备SQL逻辑查询测试语句 五 执行顺序分析 一 SEL ...
- unittest 执行测试脚本输出测试报告
import unittestimport HTMLTestRunnertest as HTMLTestRunner#获取路径path = './'#创建测试套件,读取测试脚本suite = unit ...
- python+selenium自动化软件测试(第10章):测试驱动TDD
测试驱动开发模式,要求开发在写业务代码的时候,先写出测试代码,同时单元测试例子决定了如何来写产品的代码,并且不断的成功的执行编写的所有的单元测试例子,不断的完善单元测试例子进而完善产品代码, 这样随着 ...
- python实例编写(6)--引入unittest测试框架,构造测试集批量测试(以微信统一管理平台为例)
---恢复内容开始--- 一.python单元测试实例介绍 unittest框架又叫PyUnit框架,是python的单元测试框架. 先介绍一个普通的单元测试(不用unittest框架)的实例: 首先 ...
随机推荐
- php iquery
<?php/** * */function iQuery($sql) { $dbcon = mysqli_connect("localhost", "root&qu ...
- Discovering the Computer Science Behind Postgres Indexes
This is the last in a series of Postgres posts that Pat Shaughnessy wrote based on his presentation ...
- Android学习六:Socket 使用
1socket的作用 通过http去获取服务器的数据在有些情况下是行不通的,所有使用socket与服务器通信也是必须掌握的 2.代码 好了上代码,代码中有解释,首先是简单的服务端代码 package ...
- SVN错误:run 'cleanup' if it was interrupted的解决
原文转自:http://www.lxway.com/812960411.htm 今天碰到了个郁闷的问题,svn执行clean up命令时报错“Previous operation has not fi ...
- C#操作Word的辅助类(word2003) 修改完善版
转自:http://blog.csdn.net/jiutao_tang/article/details/6567608 该类在他人编写的几个类基础上扩展完善而来,主要功能有: (1)插入文本 (2)插 ...
- 在浏览器中输入Google.com并且按下回车之后发生了什么(转载)
原文地址:https://github.com/skyline75489/what-happens-when-zh_CN#id9 本文试图回答一个古老的面试问题:当你在浏览器中输入google.com ...
- 在 Windows上配置NativeScript CLI
1.安装Node.js,到https://nodejs.org/下载安装 2.安装Chocolatey,https://chocolatey.org/,先看一下关于chocolatey的介绍: 安装方 ...
- Eclipse集成javap查看字节码
分析java语言特性的一个好帮手是使用javap工具查看java编译后的字节码,楼主今天在学习java泛型中的桥方法时遇到一些不解,想到javap这个好工具可以帮助解答一些疑惑,索性就捣鼓如何在ecl ...
- (LinkedList)Intersection of Two Linked Lists
Write a program to find the node at which the intersection of two singly linked lists begins. For ex ...
- Flex中使用CSS控制页面样式
Using file: Stylebounding.mxml Stylebounding2.mxml myCSS0329.css 在Flex4中使用CSS控制样式,既可以直接在MXML文件中写样式,也 ...