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. 【vue开发】超简单的防止连续点击js指令方法

    vue防重复点击(指令实现) 快速点击按钮会重复多次调用接口,防止出现这样的情况 全局定义,方便调用 新建plugins.js export default { install (Vue) { // ...

  2. Django 启动报错 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xc7

    pycharm 报错 cmd 报错 解决办法 首先 是计算机 编码问题  是 django 读取你的  用户host名 但是 windos 用户名 如果是中文 就会报这个错  要改成 英文

  3. javascript_13-函数是一种数据类型

    函数是一种数据类型 函数是一种数据类型 function var num =60; // 1 函数是一种数据类型 function var myFun = function(){ console.lo ...

  4. Interval 用法总结

    语法:INTERVAL 'integer [- integer]' {YEAR | MONTH} [(precision)][TO {YEAR | MONTH}] 该数据类型常用来表示一段时间差, 注 ...

  5. HTTP协议通信原理 与常见报错信息

    HTTP协议通信原理 请求报文 请求行 GET index.html HTTP 1.1 请求方法:get  读取服务器数据内容    post   提交存储服务端数据(用户注册) 协议版本:   ht ...

  6. 深入理解JVM内幕:从基本结构到Java 7新特性[转]

    英文原文:cubrid,编译:ImportNew - 朱伟杰 译文链接:http://www.importnew.com/1486.html [如需转载,请在正文中标注并保留原文链接.译文链接和译者等 ...

  7. 如何理解MVVM

    说一下对MVVM的理解 MVC Model,View,Controller.   View是视图,界面,有输入框,有按钮,有列表等. Model是数据源,比如todolist里面等title,list ...

  8. Codeforces 750 E New Year and Old Subsequence

    E. New Year and Old Subsequence 思路:线段树维护矩阵乘法. 代码: #pragma GCC optimize(2) #pragma GCC optimize(3) #p ...

  9. Linux常用命令,及网络测试命令

    出处:https://i.linuxtoy.org/files/pdf/fwunixref.pdf ======================================= 1. ifconfi ...

  10. Vmware Pro 14报错:无法连接 MKS: 套接字连接尝试次数太多;正在放弃。

    软件环境: 虚拟机软件:VMware Pro 14 母机操作系统:win7 客户机操作系统:CentOS 7     问题详情: 报错:无法连接 MKS: 套接字连接尝试次数太多:正在放弃.     ...