zoukankan      html  css  js  c++  java
  • pytest测试框架简介(一)

    pytest是python的一种单元测试框架,与unittest测试框架类似,但是比unittest框架使用起来更加简洁,效率更高.

    安装pytest: pip install pytest

    框架使用流程

    创建如下源码文件test_1.py

    def add(x, y):
        return x+y
    
    def test_add():
        assert add(1, 2) == 3

    运行pytest文件,三种命令运行方式

    1: 直接运行 pytest

    2: 直接运行 py.test

    3:直接运行  python -m pytest

     pytest运行规则

    1:查找当前目录及其子目录下以test_*.py或*_test.py文件

    2:找到文件后,在文件中找到以test开头函数并执行

    所以在创建pytest文件时必须要以test开头或者结尾,函数要以test开头。

    执行单个测试文件: pytest   test**.py(文件名称)

    用例设计原则

    文件名以test_*.py或*_test.py

    测试类以Test开头,并且不能带init方法

    以Test_开头的类

    以test_开头的方法

    所有的包pakege必须要有__init__.py文件

    断言使用assert

    运行.py模块里面的任一函数

    pytest test_mod.py::test_func

    运行.py模块里面,测试类里面的任一方法

    pytest test_mod.py::TestClass::test_method

    断言

    assert XX         判断XX为真

    assert not XX   判断XX不为真

    assert a  in b   判断b包含a

    assert a ==b   判断a等于b

    assert a != b   判断a等于b

    装饰器

    表达式:    pytest -m slow

    将运行用@pytest.mark.slow装饰器

    import pytest
    
    def add(x, y):
        return x+y
    
    def test_add_1():
        assert add(1, 2) == 3
    
    @pytest.mark.demo1
    def test_add_2():
        assert add(1, 2) == 3
    
    @pytest.mark.demo1
    def test_add_3():
        assert add(1, 2) == 3

    命令执行: pytest -m demo1 

     只会执行test_add_2 和 test_add_3 其它的不会执行

    skip: 跳过一些用例不执行

    @pytest.mark.skip(reason="skip")

    import pytest
    
    def add(x, y):
        return x+y
    
    def test_add_1():
        assert add(1, 2) == 3
    
    @pytest.mark.skip(reason="不执行")
    def test_add_2():
        assert add(1, 2) == 3
    
    def test_add_3():
        assert add(1, 2) == 3

     skipif: 特殊情况下跳过该测试用例

    import pytest
    import sys
    def add(x, y):
        return x+y
    
    def test_add_1():
        assert add(1, 2) == 3
    
    @pytest.mark.skipif(sys.version_info < (3, 8), reason="require python3.8")
    def test_add_2():
        assert add(1, 2) == 3
    
    def test_add_3():
        assert add(1, 2) == 3

    对较大的测试套件,通常最好有一个文件来定义标记,然后一致适用于整个测试套件

    import pytest
    import sys
    
    minversion = pytest.mark.skipif(sys.version_info < (3, 8), reason="require python3.8")

    共享之前需要先导入minversion

    from test_module import minversion

    pytest.skip(reason=" ")函数

    pytest.skipi()放到函数里面,只跳过该函数

    import pytest
    
    def add(x, y):
        return x+y
    
    def test_add_1():
        pytest.skip("not support")
        assert add(1, 2) == 3
    
    def test_add_2():
        assert add(1, 2) == 3
    
    def test_add_3():
        assert add(1, 2) == 3

    pytest.skip()放到函数外边,跳过整个文件

    import pytest
    def add(x, y): return x+y 
    pytest.skip("not support")def test_add_1():
        assert add(1, 2) == 3
    
    def test_add_2():
        assert add(1, 2) == 3
    
    def test_add_3():
        assert add(1, 2) == 3
  • 相关阅读:
    Visual Studio Code 工具使用教程
    JS获取节点的兄弟,父级,子级元素
    动态加载js
    实用的文本和图片无缝滚动效果
    兼容各种浏览器的文字循环无缝滚动效果
    中文乱码
    js 循环json
    js中获取basePath
    json转换为map
    java des 加密/解密
  • 原文地址:https://www.cnblogs.com/zhouzetian/p/13585033.html
Copyright © 2011-2022 走看看