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 ...
随机推荐
- 6.html图像标记img
<html> <head> <title>第六课标签</title> <meta charset="utf-8"> &l ...
- Vue-router 的练习
使用了vue-cli 生成了一套webpack的模版. 之后在其中练习 vue-router. 以下是一些记录. 1.动态路由的配置 import Vue from 'vue' import Rout ...
- 除虫记——有关WindowsAPI文件查找函数的一次压力测试
作者:朱金灿 来源:http://blog.csdn.net/clever101 这里说的除虫是指排除bug的意思.今天排除了一个有意思的bug,其中的场景大致是这样的:现在你要统计一个文件夹下非隐藏 ...
- 88E1111
千兆网phy芯片 支持GMII,RGMII,MII等接口 具备4个GMII时钟模式 支持自适应功能 超低功耗模式 功率降低模式 MDC/MDIO/TWSI接口 支持10Mb/s,100Mb/s,100 ...
- JavaScript_1_简介
1. JavaScript属于客户端脚本语言 2. JavaScript用来改进网页设计.验证表单.检测浏览器.创建cookies,以及更多的应用 a. 是为HTML设计者提供的一种编程工具 b. 可 ...
- hdu 3555 Bomb 炸弹(数位DP,入门)
题意: 给一个数字n,求从1~n中有多少个数是含有49的,比如49,149,1490等都是含49的. 思路: 2^64也顶多是十进制的20多位,那么按十进制位来分析更简单.如果能计算k位十进制数中分别 ...
- maven打包错误:java.lang.IllegalStateException: Unable to find a @SpringBootConfiguration, you need to use @ContextConfiguration or @SpringBootTest(classes=...) with your test
Tests run: 1, Failures: 0, Errors: 1, Skipped: 0, Time elapsed: 0.118 sec <<< FAILURE! - in ...
- ubuntu 14.04 配置java 1.8环境变量
从官网上下载jdk 源文件,并解压 root@hett-PowerEdge-T30:/usr/local/src# tar -xzvf jdk-8u151-linux-x64.tar.gz 解压完成之 ...
- python之路——目录
目录 python基础部分 基础部分 介绍.基本语法.流程控制 列表 元祖 字符串 字典 集合 文件操作 函数 变量 递归 迭代器,生成器,装饰器,Json和pickle 数据序列化 函数 初识函数 ...
- 设置与使用SQL Server的字符集(Collation,即排序规则)
目录 目录 正确认识SQL Server的字符集 选择合适的SQL Server字符集 错误使用SQL Server的字符集 参考资料 正确认识SQL Server的字符集 SQL Server作为一 ...