zoukankan      html  css  js  c++  java
  • pytest(3):pytest运行参数介绍

    前言

    pytest 带有很多参数,可以使用 pytest --help  来查看帮助文档,下面介绍几种常用的参数:  

    无参数

    读取路径下所有符合规则的文件,类,方法,函数全部执行。使用方法如下:

     pytest 或者 py.test 

    -v 参数

    打印详细运行日志信息,一般在调试的时候加上这个参数,终端会打印出每条用例的详细日志信息,方便定位问题。使用方法如下:

     pytest -v 

    不加-v和加-v的对比:

     -s 参数

    带控制台输出结果,当你的代码里面有 print 输出语句,如果想在运行结果中打印 print 输出的代码,在运行的时候可以添加 -s 参数,一般在调试的时候使用,使用方法如下: pytest -s  

    -k 参数

    跳过运行某个或者某些用例。

    使用方法如下:

    pytest -k '类名' 
    pytest -k '方法名' 
    pytest -k "类名 and not 方法名" //运行类里所有的方法,不包含某个方法
     1 # Author xuejie zeng
     2 # encoding utf-8
     3 #测试demo
     4 
     5 import pytest
     6 
     7 class Testone:
     8     def test_a(self):
     9         print("我是第一条用例")
    10     def test_b(self):
    11         print("我是第二条用例")
    12     def c(self):
    13         print("c")
    14 
    15 class Testtwo:
    16     def test_d(self):
    17         print("我是第三条用例")
    pytest -k '类名',运行指定的类名下的用例 

    pytest -k '方法名',运行指定的用例

    pytest -k "类名 and not 方法名" ,只运行了类里面的第二条用例,不包括第一条用例。

    -x 参数

    遇到用例失败立即停止运行,一旦发现有失败的用例即中止运行

    使用方法如下:

     pytest -x 
    # Author xuejie zeng
    # encoding utf-8
    
    
    class Testone:
        def test_a(self):
            print("我是第一条用例")
            a = 1
            b = 2
            assert a == b
        def test_b(self):
            print("我是第二条用例")
            a = 1
            b = 1
            assert a == b
        def c(self):
            print("c")
    
    class Testtwo:
        def test_d(self):
            print("我是第三条用例")
            a = 10
            b = 20
            assert a != b

     用例遍历到了3条,但是由于第一条就失败了所以后面的2条就不执行了。

    --maxfail 参数

    用例失败个数达到阀值停止运行。具体用法: 

    pytest --maxfail=[num]  当失败数等于设置的阈值时,后面的2条用例就不执行了。

     1 (venv) D:Toolspycharm_2020New_2020pytest_doc	est>pytest --maxfail=1 testdemo.py
     2 
     3 collected 3 items                                                                                                                                         
     4 
     5 testdemo.py F
     6 
     7 ======================================================================== FAILURES ========================================================================
     8 _____________________________________________________________________ Testone.test_a _____________________________________________________________________
     9 
    10 self = <pytest_doc.test.testdemo.Testone object at 0x000001AEB0EB0EB8>
    11 
    12     def test_a(self):
    13         print("我是第一条用例")
    14         a = 1
    15         b = 2
    16 >       assert a == b
    17 E       assert 1 == 2
    18 
    19 testdemo.py:12: AssertionError
    20 ------------------------------------------------------------------ Captured stdout call ------------------------------------------------------------------
    21 我是第一条用例
    22 ================================================================ short test summary info =================================================================
    23 FAILED testdemo.py::Testone::test_a - assert 1 == 2
    24 !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! stopping after 1 failures !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
    25 =================================================================== 1 failed in 0.05s ====================================================================

    当失败数小于设置的阈值时,后面的2条用例继续执行。

     1 (venv) D:Toolspycharm_2020New_2020pytest_doc	est>pytest --maxfail=2 testdemo.py
     2 
     3 collected 3 items                                                                                                                                         
     4 
     5 testdemo.py F..                                                                                                                                     [100%]
     6 
     7 ======================================================================== FAILURES ========================================================================
     8 _____________________________________________________________________ Testone.test_a _____________________________________________________________________
     9 
    10 self = <pytest_doc.test.testdemo.Testone object at 0x000001DD0EADFEF0>
    11 
    12     def test_a(self):
    13         print("我是第一条用例")
    14         a = 1
    15         b = 2
    16 >       assert a == b
    17 E       assert 1 == 2
    18 
    19 testdemo.py:12: AssertionError
    20 ------------------------------------------------------------------ Captured stdout call ------------------------------------------------------------------
    21 我是第一条用例
    22 ================================================================ short test summary info =================================================================
    23 FAILED testdemo.py::Testone::test_a - assert 1 == 2
    24 ============================================================== 1 failed, 2 passed in 0.05s ===============================================================

    -m 参数

    将运行有 @pytest.mark.[标记名] 这个标记的测试用例。

    使用方法如下:

     pytest -m [标记名]   
     1 # Author xuejie zeng
     2 # encoding utf-8
     3 
     4 
     5 import pytest
     6 
     7 class Testone:
     8     def test_a(self):
     9         print("我是第一条用例")
    10         a = 1
    11         b = 2
    12         assert a == b
    13     def test_b(self):
    14         print("我是第二条用例")
    15         a = 1
    16         b = 1
    17         assert a == b
    18 
    19 class Testtwo:
    20     @pytest.mark.d
    21     def test_d(self):
    22         print("我是第三条用例")
    23         a = 10
    24         b = 20
    25         assert a != b

     用例遍历到了3条用例,2条被忽略,1条被选中,也就是加了标记的被选中了。

    可以关注个人公众号:测试开发进阶之路

  • 相关阅读:
    构建智慧城市的五个关键点
    构建智慧城市的五个关键点
    构建智慧城市的五个关键点
    构建智慧城市的五个关键点
    微软Project Online落地中国
    微软Project Online落地中国
    微软Project Online落地中国
    微软Project Online落地中国
    美团容器平台架构及容器技术实践
    Python爬虫入门教程 30-100 高考派大学数据抓取 scrapy
  • 原文地址:https://www.cnblogs.com/zengxuejie/p/13667724.html
Copyright © 2011-2022 走看看