zoukankan      html  css  js  c++  java
  • pytest参数

    1、安装 pip install pytest

    2、pytest默认规则

    - 读取以test开头文件夹、文件,函数作为识别函数对象

    - 控制台不显示打印

    - pytest.main(['参数1','参数2'])

    import pytest
    def test_01():
        print("test01")
    
    def test_02():
        print("test02")
    
    if __name__ == '__main__':
        pytest.main()
    

    - 如果加 -s 就能看见,测试案例print打印内容

    pytest.main() 加参数必须是列表

    import pytest
    def test_01():
        print("test01")
    
    def test_02():
        print("test02")
    
    if __name__ == '__main__':
        pytest.main(['-s'])
    

     -v是详情

    import pytest
    def test_01():
        print("test01")
    
    def test_02():
        print("test02")
    
    if __name__ == '__main__':
        pytest.main(['-v'])
    

      

     - q 是简化报告

    import pytest
    def test_01():
        print("test01")
    
    def test_02():
        print("test02")
    
    if __name__ == '__main__':
        pytest.main(['-q'])
    

      

     -x :用例失败时立即停止测试,一共3条案例,第二条案例是失败,第三条案例停止执行

    import pytest
    def test_01():
        print("我是test01成功的案例")
    
    def test_02():
        assert 1==0
        print("我是test02失败的案例")
    
    def test_03():
        print("我是test03成功的案例")
    
    if __name__ == '__main__':
        pytest.main(['-x'])
    

      

     --maxfail=num 用例运行时 允许的最大失败次数,超过则立即停止。原来应该执行3条案例,但 --maxfail=2,前2条失败了,第3条就不执行。

    import pytest
    class Testdemo(object):
        def test_01(self):
            assert 1 == 3
            print("我是test01成功的案例")
    
        def test_02(self):
            assert 1==0
            print("我是test02失败的案例")
    
        def test_03(self):
            assert  1==2
            print("我是test03成功的案例")
    
    if __name__ == '__main__':
        pytest.main(["--maxfail=2"])
    

      

     -k :只运行与给定字符串表达式匹配的测试用例

       可以跟类名,可以跟方法名,可以跟类名 and not 方法名

    跟方法名,只执行第2条案例

    import pytest
    def test_01():
        print("我是test01成功的案例")
    
    def test_02():
        assert 1==0
        print("我是test02失败的案例")
    
    def test_03():
        print("我是test03成功的案例")
    
    if __name__ == '__main__':
        pytest.main(['-k test_02'])
    

      跟类名,3条案例全部执行

    import pytest
    class Testdemo(object):
        def test_01(self):
            print("我是test01成功的案例")
    
        def test_02(self):
            assert 1==0
            print("我是test02失败的案例")
    
        def test_03(self):
            print("我是test03成功的案例")
    
    if __name__ == '__main__':
        pytest.main(['-k Testdemo'])
    

      类名 and not 方法名,执行整个类下面的所有案例,但test_03除外

       -k Testdemo and not test*  代表执行这个类下面全部案例,但以test开头除外
    import pytest
    class Testdemo(object):
        def test_01(self):
            print("我是test01成功的案例")
    
        def test_02(self):
            assert 1==0
            print("我是test02失败的案例")
    
        def test_03(self):
            print("我是test03成功的案例")
    
    if __name__ == '__main__':
        pytest.main(['-k Testdemo and not test_03'])
    

      

    参数:--tb=选项(选项:'auto', 'long', 'short', 'no', 'line', 'native')

    用例运行失败时,展示错误的详细程度

    import pytest
    class Testdemo(object):
        def test_01(self):
            assert 1 == 3
            print("我是test01成功的案例")
    
        def test_02(self):
            assert 1==0
            print("我是test02失败的案例")
    
        def test_03(self):
            assert  1==2
            print("我是test03成功的案例")
    
    if __name__ == '__main__':
        pytest.main(["-k test_01","--tb=no"])
    

      

    -l 或--showlocals

    用例运行失败时,打印相关的局部变量,pytest -l

    import pytest
    class Testdemo(object):
        def test_01(self):
            a=1
            b=2
            assert 6 == (a+b)
            print("我是test01成功的案例")
    
        def test_02(self):
            assert 1==0
            print("我是test02失败的案例")
    
        def test_03(self):
            assert  1==2
            print("我是test03成功的案例")
    
    if __name__ == '__main__':
        pytest.main(["-k test_01","-l"])
    

      

    --lf / --last-failed

    只执行上次执行失败的测试

    --ff / --failed-first

    先执行完上次失败的测试后,再执行上次正常的测试

    -- 运行指定的函数(使用两对冒号 :: 分隔)

    pytest 模块名::类名::函数名

    文件名 test_case_01.py 

    class Testdemo2(object):
        @pytest.mark.parametrize("data", [("zhangsan")])
        @pytest.mark.usefixtures("fix_a")
        def test_01(self,data):
            print(data)
    
        @pytest.mark.parametrize("data", [("lisi")])
        def test_02(self,data):
            print(data)
    
    if __name__ == '__main__':
        pytest.main(["-v", "test_case_01.py::Testdemo2::test_02"])
    

    -h 查看 pytest 的所有选项

    -r option 生成简略的指定需求的报告

    option f E s x X P  p a A
    Des failed error xkipped xfailed xpassed passed passed with output all except pP all

    pytest --durations=10 获取执行最慢的10个测试用例

    -vv 展示时间小于0.01s的用例

    上班求生存,下班求发展
  • 相关阅读:
    hdu 3333 树状数组+离线处理
    poj 2352 树状数组 OR Treap
    hdu 1698 线段树
    【概率dp】D. Card Collector
    【分段哈希】H. Paint the Wall
    【置换】G. Poker 2.0
    【概率dp】C. Race to 1 Again
    【dp】D. Caesar's Legions
    【并查集】F.find the most comfortable road
    【算法系列学习】连续邮资问题
  • 原文地址:https://www.cnblogs.com/ljf520hj/p/15423282.html
Copyright © 2011-2022 走看看