自动化测试之Pytest入门

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'])

或者在命令行中输入

 

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。

相关推荐