介绍一中简单介绍了pytest的安装和简单使用,接下来我们就要实际了解pytest了

一、pytest的用例发现规则

pytest可以在不同的函数、包中发现用例,发现的规则如下

  • 文件名以test_开头的py文件
  • 以test_开头的函数
  • 以Test开头的类
  • 以test_开头的方法(与2类似)
  • 要注意的是所有的包必须要有init.py文件(在使用各种编辑器时会自动生成)

二、pytest运行方式

1、单独执行某一个py文件里所有的用例

pytest test_mod.py

2、执行目录下所有的用例

pytest testing/

会按发现规则执行该目录下符合的用例

3、单独执行某一个用例

以函数形式的用例
pytest test_mod.py::testfunc 以类形式的用例
pytest testmod.py::testclass::test_method

三、pytest的测试框架(fixture)模式---xUnit格式

pytest支持以xUnit格式型的测试模型(setup/teardown),但还与python自带的unittest还是有一点差别,如下

  • 模块形式----使用setup_module/teardown_module
  • 函数形式----使用setup_function/teardown_function
  • 类形式----使用setup_class/teardown_class
  • 方法形式---使用setup_method/teardown_method

说了还是不好理解,我们还是通过例子还呈现

1、pytest以函数形式形成测试用例

from __future__ import print_function

def setup_module(module):
print('\nsetup_module()') def teardown_module(module):
print('teardown_module()') def setup_function(function):
print('\nsetup_function()') def teardown_function(function):
print('\nteardown_function()') def test_1():
print('- test_1()') def test_2():
print('- test_2()')

运行结果如下:

通过结果可以看出,各顺序如下

setup_module()----->setup_function()----->test_1----->teardown_function()----->setup_function()----->test_2--->teardown_function()---->teardown_module()

setup_module()和teardown_module只会在开始测试及测试结束时各运行一次

而setup_function()和teardwon_function会在每个用例开始前及结束后各运行一次

2、pytest以类形式的测试用例

from __future__ import print_function
class TestClass: @classmethod
def setup_class(cls):
print ('\nsetup_class()') @classmethod
def teardown_class(cls):
print ('teardown_class()') def setup_method(self, method):
print ('\nsetup_method()') def teardown_method(self, method):
print ('\nteardown_method()') def test_3(self):
print('- test_3()') def test_4(self):
print('- test_4()')

运行结果如下

从结果可以看出,类形式的执行顺序如下

setup_class()--->setup_method()---->test_3()---->teardown_method()--->setup_mothod()-->test_4()---->teardown_method()---->teardonw_class()

setup_class和teardown_class只会在类调用前及结束后各运行一次

setup_method和teardown_method会在每个用例时都会运行

以类形式运行时,类里的用例执行顺序是不会变的,这点比unittest好

3、运行unittest框架模式

pytest也可以直接运行unittest模式的测试用例,如下

class my(unittest.TestCase):

    def delf(self,a):
print a @classmethod
def set_resource(self):
bb='setUpclass'
print bb @classmethod
def setUpClass(cls):
print "setUpclass"
cls.set_resource() def setUp(self):
print "setUp" floating_ip = ('setUptoTearDwon',)
self.addCleanup(self.delf,
floating_ip[0]) def test_1(self):
'''i dont konw'''
a=''
print "test_1" floating_ip = ('bbbbb',)
self.addCleanup(self.delf,
floating_ip[0])
print ""
self.addCleanup(self.delf,
a) def tearDown(self):
print 'this is tearDown' def test_2(self):
print "test_2" @classmethod
def tearDownClass(cls):
print "teardown...."

使用pytest运行该文件,结果如下

collected 2 items

unittest_fomater.py ..

========================== 2 passed in 0.19 seconds ===========================

可以看出,pytest也支持unittest的addCleanup功能等

需要注意的是,如果你在pytest模式中使用setupClass()函数是不行的,不会识别,但如果用例类继承之unittest.Testcase,还是可以识别的

class TestClass:

    @classmethod
def setUpClass(cls):
print ('\nsetup_class()') @classmethod
def teardown_class(cls):
print ('teardown_class()') def setup(self):
print ('\nsetup_method()') def teardown_method(self, method):
print ('\nteardown_method()') def test_3(self):
print('- test_3()') def test_4(self):
print('- test_4()')

用pytest运行时,结果如下,没有识别到setUpclass

collected 2 items

pytest_lean1.py::TestClass::test_3
setup_method()
- test_3()
PASSED
teardown_method() pytest_lean1.py::TestClass::test_4
setup_method()
- test_4()
PASSED
teardown_method()
teardown_class()

python pytest测试框架介绍二的更多相关文章

  1. python pytest测试框架介绍四----pytest-html插件html带错误截图及失败重测机制

    一.html报告错误截图 这次介绍pytest第三方插件pytest-html 这里不介绍怎么使用,因为怎么使用网上已经很多了,这里给个地址给大家参考,pytest-html生成html报告 今天在这 ...

  2. python pytest测试框架介绍三

    之前介绍了pytest以xUnit形式来写用例,下面来介绍pytest特有的方式来写用例 1.pytest fixture实例1 代码如下 from __future__ import print_f ...

  3. python pytest测试框架介绍一

    一.安装 pytest不是python默认的package,需要自动手工安装. pytest支持python 2.6--3.5之间的版本,同时可以在unix及windows上安装 安装方式: pip ...

  4. python pytest测试框架介绍五---日志实时输出

    同样的,在使用pytest进行自动化测试时,需要将实时日志打印出来,而不是跑完后才在报告中出结果. 不过,好在pytest在3.3版本开始,就支持这一功能了,而不用再像nose一样,再去装第三方插件. ...

  5. Pytest测试框架(二):pytest 的setup/teardown方法

    PyTest支持xUnit style 结构, setup() 和 teardown() 方法用于初始化和清理测试环境,可以保证测试用例的独立性.pytest的setup/teardown方法包括:模 ...

  6. 『德不孤』Pytest框架 — 1、Pytest测试框架介绍

    目录 1.什么是单元测试框架 2.单元测试框架主要做什么 3.单元测试框架和自动化测试框架有什么关系 4.Pytest测试框架说明 5.Pytest框架和Unittest框架区别 (1)Unittes ...

  7. 【pytest系列】- pytest测试框架介绍与运行

    如果想从头学起pytest,可以去看看这个系列的文章! https://www.cnblogs.com/miki-peng/category/1960108.html 前言​ ​ 目前有两种纯测试的测 ...

  8. [翻译]pytest测试框架(二):使用

    此文已由作者吴琪惠授权网易云社区发布. 欢迎访问网易云社区,了解更多网易技术产品运营经验. 调用pytest 调用命令: python -m pytest [...] 上面的命令相当于在命令行直接调用 ...

  9. python nose测试框架全面介绍十---用例的跳过

    又来写nose了,这次主要介绍nose中的用例跳过应用,之前也有介绍,见python nose测试框架全面介绍四,但介绍的不详细.下面详细解析下 nose自带的SkipTest 先看看nose自带的S ...

随机推荐

  1. Python游戏《外星人入侵》来了~

    在游戏<外星人入侵>中,玩家控制着一艘最初出现在屏幕底部中央的飞船.玩家可以使用箭头键左右移动飞船,还可使用空格键进行射击.游戏开始时,一群外星人出现在天空中,他们在屏幕中向下移动.玩家的 ...

  2. ubuntu下安装程序的五种方法

    在ubuntu当中,安装应用程序我所知道的有三种方法,分别是apt-get,dpkg安装deb和make install安装源码包三种.下面针对每一种方法各举例来说明. 一.apt-get方法 使用a ...

  3. python 使用模板模式和工厂模式的混合设计开发各种邮件客户端发送邮件

    1.使用模板模式和工厂模式的混合设计开发各种邮件客户端发送邮件. 2.模板模式的目的:能保证快速开发各种邮箱客户端,子类只需要重写模板类邮箱的抽象方法即可.之后再开发任何邮箱就只要加一个类,写3行代码 ...

  4. java okhttp发送post请求

    java的httpclient和okhttp请求网络,构造一个基本的post get请求,都比py的requests步骤多很多,也比py的自带包urllib麻烦些. 先封装成get post工具类,工 ...

  5. python2和3的区别,怎么样做到轻松切换2和3

    以下是菜鸟教程列举的.这些零散的改变需要注意. 下面这些东西可能平时的程序根本没用到,或者稍加注意就可以了.但2和3最主要的区别是,掌握编码. 编码在所有程序中无处不在,处理不好,要么乱码,要么编码解 ...

  6. python打造线程池

    # coding=utf-8 import threading import Queue import time import traceback class ThreadPoolExecutor(o ...

  7. Android输出日志到电脑磁盘

    使用Eclipse查看Log有时候挺恶心的,有些Log ADB会自动的清除,所有有时候导致抓不到有效的Log,把Log保存到文件,然后通过文本查看器查看,感觉好Happy,下面就是脚本文件: adb ...

  8. 7 -- Spring的基本用法 -- 10... 获取其他Bean的属性值;获取Field值;获取任意方法的返回值

    7.10 高级依赖关系配置 组件与组件之间的耦合,采用依赖注入管理:但基本类型的成员变量值,应直接在代码中设置. Spring支持将任意方法的返回值.类或对象的Field值.其他Bean的getter ...

  9. linux 端口占用情况

    1,查看8010端口是否被占用 [root@cloud ~]# netstat -an|grep 8010 tcp 0 0 0.0.0.0:8010 0.0.0.0:* LISTEN 2,查看8010 ...

  10. IIS日志清理(VBS版,JS版)

    IIS默认日志记录在C:\WINDOWS\system32\LogFiles,时间一长,特别是子站点多的服务器,一个稍微有流量的网站,其日志每天可以达到上百兆,这些文件日积月累会严重的占用服务器磁盘空 ...