zoukankan      html  css  js  c++  java
  • pytest ---- 用例执行

    mark中的xfail(失败)

    pytest.xfail()
    我们已经掌握了如果跳过执行测试用例,其中有一种方法是在测试函数中用pytest.skip()方法。我们现在要学的pytest.xfail()和pytest.skip()有些相似,只不过他的含义是:将该用例标记成xfail失败,并且该用例中的后续代码不会执行

    我们在测试用例中调用pytes.xfail()方法,可以选择传入reason参数表示原因。

    import allure
    import pytest
    import os
    
    class TestClass:
        def test_one(self):
            x = "this"
            assert 'h' in x
    
        def test_two(self):
            x = "hello"
            print(x)
            pytest.xfail(reason='该功能尚未完成')
            assert hasattr(x, 'check')
    
        def test_three(self):
            a = "hello"
            b = "hello world"
            assert a in b
    
    if __name__ == "__main__":
        pytest.main(['-s', "--alluredir=./reports/allure"])
    # 使用os模块等待allure测试报告生成后,进行进化测试报告
        os.system("allure generate./reports/allure -o ./reports/html --clean")
    

      

    运行结果如下图:我们可以看到该用例中pytest.xfail()方法之前的代码运行了,之后的不再运行;结果中有一天用例被标记为xfail。

    ============================= test session starts =============================
    platform win32 -- Python 3.9.0, pytest-6.2.3, py-1.10.0, pluggy-0.13.1
    rootdir: D:
    plugins: allure-pytest-2.8.40, html-3.1.1, metadata-1.11.0
    collected 3 items

    ........pycharm_projectspytest_studyscripts est_feature.py .hello
    x
    测试已忽略..

    ======================== 2 passed, 1 xfailed in 0.20s =========================

    这个方法是我们直接将用例标记为失败,那什么情况我们会这么做呢?功能未完成、已知有问题。除此之外,就是用例的执行需要前置条件或操作,如果前置条件或操作失败,那么我们就可以直接将该用例设为失败,也就是xfail。

    @pytest.mark.xfail
    除了上面学习的pytest.xfail(),xfai还有一种使用方法。就是@pytest.mark.xfail标签,他的含义是期望测试用例是失败的,但是不会影响测试用例的的执行。如果测试用例执行失败的则结果是xfail(不会额外显示出错误信息);如果测试用例执行成功的则结果是xpass

    import allure
    import pytest
    import os
    
    class TestClass:
        def test_one(self):
            x = "this"
            assert 'h' in x
        @pytest.mark.xfail
        def test_two(self):
            x = "hello"
            print(x)
            assert hasattr(x, 'check')
    
        def test_three(self):
            a = "hello"
            b = "hello world"
            assert a in b
    
    if __name__ == "__main__":
        pytest.main(['-s', "--alluredir=./reports/allure"])
    # 使用os模块等待allure测试报告生成后,进行进化测试报告
        os.system("allure generate./reports/allure -o ./reports/html --clean")
    

    文件名类名方法执行部分用例

    -v 指定的函数节点id
    如果想指定运行某个.py模块下,类里面的一个用例,如:TestClass里面testmethod用例
    每个test开头(或_test结尾)的用例,函数(或方法)的名称就是用例的节点id,指定节点id运行用-v 参数

    pytest -v test_server.py::TestClass::test_method
    

    选择运行整个class

     pytest -v test_server.py::TestClass
    

    选择多个节点运行,多个节点中间空格隔开

    pytest -v test_server.py::TestClass test_server.py::test_send_http
    

      

    -k 组合调用执行部分用例

     -k 匹配用例名称
    可以使用-k命令行选项指定在匹配用例名称的表达式

    pytest -v -k http
    

      

    运行所有的测试,根据用例名称排除掉某些用例:

    pytest -k “not send_http” -v
    

      

    同时选择匹配 “http” 和“quick”

    pytest -k “http or quick” -v
    

      

      

    使用自定义标记mark只执行部分用例

     mark标记
    以下用例,标记test_send_http()为webtest

    import pytest
    
    @pytest.mark.webtest
    def  test_send_http():
        print("test_send_http")
        assert 0
    
    
    def test_something_quick():
        print("test_something_quick")
        pass
    def  test_another():
        print("test_another")
        pass
    class TestClass:
        def test_method(self):
            print("test_method")
            pass
    
    if __name__ == "__main__":
       # pytest.main(['-s',"test_three.py","-m=webtest"])
        pytest.main() 

    执行cmd命令:

    pytest -v -m "webtest"   
    pytest -v -m "not webtest"
    

      

     

  • 相关阅读:
    CentOS7的内核优化
    centos7 系统优化脚本
    Centos7之系统优化
    Jenkins安装
    Zabbix安装
    Systemd 入门教程:命令篇
    开源ERP系统Odoo搭建文档
    SSH详解
    使用pm2来保证Spring Boot应用稳定运行
    npm安装与使用
  • 原文地址:https://www.cnblogs.com/saryli/p/14657823.html
Copyright © 2011-2022 走看看