【软件测试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)

你可能感兴趣的:(软件测试,python,开发语言)