ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

自动化测试之Pytest入门

2020-09-12 21:00:50  阅读:314  来源: 互联网

标签:入门 Pytest py 1.8 pytest 自动化 test demo8 demo9


pytest是一个功能非常全面的Python自动化测试框架

特点:

1、简单灵活,支持参数化,可以细粒度的控制测试用例;

2、不仅支持简单的单元测试,还支持复杂的功能测试,不仅可以用来做selenium/appium的UI自动化测试,还可以用作做基于Python+requests的接口自动化测试;

3、第三方插件非常丰富,如pytest-selenium(集成了selenium)、pytest-html(完美的html测试报告)、pytest-reunfailures(失败case重复测试)等;

4、测试用例跳跃式(skip)执行以及标记后选择性处理(mark);

5、很好的与CI工具(jenkins)结合;

 

编写规则:

1、测试文件以test_开头,以_test结尾也可以;

2、测试类以Test开头,并且不能带有__init__方法;

3、测试函数(方法)以test_开头;

4、断言使用assert;

 

一、安装pytest

$ pip install pytest

二、执行一个测试

测试通过

# test_demo8.py

def test_pass():
    assert 1 == 1

通过pytest来运行上面测试函数

在pycharm的Terminal中直接输入命令pytest test_demo8.py执行

MacBook-Pro:test_demo fyuanyuan$ pytest test_demo8.py

========================================================================================= test session starts ==========================================================================================
platform darwin -- Python 3.7.2, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
rootdir: /Users/fujinjie/PycharmProjects/test_demo
plugins: html-2.0.1, allure-pytest-2.8.10, metadata-1.8.0
collected 1 item

test_demo8.py . [100%]

========================================================================================== 1 passed in 0.01s ===========================================================================================

执行结果为test_demo8.py测试通过(pytest使用 . 标识测试通过PASSED)

下面使用-v选项来展示测试的详细信息

MacBook-Pro:test_demo fujinjie$ pytest -v test_demo8.py
========================================================================================= test session starts ==========================================================================================
platform darwin -- Python 3.7.2, pytest-5.3.5, py-1.8.1, pluggy-0.13.1 -- /usr/local/bin/python3.7
cachedir: .pytest_cache
metadata: {'Python': '3.7.2', 'Platform': 'Darwin-19.6.0-x86_64-i386-64bit', 'Packages': {'pytest': '5.3.5', 'py': '1.8.1', 'pluggy': '0.13.1'}, 'Plugins': {'html': '2.0.1', 'allure-pytest': '2.8.10', 'metadata': '1.8.0'}}
rootdir: /Users/fujinjie/PycharmProjects/test_demo
plugins: html-2.0.1, allure-pytest-2.8.10, metadata-1.8.0
collected 1 item                                                                                                                                                                                       

test_demo8.py::test_passing PASSED                                                                                                                                                               [100%]

========================================================================================== 1 passed in 0.01s ===========================================================================================

测试失败

# test_demo8.py

def test_fail():
    assert 1 ==2 

同样在命令行执行pytest test_demo8.py命令

MacBook-Pro:test_demo fujinjie$ pytest test_demo8.py
========================================================================================= test session starts ==========================================================================================
platform darwin -- Python 3.7.2, pytest-5.3.5, py-1.8.1, pluggy-0.13.1
rootdir: /Users/fujinjie/PycharmProjects/test_demo
plugins: html-2.0.1, allure-pytest-2.8.10, metadata-1.8.0
collected 1 item                                                                                                                                                                                       

test_demo8.py F                                                                                                                                                                                  [100%]

=============================================================================================== FAILURES ===============================================================================================
_____________________________________________________________________________________________ test_failing _____________________________________________________________________________________________

    def test_fail():
>       assert 1 == 2
E       assert 1 == 2

test_demo8.py:8: AssertionError
========================================================================================== 1 failed in 0.05s ===========================================================================================

测试结果为失败,pytest使用 F 来标识测试失败FAILED

pytest中使用assert来进行断言。

 

三、标记函数

pytest默认查找当前目录下所有以test开始或者结尾的Python文件,并执行其中所有以test开始或结束的函数(方法)

 

run.py

#--coding:utf-8 --
#run.py
import pytest if __name__ == '__main__': pytest.main(['-v'])

test_demo9.py

#--coding:utf-8 --
#test_demo9.py

def test_demo9_func1():
    assert 1 == 1

def test_demo9_func2():
    assert 2 == 2

test_demo10.py

#--coding:utf-8 --
#test_demo10.py

def test_demo10_func3():
    assert 3 == 3

def test_demo10_func4():
    assert 4 == 4

因为run.py/test_demo9.py/test_demo10.py在同一个demo目录下,所以执行run.py时pytest会查找该目录下所有以test开头或结尾的Python文件,并执行文件中所有以test开头或结尾的函数和方法

============================= test session starts ==============================
platform darwin -- Python 3.7.2, pytest-5.3.5, py-1.8.1, pluggy-0.13.1 -- /usr/local/bin/python3.7
cachedir: .pytest_cache
metadata: {'Python': '3.7.2', 'Platform': 'Darwin-19.6.0-x86_64-i386-64bit', 'Packages': {'pytest': '5.3.5', 'py': '1.8.1', 'pluggy': '0.13.1'}, 'Plugins': {'html': '2.0.1', 'allure-pytest': '2.8.10', 'metadata': '1.8.0'}}
rootdir: /Users/fujinjie/PycharmProjects/test_demo/templates/demo
plugins: html-2.0.1, allure-pytest-2.8.10, metadata-1.8.0
collecting ... collected 4 items

test_demo10.py::test_demo10_func3 PASSED                                 [ 25%]
test_demo10.py::test_demo10_func4 PASSED                                 [ 50%]
test_demo9.py::test_demo9_func1 PASSED                                   [ 75%]
test_demo9.py::test_demo9_func2 PASSED                                   [100%]

============================== 4 passed in 0.02s ===============================

由于某些原因,我只想执行test_demo9_func2()测试用例看是否通过,通过pytest有以下几种方法:

方法一,执行时指定函数名,通过 :: 标记

#--coding:utf-8 --
#run.py
import pytest


if __name__ == '__main__':
    pytest.main(['test_demo9.py::test_demo9_func2','-v'])

或者在命令行中输入

 

标签:入门,Pytest,py,1.8,pytest,自动化,test,demo8,demo9
来源: https://www.cnblogs.com/fujinjie/p/13658602.html

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有