首页 > 其他 > 详细

pytest 3个简单案例

时间:2021-06-23 22:04:10      阅读:16      评论:0      收藏:0      [点我收藏+]

第一个

‘‘‘第一种实现‘‘‘
class TestMy:
    def fun1(self):
        self.d = u2.connect()

    # 捕获连接异常
    def test_fun1(self):
        with pytest.raises(RuntimeError) as e:
            self.fun1()
        exec_msg = e.value.args[0]     # 外部运行,直接运行-输出的是本文件的路径,运行+参数输出的是参数
        assert exec_msg == Can\‘t find any android device/emulator     # 错误提示需与指定异常报错一致
        print(connected!)

第二个

‘‘‘第二种实现‘‘‘
def fun1():
    d = u2.connect()


class TestMy:
    # 捕获连接异常
    def test_fun1(self):
        with pytest.raises(RuntimeError) as e:
            fun1()
        exec_msg = e.value.args[0]     # 外部运行,直接运行-输出的是本文件的路径,运行+参数输出的是参数
        assert exec_msg == Can\‘t find any android device/emulator     # 错误提示需与指定异常报错一致
        print(unconnected!)

第三个

 pytest的作用域:

‘‘‘
function: 函数级,每个测试函数都会执行一次固件;
class: 类级别,每个测试类执行一次,所有方法都可以使用;
module: 模块级,每个模块执行一次,模块内函数和方法都可使用;
session: 会话级,一次测试只执行一次,所有被找到的函数和方法都可用。
setup:用例级,每个用例前去执行一次
teardown:用例级,每个用例前去执行一次
‘‘‘
"""
编写pytest测试用例,规则

测试文件以test_开头(以_test结尾也可以)
测试类以Test开头,并且不能带有 init 方法
测试函数以test_开头
断言使用基本的assert即可

"""
‘‘‘第三种实现‘‘‘
class My(object):

    # def __init__(self):
    #     self.d = u2.connect()

    def fun1(self):
        self.d = u2.connect()
        self.d.app_start(com.huya.nftv)


class TestMy(My):
    def setup_class(self):
        self.d = u2.connect()

    def teardown_class(self):
        print(结束)
    # 捕获连接异常
    def test_fun1(self):
        with pytest.raises(RuntimeError) as e:
            My.fun1(self)
        exec_msg = e.value.args[0]
        assert exec_msg == Can\‘t find any android device/emulator
        print(unconnected!)

 

pytest 3个简单案例

原文:https://www.cnblogs.com/spritegirl/p/14923980.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!