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. C#导出和导入Excel模板功能

    引用  Aspose.Cells; 基于WinForm 导入 private void btn_excel_input_Click(object sender, EventArgs e) { try ...

  2. CentOS 7的Linux系统优化加固

    1.关闭selinux 2.关闭防火墙 3.关闭NetworkManager 4.为系统运维管理员创建普通用户,并配置sudo(vi  sudo) 5.清空泄漏系统版本信息的文件 6.基础优化sshd ...

  3. 记录一下JProfiler的使用

    刚入职实习,第四天了,昨晚老大安排我在公司机器上装个JProfiler看一情况. 然后网上都是什么跟tomcat一起使用的,所以折腾了很久才搞出来. 我这里没用什么服务器,因为公司用的是Play!框架 ...

  4. servlet版本与tomcat版本对应关系,各版本web.xml头信息写法

    The mapping between the specifications and the respective Apache Tomcat versions is: Servlet Spec JS ...

  5. 2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线)

    2018 ACM 国际大学生程序设计竞赛上海大都会赛重现赛 F Color it (扫描线) 链接:https://ac.nowcoder.com/acm/contest/163/F来源:牛客网 时间 ...

  6. JDBC课程5--利用反射及JDBC元数据(ResultSetMetaData)编写通用的查询方法

    /**-利用反射及JDBC元数据编写通用的查询方法 * 1.先利用SQl语句进行查询,得到结果集--> * 2.查找到结果集的别名:id--> * 3.利用反射创建实体类的对象,创建aut ...

  7. 【最短路+较复杂处理】PAT-L3-005. 垃圾箱分布

    L3-005. 垃圾箱分布 大家倒垃圾的时候,都希望垃圾箱距离自己比较近,但是谁都不愿意守着垃圾箱住.所以垃圾箱的位置必须选在到所有居民点的最短距离最长的地方[此处为第一重排序选择的条件],同时还要保 ...

  8. 修改git提交的用户名和密码

    目的:每个项目自定义Git提交的用户名和邮箱 1.在本地找到某个项目所在的地址 2.找到config文件 3.增加如下配置 4. ok,这样每个项目都可以定义提交者姓名和邮箱了

  9. Java中实例方法和类方法的区别举例

    QAQQAQAQQQAQQAQQAQAQ import java.util.ArrayList; import java.util.Iterator; class myclass{ ; ; publi ...

  10. 51nod P1354 选数字 题解

    每日一题 day8 打卡 Analysis 背包+离散化 这题是我们一次模拟赛的T2,结果我的暴力全TLE了. 关键是如果将两个因数的乘积离散化在因数数组中之后等于这个乘积本身,说明a[j]*in离散 ...