1、断言用assert,可以进行==,!=,+,-,*,/,<=,>=,is True、False,is not True、False ,in ,not in 等判断。

import pytest
def add(a,b):
return a + b def is_prime(n):
if n <= 1:
return False
for i in range(2,n):
if n % i == 0:
return False
return True def test_add_1():
'''测试相等'''
assert add(3,4) == 7 def test_add_2():
'''测试不相等'''
assert add(12,3) != 16 def test_add_3():
'''测试大于或等于'''
assert add(17,22) <= 50 def test_add_4():
'''测试小于或等于'''
assert add(17,22) >=38 def test_in():
'''测试包含'''
a = 'hello'
b = 'he'
assert b in a def test_not_in():
'''测试不包含'''
a = 'hello'
b = 'hi'
assert b not in a def test_true_1():
'''判断是否为True'''
assert is_prime(13) is True def test_true_2():
'''判断是否为True'''
assert is_prime(7) is True def test_true_3():
'''判断是否不为True'''
assert is_prime(4) is not True def test_true_4():
'''判断是否不为True'''
assert is_prime(6) is not True def test_false_1():
'''判断是否为False'''
assert is_prime(8) is False if __name__ == '__main__':
pytest.main()

2、测试文件和测试函数必须以“test”开头,测试类必须以‘Test’开头。

3、可以通过main()方法执行测试用例。需要指定参数和路径,还可以指定某个测试类或测试方法用“::”隔开。如:

pytest.main(['-s','./test_fixtures_01.py::test_multiply_5_6'])

4、Pytest提供了丰富的参数运行测试用例,‘-s’:关闭捕捉,输出打印信息。‘-v’:用于增加测试用例的冗长。‘-k’ :运行包含某个字符串的测试用例。如:pytest -k add XX.py 表示运行XX.py中包含add的测试用例。‘q’:减少测试的运行冗长。‘-x’:出现一条测试用例失败就退出测试。在调试阶段非常有用,当测试用例失败时,应该先调试通过,而不是继续执行测试用例。pytest还可以运行测试目录下的所有测试用例:pytest 目录路径

5、Fixtrue

import pytest

#功能函数
def multiply(a,b):
return a * b class TestMultiply:
#=======fixtures========
@classmethod
def setup_class(cls):
print('setup_class==============================>') @classmethod
def teardown_class(cls):
print('teardown_class==========================>') def setup_method(self,method):
print('setup_method============================>') def teardown_method(self,method):
print('teardown_method============================>') def setup(self):
print('setup======================================>') def teardown(self):
print('teardown===================================>')

6、参数化。pytest本身是支持参数化的,不需要安装插件。

import pytest
import math #pytest参数化
@pytest.mark.parametrize('base,exponent,expected',[(2,2,4),(2,3,8),(2,4,16),(0,9,0)],ids = ['case1','case2','case3','case4'])
def test_pow(base,exponent,expected):
assert math.pow(base,exponent) == expected if __name__ == '__main__':
pytest.main(['-s','./test_parameterized.py'])

7、conftest.py 是pytest的本地测试配置文件。可以设置项目级别的Fixtrue还可以导入外部插件,还可以指定钩子函数。conftest.py值作用于它所在的目录及子目录。

import pytest

#设置钩子函数

@pytest.fixtrue()

def test_url():

  return 'http://www.baidu.com'

8、pytest-html 插件可以生成HTML格式的测试报告。支持测试用例失败 的截图。对于web自动化测试来说非常有用。

运行:pytest 用例路径 --html=./report/result.html            注意:--html= 没有空格。

还可以用main()方法来运行:pytest.main(['当前用例路径','--html=测试报告/XX.html '])

9、pytest-rerunfailures 插件可以在测试用例失败时进行重试。通过‘--reruns 重试次数’来设置测试用例运行失败后的重试次数。

pytest 用例路径 --reruns 3

相对于unittest框架,pytest更适合做UI自动化:1、pytest通过conftest.py文件配置全局浏览器的启动或关闭,整个自动化项目只需要启动或者关闭一次浏览器即可,将节省用例运行时间和开销。2、pytest支持用例运行失败截图。通过pytest-html可以实现,需要在conftest.py配置即可。3、测试用例失败后重跑。UI自动化测试的稳定性是个问题。有很多不可控的因素会导致测试用例的失败,pytest-rerunfailurea可以实现用例重跑。

												

pytest 基本用法的更多相关文章

  1. Pytest测试框架(三):pytest fixture 用法

    xUnit style 结构的 fixture用于初始化测试函数, pytest fixture是对传统的 xUnit 架构的setup/teardown功能的改进.pytest fixture为测试 ...

  2. pytest 常见用法

    前言 之前一篇文章简单介绍了 pytest 以及 fixture :https://www.cnblogs.com/shenh/p/11572657.html .实际在写自动化测试脚本中,还会有一些很 ...

  3. 【Python】【自动化测试】【pytest】【常用命令行选项】

    https://www.cnblogs.com/cnkemi/p/9989019.html http://www.cnblogs.com/cnkemi/p/10002788.html pytest 常 ...

  4. pytest 常用命令行选项(二)

    本文接上篇继续简介pytest常用的命令行选项. 8.-v(--verbose) 选项 使用-v/--verbose选项,输出的信息会更详细.最明显的区别就是每个文件中的每个测试用例都占一行,测试的名 ...

  5. pytest setup和teardown初始化

    用法简介: setup_method:仅作用于class用例集中的用例,置于class内,每个用例都会调用一次 setup_function:作用于独立的def用例,不可作用于class内的用例 se ...

  6. 自动化冒烟测试 Unittest , Pytest 哪家强?

    前言:之前有一段时间一直用 Python Uittest做自动化测试,觉得Uittest组织冒烟用例比较繁琐,后来康哥提示我使用pytest.mark来组织冒烟用例 本文讲述以下几个内容: 1.Uni ...

  7. [Python]使用pytest进行单元测试

    安装pytest pipenv install pytest 验证安装的版本: pytest --version This , imported /site-packages/pytest.py 接下 ...

  8. pytest初始化与清除fixture(二)

    @pytest.fixture用法 1.导入pytest模块:import pytest 2.调用装饰器函数:@pytest.fixture(callable_or_scope=None,*args, ...

  9. python测试框架-pytest

    一.pytest 介绍.运行.参数化和数据驱动.Fixture pytest安装与介绍 官网 : pip install -U pytest 查看版本号:pytest --version 为何选择py ...

随机推荐

  1. Ubuntu18.04开机启动sudo命令

    首先接前文:ubuntu18.04 下启动Android Studio报错KVM is required to run this AVD. /dev/kvm device: permission de ...

  2. centos7创建共享文件夹

    0.检查是否已经安装samba rpm -qi samba 1.未安装,安装samba, 如果已安装,请忽略: yum -y install samba samba-client 2.共享一个目录,使 ...

  3. 前端BOM对象

    location.href 查看当前的url location.href http://www.baidu.com 跳转URL location.reload 重载当前页面 windows.alert ...

  4. 【问题】root账号的UID和GID永远是0吗?

    参考:Does the root account always have UID/GID 0? 这实际上是2个问题 Does the superuser account always have uid ...

  5. python之tkinter入坑Pack()------(1)

    tkinter 的pack()可以设置的属性如下: pack_configure(self, cnf={}, **kw)Pack a widget in the parent widget. Use  ...

  6. _MyBatis3-topic06.07.08.09_ 全局配置文件_引入dtd约束(xml提示)/ 引入properties引用/ 配置驼峰命名自动匹配 /typeAliases起别名.批量起别名

    MyBatis3 的全局配置文件 : Setting -官方文档 笔记要点 出错分析 [Intellij idea配置外部DTD文件] 设置步骤: (同Eclipse中的Catalog设置 ) Fil ...

  7. POI进行导出时候发现有不可读取的内容

    通过后台查询数据,然后使用poi进行导出时候,excel进行打开会出现下面的异常: 但是在WPS中就没有问题, 如果点击否,则不会显示任何内容,点击是,就会弹出来 查看修改记录为: 刚开始也进行了很多 ...

  8. 12 复习 - webpack基本配置1

    1.npm包管理工具 npm init -y 如果创建的项目的根目录名称是中文或者包含中文,不能使用-y npm init 回车时要求你输入包的名称,自己手写项目名称,例test 2.新建src,di ...

  9. git 和 svn比较

    SVN和Git 介绍,区别,优缺点,适用范围总结 原创 2016年01月29日 15:17:36 15774   介绍   SVN SVN是Subversion的简称,是一个开放源代码的版本控制系统, ...

  10. Selenium常用API的使用java语言之17-文件上传

    对于通过input标签实现的上传功能,可以将其看作是一个输入框,即通过sendKeys()指定本地文件路径的方式实现文件上传. 创建upfile.html文件,代码如下: <html> & ...