【软件测试12】Python基础+Unittest框架

一、Python基础

import json

print("hello")
age=input("年龄:")
print("age的类型:",type(age))
age1=str(age)
print("现在的age类型:",type(age1))

age=input("年龄")
age=int(age)
height=input('height')
height=float(height)
weight=input('weight')
print('年龄是:%d,身高是:%.1f,体重是:%s'%(age,height,weight))
print(f'年龄是{age},体重是{weight},身高是{height}')
print('年龄是{},体重是{},身高是{:.3f}'.format(age,height,weight))

if age>18:
    print(age)
else:
    print(age-18)

score=input("分数:")
score=int(score)
if score<60:
    print("不及格")
elif score>=60 and score<80:
    print("良好")
elif score>=80 and score<90:
    print("优秀")
else:
    print("太棒了")

str1="fghjkl srftryi"
print(str1[1:3:1])
print(str1[::-1])#逆置
num=str1.find("h",0,6)#返回下标
print(num)
str2=str1.replace("fg","FG")
print(str2)
str3=str1.split()#空格拆分
print(str3)

list1=["dwqr","dweqr","daw"]
str1="1".join(list1)
print(str1)




class Animal:
    def eat(self):
        print(f'{self.name}喜欢吃鱼')
    def drink(self):
        print(f'{self.name}喜欢喝{self.drinkk}')


ani=Animal()
ani.name="小猫"
ani.drinkk="茶"
ani.eat()
ani.drink()

class Student:
    def __init__(self,name,age):
        self.name=name
        self.age=age

    def info(self):
        print(f'{self.name}今年:{self.age}岁')

    def __str__(self):
        return f'{self.name}今年:{self.age}岁'

    def __del__(self):
        print('销毁init值')

student=Student("KK",18)
# student.info()
print(student)

class SeniorStudent(Student):
    def infos(self):
        print(f'子类')

ss=SeniorStudent('MM',20)
ss.info()

def data_read():
    list1=[]
    with open('data.json',encoding='utf-8') as f:
        data=json.load(f)
        for i in data:
            list1.append(i.get("username"),i.get("password"),i.get("expect"))
            print(list1)
    return list1


二、Unittest框架

import unittest
#每个文件都是一个测试用例
#测试用例类可以直接继承unittest的Testcase

#unittest核心要素:TestCase/TestSuite/TestRunner/TestLoader/Fixture

# 使用步骤:
# 1、导包
# 2、实例化套件对象
# 3、套件添加测试方法
# 4、实例化运行对象
# 5、使用运行对象执行套件方法

class TestDemo(unittest.TestCase):
    def test_method1(self):
        print("methon1")
    def test_method2(self):
        print("method2")


suite=unittest.TestSuite()
suite.addTest(TestDemo("test_method1"))
suite.addTest(TestDemo("test_method2"))

runner=unittest.TextTestRunner()
runner.run(suite)

三、Unittest应用

#tool.py
def add(a,b):
    c=a+b
    return c

add(1,2)

#toolTest.py
import unittest
from tool import add

class TestDemo(unittest.TestCase):
    def test_method1(self):
        if add(1,2)==3:
            print("测试通过")
        else:
            print("测试失败")

    def test_method2(self):
        if add(5, 2) == 3:
            print("测试通过")
        else:
            print("测试失败")


suite=unittest.TestSuite()
suite.addTest(TestDemo("test_method1"))
suite.addTest(TestDemo("test_method2"))

runner=unittest.TextTestRunner()
runner.run(suite)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值