import pytest
# python 3.x中,True和False是关键字,并且总是等于1和0;故x=1相当于x=True,x=0相当于x=False
# 断言x为真
x = 1
def test_demo_1():
print(x)
assert x # 为真,用例通过
# 断言x不为真
def test_demo_2():
assert not x # x为真,用例不通过
y = 0
def test_demo_3():
print(y)
assert not y # y不为真,用例通过
# 断言y包含x 注意x和y数据类型必须相同,否则断言失败,用例不通过
# 个人觉得,此断言多用于字符串
x = 'th'
y = 'python'
def test_demo_4():
print(x, end=',')
print(y)
assert x in y # y包含x,用例通过
def test_demo_5():
print(x, end=',')
print(y)
assert y in x # x中不包含y,用例不通过
# 断言x等于y
x = 'test'
y = 'test'
z = 'sbcy'
def test_demo_6():
assert x == y # 用例通过
def test_demo_7():
assert x == z # 测试用例不通过
# 断言x不等于y
def test_demo_8():
assert x != y # 测试用例不通过
def test_demo_9():
assert x != z # 通过
if __name__ == '__main__':
pytest.main()
最后一次对x,y的赋值是:
x = ‘test’
y = ‘test’
z = ‘sbcy’
所以前面有对x,y赋值的地方,x,y都变成了test,因此有些测试用例的执行结果与预想的不同。