unittest测试框架组成_unittest接口自动化

unittest测试框架组成_unittest接口自动化一、unittest简介unittest是python的单元测试框架。unittest单元测试提供了创建测试用例,测试套件以及批量执行的方案,unittest在安装pyhton以后就直接自带了,直接importunittest就可以使用。作为单元测试的框架,unittest也是可以对程序最小模块的一种敏捷化的测试。在自动化测试中,我们虽然不需要做白盒测试,但是必须需要知道所使用语言的单元测试框架。利用单元测试框架,创建一个类,该类继承unittest的TestCase,这样可以把每

大家好,又见面了,我是你们的朋友全栈君。如果您正在找激活码,请点击查看最新教程,关注关注公众号 “全栈程序员社区” 获取激活教程,可能之前旧版本教程已经失效.最新Idea2022.1教程亲测有效,一键激活。

Jetbrains全系列IDE稳定放心使用

一、unittest简介

unittest 是python 的单元测试框架。unittest 单元测试提供了创建测试用例,测试套件以及批量执行的方案, unittest 在安装pyhton 以后就直接自带了,直接import unittest 就可以使用。

作为单元测试的框架, unittest 也是可以对程序最小模块的一种敏捷化的测试。在自动化测试中,我们虽然不需要做白盒测试,但是必须需要知道所使用语言的单元测试框架。

利用单元测试框架,创建一个类,该类继承unittest的TestCase,这样可以把每个case看成是一个最小的单元, 由测试容器组织起来,到时候直接执行,同时引入测试报告。

unittest各组件的关系为:
在这里插入图片描述

  • test fixture:初始化和清理测试环境,比如创建临时的数据库,文件和目录等,其中 setUp() 和 setDown() 是最常用的方法
  • test case:单元测试用例,TestCase 是编写单元测试用例最常用的类
  • test suite:单元测试用例的集合,TestSuite 是最常用的类
  • test runner:执行单元测试
  • test report:生成测试报告

一个单元测试示例:

__author__ = 'sunraylily'
# -*- coding: utf-8 -*-
from selenium import webdriver
from selenium.webdriver.common.by import By
from selenium.webdriver.common.keys import Keys
from selenium.webdriver.support.ui import Select
from selenium.common.exceptions import NoSuchElementException
from selenium.common.exceptions import NoAlertPresentException
import unittest, time, re
class Baidu1(unittest.TestCase):
 	def setUp(self):
 		self.driver = webdriver.Firefox()
 		self.driver.implicitly_wait(30)
		self.base_url = "http://www.baidu.com/"
		self.verificationErrors = []
		self.accept_next_alert = True
	 def test_baidusearch(self):
		driver = self.driver
 		driver.get(self.base_url + "/")
		driver.find_element_by_id("kw").click()
 		driver.find_element_by_id("kw").clear()
 		driver.find_element_by_id("kw").send_keys(u"测试")
 		driver.find_element_by_id("su").click()
 	def test_hao(self):
 		driver = self.driver
 		driver.get(self.base_url + "/")
 		driver.find_element_by_link_text("hao123").click()
 		self.assertEqual(u"hao123_上网从这里开始", driver.title)
 	def is_element_present(self, how, what):
 		try: self.driver.find_element(by=how, value=what)
 			except NoSuchElementException as e: return False
	return True
 #判断alert是否存在,可删除
	def is_alert_present(self):
 		try: self.driver.switch_to_alert()
 			except NoAlertPresentException as e: return False
 	return True
 	def close_alert_and_get_its_text(self):
 		try:
 			alert = self.driver.switch_to_alert()
 			alert_text = alert.text
 			if self.accept_next_alert:
 				alert.accept()
 			else:
 				alert.dismiss()
 			return alert_text
 			finally: self.accpet_next_alert
 #test fixture 清除环境
 	def tearDown(self):
 		self.driver.quit()
 		self.assertEqual([].self.verificationErrors)
 if __name__ == "__main__":
 	unittest.main()
''' 可以增加verbosity参数,例如unittest.main(verbosity=2) 在主函数中,直接调用main() ,在main中加入verbosity=2 ,这样测试的结果就会显示的更加详细。 这里的verbosity 是一个选项, 表示测试结果的信息复杂度,有三个值: 0 ( 静默模式): 你只能获得总的测试用例数和总的结果比如总共100个失败,20 成功80 1 ( 默认模式): 非常类似静默模式只是在每个成功的用例前面有个“ . ” 每个失败的用例前面有个“F” 2 ( 详细模式): 测试结果会显示每个测试用例的所有相关的信息 '''

二、批量执行脚本

2.1 构建测试套件

完整的单元测试很少只执行一个测试用例,开发人员通常都需要编写多个测试用例才能对某一软件功能进行比较完整的测试,这些相关的测试用例称为一个测试用例集,在unittest中是用TestSuite 类来表示的。

假设我们已经编写了testbaidu1.py,testbaidu2.py两个文件,那么我们怎么同时执行这两个文件呢?

2.1.1 addTest()的应用

当有多个或者几百测试用例的时候, 这样就需要一个测试容器( 测试套件) ,把测试用例放在该容器中进行执行,unittest 模块中提供了TestSuite 类来生成测试套件,使用该类的构造函数可以生成一个测试套件的实例,该类提
供了addTest来把每个测试用例加入到测试套件中。

将testbaidu1.py、testbaidu2.py、runall.py放在同一个目录testcase中

runall.py

# -*- coding: utf-8 -*-
import unittest,csv
import os,sys
import time
#导入testbaidu1,testbaidu2
import testbaidu1
import testbaidu2
#手工添加案例到套件,
def createsuite():
	suite = unittest.TestSuite()
 #将测试用例加入到测试容器(套件)中
 	suite.addTest(testbaidu1.Baidu1("test_baidusearch"))
 	suite.addTest(testbaidu1.Baidu1("test_hao"))
 	suite.addTest(testbaidu2.Baidu2("test_baidusearch"))
 	return suite
if __name__=="__main__":
	suite=createsuite()
	runner = unittest.TextTestRunner(verbosity=2)
 	runner.run(suite)

上述做法有两个不方面的地方,阻碍脚本的快速执行,必须每次修改runall.py:
1)需要导入所有的py文件,比如import testbaidu1,每新增一个需要导入一个
2)addTest需要增加所有的testcase,如果一个py文件中有10个case,就需要增加10次

2.1.2 makeSuite()和TestLoader()的应用

在unittest 框架中提供了makeSuite() 的方法,makeSuite可以实现把测试用例类内所有的测试case组成的测试套件TestSuite ,unittest 调用makeSuite的时候,只需要把测试类名称传入即可。

TestLoader 用于创建类和模块的测试套件,一般的情况下,使TestLoader().loadTestsFromTestCase(TestClass)
来加载测试类。

# -*- coding: utf-8 -*-
import unittest,csv
import os,sys
import time
import testbaidu1
import testbaidu2
#手工添加案例到套件,
def createsuite():
	suite = unittest.TestSuite()
 #将测试用例加入到测试容器(套件)中
	suite.addTest(unittest.makeSuite(testbaidu1.Baidu1))
	suite.addTest(unittest.makeSuite(testbaidu2.Baidu2))
 	return suite
 ''' suite1 = unittest.TestLoader().loadTestsFromTestCase(testbaidu1.Baidu1) suite2 = unittest.TestLoader().loadTestsFromTestCase(testbaidu2.Baidu2) suite = unittest.TestSuite([suite1, suite2]) return suite '''
 
if __name__=="__main__":
 	suite=createsuite()
 	runner = unittest.TextTestRunner(verbosity=2)
 	runner.run(suite)

经过makeSuite()和TestLoader()的引入,我们不用一个py文件测试类,只需要导入一次即可。

那么能不能测试类也不用每次添加指定呢?

2.1.3 discover()的应用

discover 是通过递归的方式到其子目录中从指定的目录开始, 找到所有测试模块并返回一个包含它们对象的TestSuite ,然后进行加载与模式匹配唯一的测试文件,discover 参数分别discover(dir,pattern,top_level_dir=None)

runall.py—注意路径

# -*- coding: utf-8 -*-
import unittest,csv
import os,sys
import time
#手工添加案例到套件,
def createsuite():
 
discover=unittest.defaultTestLoader.discover('../test',pattern='test*.py',top_level_dir=None)
 print discover
 return discover
 
if __name__=="__main__":
 suite=createsuite()
 runner = unittest.TextTestRunner(verbosity=2)
 runner.run(suite)

2.2 用例的执行顺序

unittest 框架默认加载测试用例的顺序是根据ASCII 码的顺序,数字与字母的顺序为: 0-9,A-Z,a~z 。
所以, TestAdd 类会优先于TestBdd 类被发现, test_aaa() 方法会优先于test_ccc() 被执行。对于测试目录与测试文件来说, unittest框架同样是按照这个规则来加载测试用例。
addTest()方法按照增加顺序来执行。

2.3 忽略用例执行

@unittest.skip("skipping") #只需加上这个注解即可
 def test_baidusearch(self):
 driver = self.driver
 driver.get(self.base_url + "/")
 driver.find_element_by_id("kw").click()
 driver.find_element_by_id("kw").clear()
 driver.find_element_by_id("kw").send_keys(u"测试")
 driver.find_element_by_id("su").click()
 driver.find_element_by_id("su").click()

三、unittest断言

自动化的测试中, 对于每个单独的case来说,一个case的执行结果中, 必然会有期望结果与实际结果, 来判断该case是通过还是失败, 在unittest 的库中提供了大量的实用方法来检查预期值与实际值, 来验证case的结果, 一般来说, 检查条件大体分为等价性, 逻辑比较以及其他, 如果给定的断言通过, 测试会继续执行到下一行的代码, 如果断言失败, 对应的case测试会立即停止或者生成错误信息( 一般打印错误信息即可) ,但是不要影响其他的case执行。

unittest 的单元测试库提供了标准的xUnit 断言方法。下面是一些常用的断言

序号 断言方法 断言描述
1 assertEqual(arg1, arg2, msg=None) 验证arg1=arg2,不等则fail
2 assertNotEqual(arg1, arg2, msg=None) 验证arg1 != arg2, 相等则fail
3 assertTrue(expr, msg=None) 验证expr是true,如果为false,则fail
4 assertFalse(expr,msg=None) 验证expr是false,如果为true,则fail
5 assertIs(arg1, arg2, msg=None) 验证arg1、arg2是同一个对象,不是则fail
6 assertIsNot(arg1, arg2, msg=None) 验证arg1、arg2不是同一个对象,是则fail
7 assertIsNone(expr, msg=None) 验证expr是None,不是则fail
8 assertIsNotNone(expr, msg=None) 验证expr是None,不是则fail

四、HTML报告生成

脚本执行完毕之后,还需要看到HTML报告,下面我们就通过HTMLTestRunner.py 来生成测试报告。
修改runall.py

# -*- coding: utf-8 -*-
import unittest,csv
import os,sys
import time
import HTMLTestRunner
#手工添加案例到套件,
def createsuite():
 
	discover=unittest.defaultTestLoader.discover('../test',pattern='test*.py',top_level_dir=None)
 	print discover
 	return discover
 if __name__=="__main__":
	 curpath=sys.path[0]
 #取当前时间
 	now=time.strftime("%Y-%m-%d-%H %M %S",time.localtime(time.time()))
 if not os.path.exists(curpath+'/resultreport'):
 	os.makedirs(curpath+'/resultreport')
 
 	filename=curpath+'/resultreport/'+now+'resultreport.html'
 	with open(filename,'wb') as fp:
 #出html报告
	 runner=HTMLTestRunner.HTMLTestRunner(stream=fp,title=u'测试报告',description=u'用例执行
情况',verbosity=2)
	 suite=createsuite()
	 runner.run(suite)

五、异常捕捉和错误截图

用例不可能每一次运行都成功,肯定运行时候有不成功的时候。如果可以捕捉到错误,并且把错误截图保存,这将是一个非常棒的功能,也会给我们错误定位带来方便。

def savescreenshot(self,driver,file_name):
 if not os.path.exists('./image'):
	 os.makedirs('./image')
	 now=time.strftime("%Y%m%d-%H%M%S",time.localtime(time.time()))
 #截图保存
 	driver.get_screenshot_as_file('./image/'+now+'-'+file_name)
 	time.sleep(1)
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。

发布者:全栈程序员-站长,转载请注明出处:https://javaforall.net/182229.html原文链接:https://javaforall.net

赞 (0)
全栈程序员-站长的头像全栈程序员-站长


相关推荐

  • Java面向对象之创建和使用对象——定义学生/教师类并输出相关信息

    Java面向对象之创建和使用对象——定义学生/教师类并输出相关信息学生类importjava.util.Scanner;publicclassStudent{Stringname=”张三”;intage=16;Stringgrade=”三年二班”;Stringfancy=”打篮球唱歌读书冒险”;publicvoidintroduce(){System.out.println(“————————————-

    2022年7月8日
  • 成功解决Address localhost:1099 is already in use

    成功解决Address localhost:1099 is already in use前言本文所说的内容适用于java的端口被占用Tomcat端口被占用的情况,可以参考解决方式二!报错过程今天使用IDEA启动项目的时候,报了下列错误:Addresslocalhost:1099isalreadyinuse问题分析这是端口号被占用了,已经有其他应用正在使用这个端口号问题解决方式一:按快捷键Ctrl+Shift+Esc打开任务管理器…………

    2022年7月20日
  • 电阻参数_关于电阻的相关参数

    实际应用时,通常采用平均电阻温度系数,定义式:TCR(平均)=(R2-R1)/R1(T2-T1)有负温度系数、正温度系数及在某一特定温度下电阻只会发生突变的临界温度系数。紫铜的电阻温度系数为1/234.5℃。不同类型电阻温度稳定性从优到次,依次为:金属箔、线绕、金属膜、金属氧化膜、碳膜、有机实芯。1。镀金并不是为了减小电阻,而是因为金的化学性质非常稳定,不容易氧化,接头上镀金是为了防止接触不良(不…

    2022年4月8日
  • JAVA乐观锁_spring的线程池配置

    JAVA乐观锁_spring的线程池配置首先介绍一些乐观锁与悲观锁:悲观锁:总是假设最坏的情况,每次去拿数据的时候都认为别人会修改,所以每次在拿数据的时候都会上锁,这样别人想拿这个数据就会阻塞直到它拿到锁。传统的关系型数据库里边就用到了很

    2022年8月2日
  • 2021年7月总结

    2021年7月总结

    2022年2月19日
  • 时间复杂度是什么_时间复杂度的表示方法

    时间复杂度是什么_时间复杂度的表示方法-宝宝为啥听不懂他们在讨论的时间复杂度0.0-我怎么知道这个算法运行得比那个算法快0.0-我究竟会不会超时0.0-我为什么还会超时0.0-时间复杂度怎么算0.0在别人还不会求时间复杂度的时候而你会了是不是很酷在别人都会求时间复杂度的时候而你不会是不是很尴尬千里之行始于足下希望这篇文章能祝你一臂之力=w= 此篇详解,希望能帮助各位稍微解决一下不解=w=…

    2022年11月2日

发表回复

您的邮箱地址不会被公开。 必填项已用 * 标注

关注全栈程序员社区公众号