pytest笔记: pytest单元测试框架

第一步:安装 和查看版本

pytest笔记: pytest单元测试框架_第1张图片

pycharm settings 查看 

pytest笔记: pytest单元测试框架_第2张图片

 第二步: 编写test_example.py

def  inc(x):
    return  x+1
def test_answer():
    assert inc(4) ==5

第三步:在当前路径下执行pytest 命令

PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example>      pytest
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example
plugins: anyio-3.5.0
collected 1 item                                                                                                                                                     

test_sample.py .                                                                                                                                               [100%]

========================================================================= 1 passed in 0.07s =========================================================================
PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example>      pytest --version
pytest 7.1.2
PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example>

pytest笔记: pytest单元测试框架_第3张图片

test_sample.py  后面带了个“.” 表示通过

def inc(x):
    return x + 1


def test_answer():
    ret = inc(3)
    assert ret == 111

如果不符合预期显示结果如下

PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example> pytest
======================================================================== test session starts ========================================================================
platform win32 -- Python 3.10.9, pytest-7.1.2, pluggy-1.0.0
rootdir: E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example
plugins: anyio-3.5.0
collected 1 item                                                                                                                                                     

test_sample.py F                                                                                                                                               [100%]

============================================================================= FAILURES ==============================================================================
____________________________________________________________________________ test_answer ____________________________________________________________________________

    def test_answer():
        ret = inc(3)
>       assert ret == 1111
E       assert 4 == 1111

test_sample.py:7: AssertionError
====================================================================== short test summary info ======================================================================
FAILED test_sample.py::test_answer - assert 4 == 1111
========================================================================= 1 failed in 0.40s =========================================================================
PS E:\data\web测试\Selenium3自动化测试实战——基于Python语言\mycode\pytest_example>

你可能感兴趣的:(Python自动化,pytest,笔记,单元测试,python)