pythonpytest测试框架(一)

pythonpytest测试框架(⼀)⽬录
⼀、安装
制作智能卡
pytest不是python默认的package,需要⼿动安装。
pytest⽀持python 2.6--3.5之间的版本,同时可以在windows、unix系统上安装
安装⽅式:
pip install pytest
安装完成后,可以查看版本:
pytest --version
增益控制⼆、第⼀个测试例⼦
1.创建test_sample.py⽂件,创建⼀个⽅法、⼀个⽤例
# content of test_sample.py
def func(x):
return x + 1
def test_answer():橡胶衬套
assert func(3) == 5
2.执⾏
$ pytest
=========================== test session starts ============================
platform linux -- , , ,
cachedir: $PYTHON_PREFIX/.pytest_cache
rootdir: $REGENDOC_TMPDIR
collected 1 item
test_sample.py F                                                    [100%]
================================= FAILURES =================================
_______________________________ test_answer ________________________________
def test_answer():
>      assert func(3) == 5
E      assert 4 == 5
E        +  where 4 = func(3)
test_sample.py:5: AssertionError
多媒体互动教学系统========================= 1 failed in 0.12 seconds =========================
此测试返回失败报告,因为func(3)不返回5。
三、pytest参数
1、-K EXPRESSION
执⾏某个关键字的⽤例
⽤例要匹配给出的表达式;使⽤python的语法,匹配的范围是⽂件名、类名、函数名为变量,⽤and来区分如下⾯⼀段测试⽤例
# content of test.py
class TestClass(object):
def test_zne(self):
x = "this"
assert 'h' in x
def test_two(self):
x = "hello"
assert hasattr(x, 'check')
def test_a(self):
assert 1==2
运⾏pytest时带-k参数
pytest -k "test and TestClass and not test_a"  test.py
结果如下:
可以看出,test_a这个⽤例被取消选择了,没有运⾏了
3、--maxfail=num
当错误个数到达给定数时,退出测试,这⾥就不列举实例了,结果与-x类似
4、-m MARKEXPR
只能运⾏有相应标识的测试⽤例,使⽤这个参数,测试⽤例要使⽤@pytest.mark.marker修饰
如下实例
# content of test.py
import pytest猴车
class TestClass(object):
def test_one(self):
'''new_etests'''
x = "this"
assert 'h' in x
@pytest.mark.slow
def test_two(self):
'''new_sssetests'''
x = "hello"
assert hasattr(x, 'check')
def test_a(self):
assert 1==2
teste_two使⽤了@pytest.mark.slow来修饰
在使⽤时,使⽤如下参数
pytest –m slow test.py
结果如下:
从上图中可以看出,只运⾏了⼀个我们带有标识的⽤例。
注意,-m后⾯不能带''号(单引号),只能带“”(双引号),不然识别不到
如果要运⾏多个标识的话,⽤表达式,如下
pytest -m "slow or faster"  运⾏有slow标识或 faster标识⽤例
pytest -m "slow and faster"  运⾏有slow和faster标识的⽤例
pytest -m "slow and not faster"  运⾏有slow和没有faster标识的⽤例
5、 -v, --verbose
详细结果
6、-q, --quiet
极简结果显⽰,简化控制台的输出,可以看出输出信息和之前不添加-q不信息不⼀样, 下图中有两个..点代替了pass结果
7、-s
输⼊我们⽤例中的调式信息,⽐如print的打印信息等,我们在⽤例中加上⼀句 print(driver.title),我们再运⾏⼀下我们的⽤例看看,调试信息输出
8、-V
可以输出⽤例更加详细的执⾏信息,⽐如⽤例所在的⽂件及⽤例名称等
9、--junit-xml=path
输出xml⽂件格式,在与jenkins做集成时使⽤
10、 --result-log=path
将最后的结果保存到本地⽂件中
注意:标黄的是经常使⽤的
四、pytest ⽤例规则
pytest可以在不同的函数、包中发现⽤例,发现的规则如下
⽂件名以test_开头的py⽂件
以test_开头的函数
以Test开头的类
以test_开头的⽅法(与2类似)
要注意的是所有的包必须要有init.py⽂件(在使⽤各种编辑器时会⾃动⽣成)
五、pytest运⾏⽅式
1.单独执⾏某⼀个py⽂件⾥所有的⽤例
pytest test.py
2.执⾏⽬录下所有⽤例
pytest testcase/
3.单独执⾏某个⽤例
以函数形式的⽤例
pytest test_login.py::test_1
以类形式的⽤例
pytest test_login.py::TestClass::test_1
六、pytest fixture
pytest⽀持以xUnit格式型的测试模型(setup/teardown),但还与python⾃带的unittest还是有⼀点差别,如下模块形式----使⽤setup_module/teardown_module
函数形式----使⽤setup_function/teardown_function
类形式----使⽤setup_class/teardown_class
⽅法形式---使⽤setup_method/teardown_method
注意:
1.pytest也可以直接运⾏unittest模式的测试⽤例
2.如果你在pytest模式中使⽤setupClass()函数是不⾏的,不会识别,但如果⽤例类继承之unittest.Testcase,还是可以识别的1、fixture scope的范围参数
pc104总线之前使⽤@pytest.fixture(scope='module')来定义框架,scope的参数有以下⼏种
function  每⼀个⽤例都执⾏
class        每个类执⾏
module    每个模块执⾏(函数形式的⽤例)
session    每个session只运⾏⼀次,在⾃动化测试时,登录步骤可以使⽤该session
2、调⽤fixture的三种⽅法
2.1函数或类⾥⾯⽅法直接传fixture的函数参数名称
from __future__ import print_function
import pytest
@pytest.fixture(scope='module')
def resource_a_setup(request):
print('\nresources_a_setup()')
def resource_a_teardown():
print('\nresources_a_teardown()')
request.addfinalizer(resource_a_teardown)
def test_1(resource_a_setup):
print('test_1()')
def test_2():
print('\ntest_2()')
def test_3(resource_a_setup):
print('\ntest_3()')
使⽤-s -v运⾏查看详情如下
2.2使⽤装饰器@pytest.mark.usefixtures()修饰需要运⾏的⽤例

本文发布于:2024-09-21 22:54:18,感谢您对本站的认可!

本文链接:https://www.17tex.com/tex/4/215898.html

版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。

标签:测试   输出   函数
留言与评论(共有 0 条评论)
   
验证码:
Copyright ©2019-2024 Comsenz Inc.Powered by © 易纺专利技术学习网 豫ICP备2022007602号 豫公网安备41160202000603 站长QQ:729038198 关于我们 投诉建议