pytest学习总结二:setup和teardown的使用总结

2021/10/21 23:14:08

本文主要是介绍pytest学习总结二:setup和teardown的使用总结,对大家解决编程问题具有一定的参考价值,需要的程序猿们随着小编来一起学习吧!

介绍setup和teardown之前,先来介绍一个测试场景,比如我们要测试淘宝的购物车的添加功能,那我们是不是需要先登录,然后再到购物车模块去操作,最后是退出账号,那么像这种测试一个模块前后需要做的准备工作和收尾的工作,可以通过写代码实现,但是pytest帮我们封装好了方法teardown和setup,有不同的场景下对应的不同的方法。

根据用例运行级别可以分为以下几种
模块级(setup_module/teardown_module)开始于模块始末,全局的

函数级(setup_function/teardown_function)只对函数用例生效(不在类中)

类级(setup_class/teardown_class)只在类中前后运行一次(在类中)

方法级(setup_method/teardown_method)开始于方法始末(在类中)

类里面的(setup/teardown)运行在调用方法的前后
举例说明:
一、函数级别的。

import pytest
count = 1

def setup_function():
    print("函数测试开始了.....")

def teardown_function():
    print("函数测试结束了.....")

def add(x, y):
    if type(x) is int or type(y) is int:
        return x+y
    else:
        raise TypeError
# @pytest.mark.ToDo3

def test_case1():
    print("正常值。。。")
    assert add(1,2) == 3

# @pytest.mark.toDo2
# @pytest.mark.skipif(count <= 1, reason = "count值太小")
def test_case2():
    print("异常值")
    with pytest.raises(TypeError):
        assert add(1,"2") != 3

if __name__ == '__main__':
    pytest.main(['-sq',  'test_10_21.py'])

运行结果,可以看到:每个测试函数执行前和执行后都会去执行一遍。

C:\Python36\python.exe D:/自动化B/python_test/test_10_21.py
函数测试开始了.....
正常值。。。
.函数测试结束了.....
函数测试开始了.....
异常值
.函数测试结束了.....
sss
2 passed, 3 skipped in 0.04s

Process finished with exit code 0

二、模块级别的。
将上面的那两行代码改成下面两行:

def setup_module():
    print("模块测试开始了.....")

def teardown_module():
    print("模块测试结束了.....")

运行结果:

函数测试开始了.....
正常值。。。
.异常值
.sss函数测试结束了.....

2 passed, 3 skipped in 0.06s

可以看到,在整个模块运行前和运行结束后分别执行了一次。

三、类和方法(这里需要注意一下,这个是放在类中的。)

import pytest
count = 1
def setup_module():
    print("模块测试开始了.....")

def teardown_module():
    print("模块测试结束了.....")
    
def add(x, y):
    if type(x) is int or type(y) is int:
        return x+y
    else:
        raise TypeError
# @pytest.mark.ToDo3

def test_case1():
    print("正常值。。。")
    assert add(1,2) == 3

# @pytest.mark.toDo2
# @pytest.mark.skipif(count <= 1, reason = "count值太小")
def test_case2():
    print("异常值")
    with pytest.raises(TypeError):
        assert add(1,"2") != 3
# @pytest.mark.skip(reason = "我不想被执行")
class TestCase(object):
    def setup_class(self):
        print("类测试开始了.....")

    def teardown_class(self):
        print("类测试结束了.....")

    def setup_method(self):
        print("方法测试开始了.....")

    def teardown_method(self):
        print("方法测试结束了.....")

    def setup(self):
        print("方法调用测试开始了.....")

    def teardown(self):
        print("方法调用测试结束了.....")
    def test_CASE3(self):
        print("特殊字符:")
        with pytest.raises(TypeError):
            assert add("$", "****") != 3

@pytest.mark.skip()
def test_error():
    print("这是一个错误结果的用例测试")
    assert 1 == 2
@pytest.mark.skip()
def test_CASE4():
    print("特殊字符:")
    with pytest.raises(TypeError):
        assert add("][]", ">?>") != 3


if __name__ == '__main__':
    pytest.main(['-sv',  'test_10_21.py'])

运行结果:

模块测试开始了.....
正常值。。。
.异常值
.类测试开始了.....
方法测试开始了.....
方法调用测试开始了.....
特殊字符:
.方法调用测试结束了.....
方法测试结束了.....
类测试结束了.....
ss模块测试结束了.....

3 passed, 2 skipped in 0.07s



这篇关于pytest学习总结二:setup和teardown的使用总结的文章就介绍到这儿,希望我们推荐的文章对大家有所帮助,也希望大家多多支持为之网!


扫一扫关注最新编程教程