Pytest的安装和基础使用(一)

Pytest的安装和基础使用

一、pytest的安装

  • pytest 安装pip install pytest,使用5.2.1

二、pytest常用的运行模式介绍

  • test_01.py

    import pytest
    
    def test_b():
        print("测试b函数")
        assert 1
    
    def test_a():
        print("测试a函数")
        assert 0
    
    if __name__ == '__main__':
        pytest.main()
    
2.1 默认参数模式(pytest test_01.py、pytest.main())
  • 默认运行方式—> 终端模式:pytest test_01.py 测试类主函数模式: pytest.main()

    ================================================================================ test session starts ================================================================================
    platform win32 -- Python 3.6.8, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
    rootdir: C:\Users\LGY\Desktop\pytest-learn
    collected 2 items                                                                                                                                                                    
    
    test_01.py .F                                                                                                                                                                  [100%]
    
    ===================================================================================== FAILURES ======================================================================================
    ______________________________________________________________________________________ test_a _______________________________________________________________________________________
    
        def test_a():
            print("测试a函数")
    >       assert 0
    E       assert 0
    
    test_01.py:9: AssertionError
    ------------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------------
    测试a函数
    ============================================================================ 1 failed, 1 passed in 0.03s ============================================================================
    
2.1 -s参数运行模式(pytest -s test_01.py、pytest.main(["-s"])):关闭捕捉,输出打印信息
================================================================================ test session starts ================================================================================
platform win32 -- Python 3.6.8, pytest-5.2.1, py-1.8.0, pluggy-0.13.0
rootdir: C:\Users\LGY\Desktop\pytest-learn
collected 2 items                                                                                                                                                                    

test_01.py 测试b函数
.测试a函数
F

===================================================================================== FAILURES ======================================================================================
______________________________________________________________________________________ test_a _______________________________________________________________________________________

    def test_a():
        print("测试a函数")
>       assert 0
E       assert 0

test_01.py:9: AssertionError
============================================================================ 1 failed, 1 passed in 0.03s ============================================================================


结果分析:跟默认运行相比函数里面的打印信息输出到终端,而默认的运行方式只有错误的测试用例输出在Captured stdout call

2.2 -q参数运行模式(pytest -q test_01.py、pytest.main(["-q"])):减少测试的运行冗长,简化输出
.F                                                                                                                                                                             [100%]
===================================================================================== FAILURES ======================================================================================
______________________________________________________________________________________ test_a _______________________________________________________________________________________

    def test_a():
        print("测试a函数")
>       assert 0
E       assert 0

test_01.py:9: AssertionError
------------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------------
测试a函数
1 failed, 1 passed in 0.04s

2.3 -v参数运行模式(pytest -v test_01.py、pytest.main(["-v"])):增加测试的运行冗长,详细输出
================================================================================ test session starts ================================================================================
platform win32 -- Python 3.6.8, pytest-5.2.1, py-1.8.0, pluggy-0.13.0 -- c:\users\lgy\pytest-env\scripts\python.exe
cachedir: .pytest_cache
rootdir: C:\Users\LGY\Desktop\pytest-learn
collected 2 items                                                                                                                                                                    

test_01.py::test_b PASSED                                                                                                                                                      [ 50%]
test_01.py::test_a FAILED                                                                                                                                                      [100%]

===================================================================================== FAILURES ======================================================================================
______________________________________________________________________________________ test_a _______________________________________________________________________________________

    def test_a():
        print("测试a函数")
>       assert 0
E       assert 0

test_01.py:9: AssertionError
------------------------------------------------------------------------------- Captured stdout call --------------------------------------------------------------------------------
测试a函数
============================================================================ 1 failed, 1 passed in 0.05s ============================================================================


2. 4-k参数运行模式(pytest -k test_01.py、pytest.main(["-k"])):指定执行的测试用例。
  • pytest -k “test_a” :执行当前目前所有包含test_apy(比如test_01.py 、test_02.py 中的所有test_a函数)文件中的测试函数
  • pytest -k “test_a” test_01.py :执行test_01.pytest_a函数
2.5 pytest dir:执行当前目录下的用例

比如pytest -v ./abc:执行abc目录下的执行用例

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值