非功能测试(Non-Functional Testing)
一句话总结:测试软件的性能、安全、可用性等质量属性。
🌟 快速理解
功能测试 vs 非功能测试:
1 2 3 4 5 6 7 8 9 10
| 功能测试(What): - 能登录吗?✅ - 能搜索吗?✅ - 能下单吗?✅
非功能测试(How): - 登录速度快吗?⚡ - 能承载多少用户?📊 - 数据安全吗?🔒 - 界面好用吗?🎨
|
📌 核心概念
什么是非功能测试?
非功能测试:测试软件的质量属性,如性能、安全性、可用性、可靠性等。
功能 vs 非功能
| 维度 |
功能测试 |
非功能测试 |
| 关注点 |
做什么 |
怎么做 |
| 测试内容 |
功能是否正常 |
性能、安全、可用性 |
| 示例 |
能否登录 |
登录速度、安全性 |
| 测试时机 |
开发阶段 |
开发后期 |
🔄 非功能测试类型
1. 性能测试 ⚡
测试内容:响应时间、吞吐量、资源使用率
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| from locust import HttpUser, task
class PerformanceTest(HttpUser): @task def test_login_performance(self): self.client.post("/login", json={ "email": "test@example.com", "password": "password123" })
|
2. 安全测试 🔒
测试内容:SQL注入、XSS、认证、授权
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| def test_sql_injection(): """测试SQL注入""" response = login( email="admin' OR '1'='1", password="anything" ) assert response["success"] == False
def test_xss(): """测试XSS攻击""" response = create_comment( content="<script>alert('XSS')</script>" ) assert "<script>" not in response["content"]
|
3. 可用性测试 🎨
测试内容:界面友好性、易用性
1 2 3 4 5 6 7 8 9 10 11
| def test_usability(): """测试可用性""" assert page.find_element("#login-btn").is_displayed() response = login("", "") assert "请输入邮箱" in response["error"] assert page.find_element("#quick-buy").is_displayed()
|
4. 兼容性测试 🌐
测试内容:浏览器、操作系统、设备兼容性
1 2 3 4 5 6 7 8 9 10 11
| @pytest.mark.parametrize("browser", [ "chrome", "firefox", "safari", "edge" ]) def test_browser_compatibility(browser): """测试浏览器兼容性""" driver = get_driver(browser) driver.get("https://example.com") assert "欢迎" in driver.page_source
|
5. 可靠性测试 🛡️
测试内容:系统稳定性、容错能力
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| def test_reliability(): """测试可靠性""" for i in range(10000): response = get_products() assert response.status_code == 200 stop_database() response = get_products() assert response["error"] == "服务暂时不可用" start_database() response = get_products() assert response.status_code == 200
|
6. 可扩展性测试 📈
测试内容:系统扩展能力
1 2 3 4 5 6
| def test_scalability(): """测试可扩展性""" for users in [100, 1000, 5000, 10000]: response_time = load_test(users=users) assert response_time < 1000
|
📊 最佳实践
1. 定义明确的指标
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| performance_metrics = { "response_time": "< 500ms", "throughput": "> 1000 TPS", "cpu_usage": "< 80%", "memory_usage": "< 70%" }
security_metrics = { "sql_injection": "防护", "xss": "防护", "csrf": "防护", "authentication": "强制" }
|
2. 使用专业工具
| 测试类型 |
推荐工具 |
| 性能测试 |
Locust, K6, JMeter |
| 安全测试 |
OWASP ZAP, Burp Suite |
| 可用性测试 |
UserTesting, Hotjar |
| 兼容性测试 |
BrowserStack, Sauce Labs |
3. 集成到CI/CD
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
| name: Non-Functional Tests
on: [push]
jobs: performance-test: runs-on: ubuntu-latest steps: - run: locust -f test_performance.py --headless security-test: runs-on: ubuntu-latest steps: - run: zap-baseline.py -t https://example.com
|
🔗 相关主题
- [[功能测试]] - 测试功能是否正常
- [[性能测试]] - 非功能测试的一种
- [[安全测试]] - 非功能测试的一种
💡 快速参考
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
| 非功能测试速查表 ================
定义:测试软件的质量属性
测试类型: 1. 性能测试(速度、吞吐量) 2. 安全测试(SQL注入、XSS) 3. 可用性测试(易用性) 4. 兼容性测试(浏览器、设备) 5. 可靠性测试(稳定性) 6. 可扩展性测试(扩展能力)
功能 vs 非功能: - 功能:做什么(What) - 非功能:怎么做(How)
推荐工具: - 性能:Locust, K6 - 安全:OWASP ZAP - 兼容:BrowserStack
最佳实践: 1. 定义明确的指标 2. 使用专业工具 3. 集成到CI/CD 4. 定期测试
|