pytest 基本用法

摘要:
returna+bdefis_prime(n):=16deftest_add_3()!'''测试包含'''a='hello'b='he'assertbinadeftest_not_in():'''测试不包含'''a='hello'b='hi'assertbnotinadeftest_true_1():

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 基本用法》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇keycloak~自定义rest接口10款最好用的移动web开发工具下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

jQuery attr style 选择器在不同浏览器中的表现

  今天写了段JQuery选择器代码,为了找到Style 中含有display:none的元素,本以为很简单的事情,但在不同浏览器中运行时,就出现了问题。   Html 代码:      使用下面的选择器语句在谷歌中正常,但IE中获取不到正确的结果,怀疑写法在IE中不兼容,换了各种写法都不行,网上查找一番,无解。 $("td:not([style*='di...

使用Kubeflow构建机器学习流水线

在此前的文章中,我已经向你介绍了Kubeflow,这是一个为团队设置的机器学习平台,需要构建机器学习流水线。 在本文中,我们将了解如何采用现有的机器学习详细并将其变成Kubeflow的机器学习流水线,进而可以部署在Kubernetes上。在进行本次练习的时候,请考虑你该如何将现有的机器学习项目转换到Kubeflow上。 我将使用Fashion MNIST作...

maven跳过单元测试-maven.test.skip和skipTests的区别

-DskipTests,不执行测试用例,但编译测试用例类生成相应的class文件至target/test-classes下。 -Dmaven.test.skip=true,不执行测试用例,也不编译测试用例类。 一 使用maven.test.skip,不但跳过单元测试的运行,也跳过测试代码的编译。 mvn package -Dmaven.test.skip=...

python连接mysql并进行增删改查操作

查询了菜鸟教程以及博客园等各种文章,有修改完善 转载自https://www.cnblogs.com/yuhou/p/10893056.html 自己完善部分: python连接mysql使用连接池: 一、为什么要用连接池? 1、数据库 本身有压力,并不能创建太多的并发数访问数据库,如果是大表那更加会有压力,因此限制一定的连接是更加科学的方法。2、创建和释...

SendMessage发送WM_COMMAND消息控制另一个程序的某一个按钮

procedure TfrmMain.btnSendClick(Sender: TObject); var hCalc, h1: Cardinal; begin WinExec('calc', SW_SHOWNORMAL);//运行计算器 hCalc := FindWindow('SciCalc', nil);...

pytest之多进程和多线程

若分布式执行用例,用例设计必须遵循以下原则: 1.用例之间都是独立的(不存在依赖关系); 2.用例执行无先后顺序要求; 一、 pytest-xdist多进程pytest-xdist仅支持多进程,不支持多线程。 安装:pip install pytest-xdist 常用参数解析:  -n:进程数,也就是cpu个数。可以指定个数,最大值为当前机器cpu个数,...