【Python+Pytest】简单的Pytest例子,感觉比unittest好用一些,而且兼容unittest代码

切记一点,py文件、类名、测试用例名都要以“test_”开头。

# 切记,py文件也要以“test_”开头
import pytest  # 首先引入pytest。简单的例子不引入也能运行

def add(x):    # 先申明1个函数“返回传值+2”,做为测试使用
    return x + 2

class Test_Class_Add():  # 先定义1个类,一定要以“test_”开头
    def test_add1(self):    # 定义测试用户,一定要以“test_”开头
        assert add(2) == 5  # 使用assert进行断言,是否相等
    def test_add2(self):
        assert add(22) == 24
    def test_add3(self):
        assert add(100) == 102

class Test_Class_In():    # 定义第2个类
    def test_in(self):
        a = "Hello World!"
        b = "Hi"
        c = "World"
        assert c in a     # 使用in和not in进行断言
        assert b not in a
    def test_notin(self):
        assert b in a
        assert c not in a

测试结果如下:

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值