自动化的基础
- 怎么写用例,怎么运行用例 单元测试框架
- 用例写在哪,测试程序怎么读取用例 Excel,CSV ,YAML, Json
- 日志处理
- 配置文件处理
- 什么是接口,接口类型
unittest 框架
- 这是一个内置的框架
- 单元测试框架
- 通过代码怎么做测试
单元测试
- 单元测试 —> 模块测试 —> 集成测试 —> 系统测试
- 就是对某个函数或类进行测试
- 组织代码的最小单位
单元测试怎么做
- 1、自己编写用例函数,自己调用,自己生成报告,自己组织报告
- 2、使用线程的单元测试框架
单元测试框架的作用
- 1、自动收集用例
- 2、自动调用用例
- 3、生成测试报告
- 4、自动断言
- 5、编写预置条件
unittest的用法 怎么编写用例
- 测试用例是卸载模块当中,模块名称 test_*.py
- 写的测试用例(测试函数)也是以test开头
- 写测试类 Test
- 测试类要继承 unittest.TestCase
运行测试用例
- 1、运行单个测试函数
- 2、运行单个测试类
- 3、运行单个测试模块
- 4、运行整个项目的用例
项目工程目录结构
- day12_单元测试框架 # 目录
- report 目录
- d1_单元测试.py
- test_login.py # 测试用例
- test_login1.py
- 总结.md
- 练习题.py
- run.py # 主程序的入口
def login():
"""登录功能,开发写的"""
response = {"message":None,
"code":None ,
"token":None
}
if username == 'kino' and passwd = '123456':
response['message'] = 'success'
response['code'] = 200
response['token'] = 'success'
return response
return {"message":"good token",
"code": 200,
"token":"good token"
}
if username == '':
response['message'] = '用户名为空'
response['code'] = 400
return response
if password == '':
response['message'] = '密码为空'
response['code'] = 400
return response
else:
response['message'] = '用户名或密码错误'
response['code'] = 400
return response
print(login('kino','123456'))
print(login('','123456'))
print(login('kino',''))
print(login('kio','12345688'))
case1 = {"username":"kino","passwod":"123456",
"expected":
{"message":"success","code": 200,"token":"good token"}
}
case2 = {"username":"","passwod":"123456",
"expected":
{"message":"用户名为空","code": 400,"token":None}
}
case3 = {"username":"kino","passwod":"",
"expected":
{"message":"密码为空","code": 400,"token":None}
}
case4 = {"username":"kino1","passwod":"1234567",
"expected":
{"message":"用户名或密码错误","code": 400,"token":None}
}
def valid_login():
"""测试登录功能是否正常"""
actual = login(case1['username'], case1['passwd'])
expected = case1['expected']
if actual == expeced:
print("测试用例通过")、
else:
print("测试用例失败")
def valid_login_assert():
"""测试登录功能是否正常"""
actual = login(case1['username'], case1['passwd'])
expected = case1['expected']
msg =
f"""
expected:{expected}
acutal:{acutal}
"""
assert expected == actual, msg
valid_login_assert()
import unittest
class TestLogin(unittest.TestCase):
def test_login(self):
actual = login(case1['username'],case1['password'])
expected = case1['expected']
self.assertEqual(expected, acutal)
def test_login2(self):
actual = login(case2['username'],case2['password'])
expected = case2['expected']
self.assertEqual(expected, acutal)
def test_login3(self):
actual = login(case3['username'],case3['password'])
expected = case3['expected']
self.assertEqual(expected, acutal)
def test_login4(self):
actual = login(case4['username'],case4['password'])
expected = case4['expected']
self.assertEqual(expected, acutal)
import unittest
import unittestreport
suit = unittest.defaultTestLoader.discover('test')
print(suit)
result = unittest.TextTestRunner().run(suit)
print(result)
runner = unittestreport.TestRunner(suit,
title = "测试报告",
tester = "荒年",
desc="xxx项目生成的测试报告",
tempaates = 1
)
runner.run()
print(result1)