(原文:https://www.cnblogs.com/fancy0158/p/10046333.html)

unittest提供的Fixtures用以在测试执行前和执行后进行必要的准备和清理工作,可以定义在模块、类、用例执行前后的工作

1、模块

在模块中定义setUpModule()和tearDownModule()处理模块执行前后的工作

def setUpModule():
print('module start') def tearDownModule():
print('module end')

2、测试类

在测试类中定义setUpClass()和tearDownClass()函数处理测试类执行前的工作,setUpClass和tearDownClass的写法稍微不同:

a、需通过@classmethod进行装饰

b、方法的参数是cls,cls与self并无不同之处,只是大家都习惯了这样的命名

@classmethod
def setUpClass(cls):
print('Class start')
@classmethod
def tearDownClass(cls):
print('class end')

3、测试用例

即我们前面熟悉的setUp()和tearDown()方法,每个用例执行前后都会调用

def setUp(self):
print('test case start')
def tearDown(self):
print('test case end')

在前面我们把Desired Capabilities 启动 session 时相关的配置放在setUp()中,把driver的退出放在tearDown()中。导致每执行一个测试用例,就需要配置连接一次移动端设备。

我们可以把相关的配置工作和清理工作放到测试类中,这样就不必每次执行一个case都重新配置和退出。比如,在计算器中,配置连接到移动端应用,连续执行加运算和减运算case,再退出。

 
# coding:utf-8
from appium import webdriver
import unittest, time

#模块的fixtures处理
def setUpModule():
print('module start')
def tearDownModule():
print('module end') class TestFixture(unittest.TestCase):
#测试类中配置参数
@classmethod
def setUpClass(cls):
print('Class start')
# Desired Capabilities启动session时配置的参数
desired_caps = {}
desired_caps['platformName'] = 'Android' #移动平台
desired_caps['platformVersion'] = '4.4.2' # 指定平台的系统版本
desired_caps['deviceName'] = 'Android Emulator' # 设备名称
desired_caps['appPackage'] = 'com.android.calculator2' # 包名
desired_caps['appActivity'] = '.Calculator' # launcherActivity
cls.driver = webdriver.Remote('http://localhost:4723/wd/hub', desired_caps) # 定义webdriver对象 @classmethod
def tearDownClass(cls):
print('class end')
cls.driver.quit() #所有用例执行完之后退出 def setUp(self):
print('test case start') #用例执行前 def tearDown(self):
print('test case end') #用例执行后 #加号运算case
def test_add(self):
print('add case is running')
self.driver.find_element_by_name("5").click()
self.driver.find_element_by_name("+").click()
self.driver.find_element_by_name("8").click()
self.driver.find_element_by_android_uiautomator("new UiSelector().text(\"=\")").click()
time.sleep(2)
self.driver.find_element_by_name("CLR").click() #减运算case
def test_sub(self):
print('sub case is running')
self.driver.find_element_by_name("9").click()
self.driver.find_element_by_xpath("//android.widget.Button[contains(@content-desc,'minus')]").click()
self.driver.find_element_by_name("1").click()
self.driver.find_element_by_name("=").click()
time.sleep(2)
self.driver.find_element_by_name("CLR").click() if __name__ == '__main__':
#构造测试集
suite = unittest.TestSuite()
suite.addTest(TestFixture('test_sub'))
suite.addTest(TestFixture('test_add'))
#执行测试
runner = unittest.TextTestRunner()
runner.run(suite)
 

测试输出结果如下:

 
module start
Class start
test case start
sub case is running
test case end
test case start
add case is running
test case end
class end
module end
 

可以看到,在测试机上,执行完减运算后马上执行加运算,测试用例连续执行。执行完所有的测试用例后才退出

转载请注明出处并带上原链接

Appium+python的单元测试框架unittest(2)——fixtures(转)的更多相关文章

  1. Appium+python的单元测试框架unittest(1)(转)

    unittest为python语言自带的单元测试框架,python把unittest封装为一个标准模块封装在python开发包中.unittest中常用的类有:unittest.TestCase.un ...

  2. Appium+python的单元测试框架unittest(4)——断言(转)

    (原文:https://www.cnblogs.com/fancy0158/p/10051576.html) 在我们编写的测试用例中,测试步骤和预期结果是必不可少的.当我们运行测试用例时,得到一个运行 ...

  3. Appium+python的单元测试框架unittest(3)——discover(转)

    (原文:https://www.cnblogs.com/fancy0158/p/10047906.html) TestSuite套件可以添加很多个用例后运行,但是每个用例都需要调用addTest()函 ...

  4. python之单元测试框架—unittest

    一. 什么是单元测试?单元测试的对象是什么? 1: 什么是单元测试? 按照定义,单元测试就是对单个模块或者单个类或者单个函数进行测试,一般是开发做的,按照阶段分,一般就是单元测试.集成测试.系统测试. ...

  5. Python之单元测试框架unittest

    创建class继承unittest,每一个测试用例是以test开头的函数,先执行setup,然后用例按照字母的顺序执行,然后执行teardown import unittest class demo( ...

  6. python之单元测试框架—unittest(补充)

    一. unittest最核心的四个概念 unittest中最核心的四个概念是:test case,test suite,test runner,test fixture TestCase:一个test ...

  7. Python单元测试框架unittest之单用例管理(一)

    一.概述 本文介绍python的单元测试框架unittest,unittest原名为PyUnit,是由java的JUnit衍生而来,这是Python自带的标准模块unittest.unittest是基 ...

  8. 单元测试框架unittest

    单元测试:单元测试,是指对软件中的最小可测试单元进行检查和验证,对于单元测试中单元的含义,一般来说,要根据实际情况去判定其具体含义如:c语言中单元指一个函数,java里单元指一个类,图形化的软件中可以 ...

  9. Python 单元测试框架系列:聊聊 Python 的单元测试框架(一):unittest

    作者:HelloGitHub-Prodesire HelloGitHub 的<讲解开源项目>系列,项目地址:https://github.com/HelloGitHub-Team/Arti ...

随机推荐

  1. face++

    1.链表反转 2.快排 3.m*k   n*k两矩阵计算欧几里得距离np.tile 4.链表排序,要求时间复杂度小于O(N^2),空间O(1),不允许改变链表的值 5.2sum及其变体 6.给一个数组 ...

  2. 使用iptables和tc对端口限速

    首先,我们来看一下tc,TC(Traffic Control)命令,是linux自带的告警流控命令.Linux操作系统中的流量控制器TC(Traffic Control)用于Linux内核的流量控制, ...

  3. HDU 1102(Constructing Roads)(最小生成树之prim算法)

    题目链接: http://acm.hdu.edu.cn/showproblem.php?pid=1102 Constructing Roads Time Limit: 2000/1000 MS (Ja ...

  4. datatable的excel导入,其中有关于datatables的很多参数设置

    datatable的excel导入,其中有关于datatables的很多参数设置 http://www.cnblogs.com/liyuhuan/p/5633095.html

  5. nRF5 SDK for Mesh(一) 介绍和下载源码

    一: 官网介绍地址:http://www.nordicsemi.com/Products/Bluetooth-low-energy/nRF5-SDK-for-Mesh Nordic offers a ...

  6. 二叉查找树(二叉排序树)(C语言)

    #include<stdio.h> #include "fatal.h" struct TreeNode; typedef struct TreeNode *Posit ...

  7. 你真的了解Scrum吗?

    敏捷开发是以用户的需求为核心,采用迭代.循序渐进的方法进行软件开发.而Scrum是实现敏捷开发的具体方式之一.然而你对Scrum又了解多少呢? 什么是Scrum Scrum是橄榄球运动的一个专业术语, ...

  8. 使用DBNEWID Utility 修改oracle数据库的 db name 和 dbid

    使用DBNEWID Utility 工具可以同时修改数据库名.DBID,也可以只修改其中一项 官方参考: https://docs.oracle.com/cd/E11882_01/server.112 ...

  9. Unity-iPhone has Conflicting Provisioning Settings

    Select the top level node called Unity-iPhone in the left tree view (the one with the blue item). Se ...

  10. 随机获取UDID

    (NSString *)uuidString { CFUUIDRef uuid_ref = CFUUIDCreate(NULL); CFStringRef uuid_string_ref= CFUUI ...