pytest测试框架(1)

目录

1、安装Pytest

2、Pytest规则

3、测试固件

4、断言

5、测试执行


1、安装Pytest

1.1、打开终端;

1.2、使用清华大学的镜像源,其他源的也行;

执行命令行: pip3 install -i https://pypi.tuna.tsinghua.edu.cn/simple  pytest

1.3、终端中执行:pip3 show pytest 或者pytest --version 查看版本号;

2、Pytest规则

 2.1、文件命名:默认以“test_”开头或者以“_test”结尾(和unittest有差别,unittest默认以“test”开头或结尾);

2.2、测试类(class)命名:默认以”Test”开头;

2.3、测试方法(函数)命名:默认以”test_”开头;

2.4、断言:直接使用Pytest语言的断言;

2.5、示例代码1(class风格的代码):测试问价名:test_1.py

import pytest

class Test_1(object):

    def test_01(self):
        print('测试用例: 1')
        assert '1' == '1'

    def test_02(self):
        print('测试用例: 2')
        assert 2 == 2

if __name__ == '__main__':
    pytest.main(["-s", "test_01.py"])

2.6、执行结果为:

 和unit test一样“.”表述断言成功,“F”表示断言失败;如下图:第二个用例断言失败

2.6、示例代码2(函数风格代码):(可以不把测试例放到class中;例文件test_02.py:

import pytest


def test_01():
    print('测试用例: 1')
    assert '1' == '1'


def test_02():
    print('测试用例: 2')
    assert 2 == 2


if __name__ == '__main__':
    pytest.main(["-s"])

 2.7、执行结果如下:(指定文件名)

2.8、执行结果如下:(不指定文件名)

3、测试固件

3.1、测试固件分类及使用场景

3.1、setup_module/teardown_module : 用于模块的始末,只执行一次,是个全局方法:

3.2、setup_function/teardown_function : 只对函数生效,不用在类中;

3.3、setup_class/teardown_class:在类中应用,在类开始和结束时执行;

3.4、setup_method/teardown_method:在累的方法开始和结束处执行;

3.5、setup/teardown:在调用方法的前后执行;(pytest-7.2.0版本中不可用不确定是否是版本的原因

3.2、使用场景示例

1、函数中的测试固件

setup_module/teardown_module : 用于模块的始末,只执行一次,是个全局方法:

setup_function/teardown_function : 只对函数生效,不用在类中;

1.1、函数样例:测试文件:test_03.py

import pytest

"""
函数中的测试固件
setup_module/teardown_module : 用于模块的始末,只执行一次,是个全局方法:
setup_function/teardown_function : 只对函数生效,不用在类中;
"""

def setup_module():
    print("setup_module")

def teardown_module():
    print("teardown_module")

def setup_function():
    print("setup_function")

def teardown_function():
    print("teardown_function")

def test_01():
    print('测试用例: 1')


def test_02():
    print('测试用例: 2')

if __name__ == '__main__':
    pytest.main(["-s", "./test_03.py"])

1.2、执行结果:

 

 2、class中的测试固件

setup_class/teardown_class:在类中应用,在类开始和结束时执行;

setup_method/teardown_method:在累的方法开始和结束处执行;

2.1、示例代码;测试文件:test_04.py

import pytest

"""
class中的测试固件
setup_class/teardown_class:在类中应用,在类开始和结束时执行;
setup_method/teardown_method:在累的方法开始和结束处执行;
"""

class Test_01():
    def setup_class(self):
        print("setup_class")

    def teardown_class(self):
        print("teardown_class")

    def setup_method(self):
        print("setup_method")

    def teardown_method(self):
        print("teardown_method")

    def test_01(self):
        print('测试用例: 1')

    def test_02(self):
        print('测试用例: 2')

if __name__ == '__main__':
    pytest.main(["-s", "./test_04.py"])

2.2、执行结果:

3、setup和teardown

pytest-7.2.0中不可用,在网上查资料看有的博主的博文中有可以使用的分享我这里也做了下尝试

setup/teardown:在调用方法的前后执行;

3.1、setup和teardown既可以应用在函数中,也可以应用在class中,作用对象是函数或方法,且均为小写字母;

3.2、在函数中的应用,示例代码:测试文件:test_05.py

import pytest

"""
函数中的测试固件
setup_module/teardown_module : 用于模块的始末,只执行一次,是个全局方法:
setup/teardown:在调用方法的前后执行;
"""

def setup_module():
    print("setup_module")

def teardown_module():
    print("teardown_module")

def setup():
    print("setup")

def teardown():
    print("teardown")

def test_01():
    print('测试用例: 11')


def test_02():
    print('测试用例: 22')

if __name__ == '__main__':
    pytest.main(["-s", "./test_05.py"])

执行结果: 

3.3、在class中的应用,示例代码:测试文件:test_06.py,

可以执行但会有报错打印提示pytest自动对setup和teardown做了修改

import pytest

"""
class中的测试固件
setup_class/teardown_class:在类中应用,在类开始和结束时执行;
setup/teardown:在调用方法的前后执行;
"""

class Test_01():
    def setup_class(self):
        print("setup_class")

    def teardown_class(self):
        print("teardown_class")

    def setup(self):
        print("setup")

    def teardown(self):
        print("teardown")

    def test_01(self):
        print('测试用例: 1')

    def test_02(self):
        print('测试用例: 2')

if __name__ == '__main__':
    pytest.main(["-s", "./test_06.py"])

 执行结果:

4、断言

4.1、pytest和unittest断言对比

unittest是有专门的断言方法,而pytest直接使用assert关键字进行断言,相对于unittest更加灵活一些;

4.2、示例代码,测试文件:test_07.py

import pytest

class Test_01():

    def setup_class(self):
        print("setup_class")

    def teardown_class(self):
        print("teardown_class")

    def setup_method(self):
        print("setup_method")

    def teardown_method(self):
        print("teardown_method")

    def test_01(self):
        print('测试用例: 1')
        assert 3 < 5   # python 比较运算符

    def test_02(self):
        print('测试用例: 2')
        assert "shell" in "shell_11"  # 成员运算符

if __name__ == '__main__':
    pytest.main(["-s", './test_07.py'])

执行结果:

5、测试执行

5.1、使用main函数执行

  1. 使用pytest.main([“-s”])执行同级和下级目录所有符合条件的测试用例(和unittest不同);
  2. 创建文件夹demo2,里面存放3个测试文件(test_01.py、test_02.py、file_3.py(不符合命名格式))和一个目录demo2_2里面存放3个测试文件(test_1_1.py、test_1_2.py)          test_01.py代码:
import pytest


def test_01():
    print('测试用例: 1')
    assert '1' == '1'


def test_02():
    print('测试用例: 2')
    assert 2 == 2


if __name__ == '__main__':
    pytest.main(["-s"])

  test_02.py代码:

import pytest


def test_01():
    print('测试用例: 3')
    assert '1' == '1'


def test_02():
    print('测试用例: 4')
    assert 2 == 2


if __name__ == '__main__':
    pytest.main(["-s", "./test_02.py"])

file_3.py代码:(文件名不符合规范,内部函数其中一个不符合规范

import pytest


def file_01():
    print('测试用例: file_1')
    assert '1' == '1'


def test_02():
    print('测试用例: test_02')
    assert 2 == 2


if __name__ == '__main__':
    pytest.main(["-s", "./file_3.py"])

  单独执行结果:(不符合规范命名的未执行

test_1_1.py代码:

import pytest


def test_01():
    print('测试用例: 1-1')
    assert '1' == '1'


def test_02():
    print('测试用例: 1-2')
    assert 2 == 2


if __name__ == '__main__':
    pytest.main(["-s", "./test_1_1.py"])

  test_1_2.py代码

import pytest


def test_01():
    print('测试用例: 2-1')
    assert '1' == '1'


def test_02():
    print('测试用例: 2-2')
    assert 2 == 2


if __name__ == '__main__':
    pytest.main(["-s", "./test_1_2.py"])

3、执行demo2 目录下的test_01.py文件;使用pytest.main([“-s”])执行;

执行结果如下:(文件file_3.py未执行不符合命名规范

5.1.2、执行指定文件使用pytest.main([“-s”, “文件名”])

              例:pytest -k key xxx.py 表示执行xxx.py里包含key字符串的测试用例;

           3、q 表示减少测试的运行冗长;

           4、-x 表示出现一条测试用例失败就退出测试。  

​​​​​​​

  1. 测试文件test_08.py
    import pytest
    
    import pytest
    
    class Test_1():
    
        def test_01(self):
            print('测试用例: 1')
            assert '1' == '1'
    
        def test_02(self):
            print('测试用例: 2')
            assert 2 == 2
    
    class Test_2():
    
        def test_03(self):
            print('测试用例: 3')
            assert '1' == '1'
    
        def test_04(self):
            print('测试用例: 4')
            assert 2 == 2
    
    if __name__ == '__main__':
        pytest.main(["-s", "test_08.py::Test_2::test_04"])
    

    2、执行结果:

  2. 5.1.3、执行指定文件里的指定class下的所有用例使用pytest.main([“-s”, “文件名::class”])

  3. 注:执行指定class里的指定用例一样:pytest.main([“-s”, “文件名::class名::用例名”])

  4. 1、示例代码:

  5. import pytest
    
    import pytest
    
    class Test_1():
    
        def test_01(self):
            print('测试用例: 1')
            assert '1' == '1'
    
        def test_02(self):
            print('测试用例: 2')
            assert 2 == 2
    
    class Test_2():
    
        def test_03(self):
            print('测试用例: 3')
            assert '1' == '1'
    
        def test_04(self):
            print('测试用例: 4')
            assert 2 == 2
    
    if __name__ == '__main__':
        pytest.main(["-s", "test_08.py::Test_2"])
  6. 2、执行结果:

  7.  

    5.2、在命令行窗口中执行

  8. 5.2.1、执行当前目录下及其下级目录所有符合条件的测试用例文件

  9. 1、打开终端,cd命令切换到要执行的测试文件所在的目录里;(这里继续使用demo2目录下的所有文件)

  10.  

    2、执行方法及结果;(在该目录下输入pytest)

  11.  

    5.2.2、执行指定的文件

  12. 1、执行demo2目录里的test_01.py

  13. 2、输入命令行:pytest  文件名

  14. 3、执行结果如下:

  15. 5.2.3、执行指定的用例类

  16. 1、以test_09.py文件为例;

  17. 2、执行方法:pytest  文件名::class名

  18. 3、执行结果:(执行了class Test_2里的2个用例)

  19. 5.3、执行测试用的参数使用

  20.   1、-s  表示:关闭捕捉,输出信息;-v则不会在控制台输出信息

  21.   2、-k 表示执行包含某个字符串的测试用例;

猜你喜欢

转载自blog.csdn.net/weixin_44701654/article/details/128005596