功能测试(Functional Testing)
一句话总结:验证软件的每个功能是否按照需求正常工作。
🌟 快速理解
就像检查手机的每个功能:
- ✅ 能打电话吗?
- ✅ 能发短信吗?
- ✅ 能拍照吗?
- ✅ 能上网吗?
功能测试 = 逐个检查每个功能是否正常
📌 核心概念
什么是功能测试?
功能测试:验证软件的每个功能是否符合需求规格说明。
功能测试 vs 非功能测试
| 维度 |
功能测试 |
非功能测试 |
| 关注点 |
做什么(What) |
怎么做(How) |
| 测试内容 |
功能是否正常 |
性能、安全、可用性 |
| 示例 |
能否登录 |
登录速度如何 |
🎯 真实案例
案例:电商系统功能测试
功能清单:
- 用户注册 ✅
- 用户登录 ✅
- 商品搜索 ✅
- 加入购物车 ✅
- 下单 ✅
- 支付 ✅
测试示例:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32
| def test_user_registration(): """测试用户注册功能""" response = register( email="test@example.com", password="password123" ) assert response["success"] == True assert "user_id" in response
def test_user_login(): """测试用户登录功能""" response = login( email="test@example.com", password="password123" ) assert response["success"] == True assert "token" in response
def test_product_search(): """测试商品搜索功能""" response = search(keyword="iPhone") assert len(response["products"]) > 0 assert "iPhone" in response["products"][0]["name"]
def test_add_to_cart(): """测试加入购物车功能""" response = add_to_cart( product_id=123, quantity=1 ) assert response["success"] == True assert response["cart_count"] == 1
|
✅ 功能测试技术
1. 基于需求的测试
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
|
def test_login_with_valid_credentials(): """正确的凭据""" response = login("test@example.com", "password123") assert response["success"] == True
def test_login_with_invalid_password(): """错误的密码""" response = login("test@example.com", "wrong") assert response["error"] == "密码错误"
def test_login_with_nonexistent_email(): """不存在的邮箱""" response = login("notexist@example.com", "password123") assert response["error"] == "用户不存在"
|
2. 等价类划分
1 2 3 4 5 6 7 8
| def test_age_equivalence_classes(): assert validate_age(30) == True assert validate_age(17) == False assert validate_age(66) == False
|
3. 边界值分析
1 2 3 4 5 6 7 8
| def test_age_boundary_values(): assert validate_age(17) == False assert validate_age(18) == True assert validate_age(65) == True assert validate_age(66) == False
|
📋 功能测试流程
1 2 3 4 5 6 7 8 9
| graph TD A[需求分析] --> B[设计测试用例] B --> C[准备测试数据] C --> D[执行测试] D --> E{测试通过?} E -->|是| F[记录结果] E -->|否| G[报告缺陷] G --> H[修复缺陷] H --> D
|
🛠️ 功能测试工具
| 工具 |
类型 |
推荐度 |
| Pytest |
Python |
⭐⭐⭐⭐⭐ |
| Jest |
JavaScript |
⭐⭐⭐⭐⭐ |
| Selenium |
UI测试 |
⭐⭐⭐⭐ |
| Postman |
API测试 |
⭐⭐⭐⭐⭐ |
📊 最佳实践
1. 覆盖所有功能
1 2 3 4 5 6 7 8 9 10 11 12 13
| features = [ "用户注册", "用户登录", "商品搜索", "加入购物车", "下单", "支付" ]
for feature in features: test_function = f"test_{feature}"
|
2. 测试正常和异常场景
1 2 3 4 5 6 7 8 9 10 11 12 13
| def test_login_success(): response = login("test@example.com", "password123") assert response["success"] == True
def test_login_with_empty_email(): response = login("", "password123") assert response["error"] == "请输入邮箱"
def test_login_with_empty_password(): response = login("test@example.com", "") assert response["error"] == "请输入密码"
|
3. 使用数据驱动测试
1 2 3 4 5 6 7 8 9 10
| @pytest.mark.parametrize("email,password,expected", [ ("test@example.com", "password123", True), ("test@example.com", "wrong", False), ("notexist@example.com", "password123", False), ("", "password123", False), ("test@example.com", "", False), ]) def test_login_scenarios(email, password, expected): response = login(email, password) assert response["success"] == expected
|
🔗 相关主题
- [[非功能测试]] - 测试性能、安全等
- [[系统测试]] - 包含功能测试
- [[验收测试]] - 功能测试的一种
💡 快速参考
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| 功能测试速查表 ================
定义:验证每个功能是否按需求工作
测试技术: 1. 基于需求的测试 2. 等价类划分 3. 边界值分析 4. 决策表测试
测试范围: ✅ 用户界面 ✅ API接口 ✅ 数据库 ✅ 业务逻辑
推荐工具: - Python: Pytest - JavaScript: Jest - UI: Selenium - API: Postman
最佳实践: 1. 覆盖所有功能 2. 测试正常和异常场景 3. 使用数据驱动测试 4. 自动化执行
|