python--setUp()和tearDown()应用

setUp:表示前置条件,它在每一个用例执行之前必须会执行一次 

setUp可以理解为我们需要自动化测试时,需要打开网页窗口,输入对应测试地址,这一些属于前置条件。

tearDown:表示释放资源,它在每次用例执行完之后会执行一次

tearDown可以理解为我们测试完毕后,需要关闭浏览器。

以下示例演示setUp和tearDown的执行顺序:

#coding:utf-8
import unittest
class Xseq(unittest.TestCase):
def setUp(self):
print "前置测试条件"
def tearDown(self):
print "结束测试条件"
def testadd(self):
print ‘1+1=‘,1+1
def testsub2(self):
print ‘3-2=‘,3-2
def testsub1(self):
print ‘3-3=‘,3-3
def mul(self):
print ‘3*1‘,3*1
if __name__=="__main__":
unittest.main()

运行结果为:

由此可看到运行的顺序:

1. 每次执行函数方法时,必须先执行一次setUp()函数,执行完一个函数方法后,必须再执行一次tearDown()函数

2. 执行函数的时候有序号时,例如testxx1,testxx2 会根据序号来进行执行

3. 当函数方法不是以test开头是,不执行该方法。

若我想要改变setUp()在所有用例执行之前只执行一次,和tearDown()函数在所有用例执行之后只执行一次的话

1. 需要使用python中的@classmethod内置装饰器

2. 需要把函数名改为setUpClass(cls) / tearDownClass(cls)

以下为示例:

#coding:utf-8
import unittest
class Xseq(unittest.TestCase):
@classmethod
def setUpClass(cls):
print "前置测试条件"
@classmethod
def tearDownClass(cls):
print "结束测试条件"
def testadd(self):
print ‘1+1=‘,1+1
def testsub2(self):
print ‘3-2=‘,3-2
def testsub1(self):
print ‘3-3=‘,3-3
def mul(self):
print ‘3*1‘,3*1
if __name__=="__main__":
unittest.main()

运行结果为:

原文地址:https://www.cnblogs.com/yitao326/p/10538708.html

时间: 2024-07-30 19:15:27

python--setUp()和tearDown()应用的相关文章

Python 单元测试 之setUP() 和 tearDown()

setUp:表示前置条件,它在每一个用例执行之前必须会执行一次 setUp可以理解为我们需要自动化测试时,需要打开网页窗口,输入对应测试地址,这一些属于前置条件. tearDown:表示释放资源,它在每次用例执行完之后会执行一次 tearDown可以理解为我们测试完毕后,需要关闭浏览器. 以下示例演示setUp和tearDown的执行顺序: #coding:utf-8import unittestclass Xseq(unittest.TestCase): def setUp(self): pr

Pytest系列(3) - setup和teardown的详细使用

如果你还想从头学起Pytest,可以看看这个系列的文章哦! https://www.cnblogs.com/poloyy/category/1690628.html 前言 用过unittest的童鞋都知道,有两个前置方法,两个后置方法:分别是 setup() setupClass() teardown() teardownClass() Pytest也贴心的提供了类似setup.teardown的方法,并且还超过四个,一共有十种 模块级别:setup_module.teardown_module

Junit测试中的setup和teardown 和 @before 和 @After 方法

这几天做Junit测试接触到了setup和teardown两个方法,简单的可以这样理解它们,setup主要实现测试前的初始化工作,而teardown则主要实现测试完成后的垃圾回收等工作. 需要注意的是Junit3中每个测试方法执行时都会执行它们,而不是一个类中执行一次,查了查资料,JUnit4版本采用注解的方式可以实现一个类只执行一次,下面看看测试代码: jar下载地址: http://search.maven.org/#search%7Cgav%7C1%7Cg%3A%22junit%22%20

python安装Fabrix出现Command "python setup.py egg_info"错误

python安装Fabrix出现Command "python setup.py egg_info" failed with error code 1 in /tmp/pip-build-ynJnAl/pynacl/ 解决: 向上查看发现No package 'libffi' found,原因是这个libffi库未安装 所以使用:yum install libffi-devel -y 最后安装成功

解决安装ipython时Command "python setup.py egg_info" failed with error code 1 in /tmp

最近使用ubuntu16.04 server版安装ipython的时候一直在报错: IPython 6.0+ does not support Python 2.6, 2.7, 3.0, 3.1, or 3.2.      When using Python 2.7, please install IPython 5.x LTS Long Term Support version. python2.7 ipython Command "python setup.py egg_info"

python setup

python setup 使用Python以来,基本不可逃避的就是setup.py文件,因为我们要安装各种各样的python软件或者其它第三方package,但是经常使用python setup.py install进行安装,却不甚明白该文件的工作原理.由于目前工作涉及了openstack,一个基于python的云操作系统,因此,理解一些python的基本知识是必须要的.故找时间了解了基本的python setup.py文件的制作. 一.一个简单的setup.py文件: 比如,我们经常使用的一个

解决Command "python setup.py egg_info" failed with error code 1坎坷路

机器:os x 本地想跑一下一个开源django博客应用,下了源码到里头pip install -r requirements.txt 结果屡次出错,Command "python setup.py egg_info" failed with error code 1 试了多次一直于最后一行这个错误. 我开始以为权限问题,setuptools问题 后来有转机了,我把 requirements.txt里头一个个安装,最后原是psycopg2这个安装有问题 pip我错怪你了,不过把官方源换

Command "python setup.py egg_info" failed with error code 1一种问题的解决方法

问题描述:无论是你在pycharm中直接使用import and install命令,还是pip的时候出现了Command "python setup.py egg_info" failed with error code 1这样的问题描述,需要考虑可能pip的这个源里面没有对应的python3.5的模块 解决方法:通常可以直接google "XXX for python3.5",或者在github中找到对应的python3.5的项目,将对应的.py文件添加到pyt

Python setup.py和MANIFEST.in文件

Setup.py文件 from setuptools import setup from codecs import open # 第三方依赖包及版本号 requires = ['beautifulsoup4>=4.3.2', 'gearman>=2.0.2', 'pymongo>=2.7.2', 'threadpool>=1.2.7', 'geoip2>=2.1.0', 'pywin32>=219'] # 包列表 packages = ['MSE', 'MSE.Dev

解决 Command "python setup.py egg_info" failed with error code 1 问题

参考: "pip install unroll": "python setup.py egg_info" failed with error code 1 解决 Command "python setup.py egg_info" failed with error code 1 问题 在执行 pip install -r requirements.txt 时遇到错误: Command "python setup.py egg_info