Python unittest 学习
import unittest class UTest(unittest.TestCase):
def test_upper(self):
self.assertEqual('foo'.upper(), 'FOO') def test_isupper(self):
self.assertTrue('FOO'.isupper())
self.assertFalse('Foo'.isupper()) if __name__ == '__main__':
unittest.main()
suite = unittest.TestLoader().loadTestsFromTestCase(UTest)
unittest.TextTestRunner(verbosity=2).run(suite)
suite = unittest.TestSuite()
suite.addTest(UTest('test_isupper'))
suite.addTest(UTest('test_upper'))
runner = unittest.TextTestRunner()
runner.run(suite)
但是,如果是class 里只有一个测试方法,但测试时需要不同的测试数据,C 的方法派上用场:
def __init__(self, marketcode, stockcode, stocktype,methodName):
super(Comparison, self).__init__(methodName)
self.marketcode = marketcode
self.stockcode = stockcode
self.stocktype = stocktype
def test_getPriceInfo(self):
stime = Utils.getTimestamp()
mkt_price = ParseResponse.getMarketLastPrice(self.marketcode,self.stockcode,self.stocktype)
position_price = ParseResponse.getPositionLastPrice(self.loginKey,self.loginCookie,self.stockcode,self.marketcode)
self.infoDir["timestamp"] = stime
self.infoDir["ticker"] = self.marketcode + self.stockcode
self.infoDir["postionprice"] = position_price
self.infoDir["marketprice"] = mkt_price
AlertRecorder.cmpMktPosition(self.infoDir)
print self.infoDir
c. 最后在class 外定义一个运行的方法,其中 Comparison(stockInfo[0],stockInfo[1],stockInfo[2], "test_getPriceInfo") 即是创建一个测试类示例,test_getPriceInfo为测试用例方法名,这个是 unittest.TestCase类需要的参数。
def run():
suite = unittest.TestSuite()
stockInfoList = Scheduler.get_mktPostion_stocks()
for stockInfo in stockInfoList:
suite.addTest(Comparison(stockInfo[0],stockInfo[1],stockInfo[2], "test_getPriceInfo"))
runner = unittest.TextTestRunner()
runner.run(suite)
def run():
suite = unittest.TestLoader().loadTestsFromTestCase(testCaseClassName)
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)
def run():
suite = unittest.TestSuite()
suite.addTest(testCaseClassName('test_method'))
runner = unittest.TextTestRunner(verbosity=2)
runner.run(suite)
suite = unittest.TestLoader().loadTestsFromTestCase(UTest)
runner = unittest.TextTestRunner(verbosity=2)
result = runner.run(suite)
print result.testsRun #运行的测试用例的总数
print len(result.failures) #失败的测试用例的数目
Python unittest 学习的更多相关文章
- XUnit测试框架-Python unittest
选择 语言选择 本次个人作业我选择的语言是Python,了解学习Python有一段时间了但是一直没有练习,所以这次玩蛇,使用的版本是Python3.6. 开发工具选择 我选择的IDE是Pycharm, ...
- python+appium学习总结
经过了这个月的学习,今天终于完成了公司APP系统的自动化的脚本的编写. 通过单元测试框架UNITTEST,进行脚本的连跑,本来还想把测试数据统一写到EXCEL表格内,实现脚本与数据的分离. 后来发现增 ...
- 【原】Learning Spark (Python版) 学习笔记(三)----工作原理、调优与Spark SQL
周末的任务是更新Learning Spark系列第三篇,以为自己写不完了,但为了改正拖延症,还是得完成给自己定的任务啊 = =.这三章主要讲Spark的运行过程(本地+集群),性能调优以及Spark ...
- 60分钟Python快速学习(给发哥一个交代)
60分钟Python快速学习 之前和同事谈到Python,每次下班后跑步都是在听他说,例如Python属于“胶水语言啦”,属于“解释型语言啦!”,是“面向对象的语言啦!”,另外没有数据类型,逻辑全靠空 ...
- python爬虫学习(1) —— 从urllib说起
0. 前言 如果你从来没有接触过爬虫,刚开始的时候可能会有些许吃力 因为我不会从头到尾把所有知识点都说一遍,很多文章主要是记录我自己写的一些爬虫 所以建议先学习一下cuiqingcai大神的 Pyth ...
- python爬虫学习 —— 总目录
开篇 作为一个C党,接触python之后学习了爬虫. 和AC算法题的快感类似,从网络上爬取各种数据也很有意思. 准备写一系列文章,整理一下学习历程,也给后来者提供一点便利. 我是目录 听说你叫爬虫 - ...
- Python正则表达式学习摘要及资料
摘要 在正则表达式中,如果直接给出字符,就是精确匹配. {m,n}? 对于前一个字符重复 m 到 n 次,并且取尽可能少的情况 在字符串'aaaaaa'中,a{2,4} 会匹配 4 个 a,但 a{2 ...
- python 线程学习
彩照 一.学习[1] # -*- coding: utf-8 -*- import time import thread def timer(no, interval): cnt = 0 while ...
- Openstack python api 学习文档 api创建虚拟机
Openstack python api 学习文档 转载请注明http://www.cnblogs.com/juandx/p/4953191.html 因为需要学习使用api接口调用openstack ...
随机推荐
- 涉及到弹出层的opacity样式问题
最近遇到一个弹出层在Chrome中重复的问题,观察发现是opacity引起的以下是代码及现象<!DOCTYPE html><html> <head> ...
- 阿里云栖社区dubbo 资源整理
1.apache dubbo pdf git 地址:https://github.com/dubbo/awesome-dubbo/tree/master/slides/meetup/201905%40 ...
- Xcode 升级后,cocoaPod 问题
当我从Xcode 6.3切换到Xcode6.4的时候,因为我是mac上安装了两个不同的版本,现在把Xcode 6.3卸掉了. 现在再次运行pod install命令的时候,提示如下错误: Upda ...
- Linux中grep、sed、awk使用介绍
linux文件操作命令介绍1)grepgrep 用于在文件中查找符合条件的记录grep 参数 过滤条件 文件过滤的条件中可使用正则表达式-c 显示符合的行数-i 忽略大小写-n 显示符合要求的记录,包 ...
- DataModel doesn't have preference values
mahout和hadoop实现简单的智能推荐系统的时候,出现了一下几个方面的错误 DataModel doesn't have preference values 意思是DataModel中没有找到初 ...
- idea快速生成实体类Entity
1)打开idea 2)添加mysql的数据连接 3)生成类
- spring boot 下 dataTable|pagehelper 组合进行分页 筛选 排序
1)Js 需提前引用 jquery.dataTables $(function () { //提示信息 初始化设置 一般不需要改 var lang = { "sProcessing" ...
- Shell脚本调用SQL文格式
Shell脚本调用SQL文格式 1. 定义需要执行的SQL文,以及需要输出文件 OUTFILE=\${DATADIR}/\${FILENAME} SQLFILE=\${DATADIR}/check_t ...
- PLAYGROUND 延时运行
PLAYGROUND 延时运行 由 王巍 (@ONEVCAT) 发布于 2015/09/16 从 WWDC 14 的 Keynote 上 Chris 的演示就能看出 Playground 异常强大,但 ...
- 674. Longest Continuous Increasing Subsequence@python
Given an unsorted array of integers, find the length of longest continuous increasing subsequence (s ...