ICode9

精准搜索请尝试: 精确搜索
首页 > 其他分享> 文章详细

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

2021-10-21 22:29:58  阅读:221  来源: 互联网

标签:总结 teardown setup pytest ..... 测试 def


介绍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

标签:总结,teardown,setup,pytest,.....,测试,def
来源: https://blog.csdn.net/weixin_43726471/article/details/120896068

本站声明: 1. iCode9 技术分享网(下文简称本站)提供的所有内容,仅供技术学习、探讨和分享;
2. 关于本站的所有留言、评论、转载及引用,纯属内容发起人的个人观点,与本站观点和立场无关;
3. 关于本站的所有言论和文字,纯属内容发起人的个人观点,与本站观点和立场无关;
4. 本站文章均是网友提供,不完全保证技术分享内容的完整性、准确性、时效性、风险性和版权归属;如您发现该文章侵犯了您的权益,可联系我们第一时间进行删除;
5. 本站为非盈利性的个人网站,所有内容不会用来进行牟利,也不会利用任何形式的广告来间接获益,纯粹是为了广大技术爱好者提供技术内容和技术思想的分享性交流网站。

专注分享技术,共同学习,共同进步。侵权联系[81616952@qq.com]

Copyright (C)ICode9.com, All Rights Reserved.

ICode9版权所有