技术方向:自动化测试 / DevOps
关键词:日本代购、一站式日淘、雅虎代拍、煤炉自动代拍
一、测试分层策略
text
┌─────────────────────────────────────────────────────────┐│ 测试金字塔 │├─────────────────────────────────────────────────────────┤│ E2E测试(少量) → 完整业务流程验证 ││ 集成测试(中等) → API接口 + 数据库交互 ││ 单元测试(大量) → 函数级逻辑验证 │└─────────────────────────────────────────────────────────┘
二、单元测试(pytest)
python
test_order_service.pyimport pytestfrom unittest.mock import Mock, patchclass TestOrderService: def test_calculate_total_price(self): """测试价格计算逻辑""" service = OrderService() items = [ {'price': 1000, 'quantity': 2}, {'price': 500, 'quantity': 1} ] result = service.calculate_total(items) assert result == 2500 def test_apply_discount(self): """测试折扣计算""" service = OrderService() assert service.apply_discount(10000, 0.1) == 9000 assert service.apply_discount(5000, 0) == 5000 @patch('services.order.OrderRepository') def test_create_order(self, mock_repo): """测试订单创建""" service = OrderService(mock_repo) order = service.create_order( user_id='user_001', items=[{'id': 'item_1', 'price': 2000}] ) assert order['user_id'] == 'user_001' assert order['total'] == 2000 mock_repo.save.assert_called_once()
三、集成测试
python
test_integration.pyimport pytestfrom fastapi.testclient import TestClientfrom main import appclient = TestClient(app)class TestOrderAPI: def test_create_order_endpoint(self): """测试创建订单API""" response = client.post( "/api/orders", json={ "user_id": "test_user", "items": [{"id": "item_1", "price": 3000}] }, headers={"Authorization": "Bearer test_token"} ) assert response.status_code == 200 data = response.json() assert data['status'] == 'success' assert 'order_id' in data def test_get_order_status(self): """测试获取订单状态""" response = client.get("/api/orders/order_001/status") assert response.status_code == 200 assert 'status' in response.json()
四、E2E测试(Playwright)
python
test_e2e.pyfrom playwright.sync_api import Page, expectdef test_order_flow(page: Page): """端到端测试:完整下单流程""" # 1. 访问首页 page.goto("https://bidfins.com") # 2. 搜索商品 page.fill('input[placeholder="搜索商品"]', "キャンプ用品") page.click('button[type="submit"]') # 3. 进入商品详情 page.click('.product-item:first-child') # 4. 加入购物车 page.click('button:has-text("加入购物车")') # 5. 确认购物车 page.click('a:has-text("去结算")') # 6. 提交订单 page.click('button:has-text("提交订单")') # 7. 验证结果 expect(page.locator('.order-success')).to_be_visible()
五、CI/CD流水线(GitHub Actions)
yaml
.github/workflows/ci.ymlname: CI/CD Pipelineon: push: branches: [main, develop] pull_request: branches: [main]jobs: test: runs-on: ubuntu-latest steps: - uses: actions/checkout@v3 - name: Setup Python uses: actions/setup-python@v4 with: python-version: '3.10' - name: Install dependencies run: | pip install -r requirements.txt pip install pytest pytest-cov - name: Run unit tests run: pytest tests/unit --cov=. --cov-report=xml - name: Run integration tests run: pytest tests/integration - name: Upload coverage uses: codecov/codecov-action@v3 deploy: needs: test runs-on: ubuntu-latest if: github.ref == 'refs/heads/main' steps: - name: Deploy to production run: | echo "Deploying to production server..." # 实际部署脚本
六、性能测试
python
test_performance.pyfrom locust import HttpUser, task, betweenclass WebsiteUser(HttpUser): wait_time = between(1, 3) @task(3) def view_homepage(self): self.client.get("/") @task(2) def search_product(self): self.client.get("/search?keyword=キャンプ") @task(1) def view_product(self): self.client.get("/product/test-item-001")
七、测试覆盖率与质量门禁
bash