Skip to content

测试部署

项目开发完成后,需要进行测试和部署。本章介绍测试策略和部署方案。

测试策略

单元测试

bash
pip install pytest pytest-asyncio
python
# tests/test_formatter.py
import pytest
from services.formatter import markdown_to_html

def test_markdown_to_html():
    md = "# 标题\n\n这是正文"
    html = markdown_to_html(md)
    assert "<h1>" in html
    assert "标题" in html

def test_empty_content():
    html = markdown_to_html("")
    assert html == ""

集成测试

python
# tests/test_api.py
import pytest
from fastapi.testclient import TestClient
from main import app

client = TestClient(app)

def test_root():
    response = client.get("/")
    assert response.status_code == 200

def test_publish_endpoint():
    response = client.post("/publish", json={
        "article": {
            "title": "测试文章",
            "content": "测试内容"
        },
        "platforms": ["juejin"]
    })
    assert response.status_code == 200

部署方案

Docker 部署

dockerfile
# Dockerfile
FROM python:3.11-slim

WORKDIR /app
COPY requirements.txt .
RUN pip install -r requirements.txt

COPY . .
EXPOSE 8000

CMD ["uvicorn", "main:app", "--host", "0.0.0.0"]
bash
# 构建和运行
docker build -t content-distributor .
docker run -p 8000:8000 content-distributor

云平台部署

推荐平台

  • Vercel:适合前端,免费额度充足
  • Railway:适合后端,支持 Docker
  • 阿里云 / 腾讯云:国内访问快

环境变量配置

bash
# .env
JUEJIN_COOKIE=your_cookie_here
WORDPRESS_URL=https://your-blog.com/wp-json
WORDPRESS_USER=admin
WORDPRESS_PASSWORD=app_password

运维监控

日志记录

python
import logging

logging.basicConfig(
    level=logging.INFO,
    format='%(asctime)s - %(name)s - %(levelname)s - %(message)s'
)

logger = logging.getLogger(__name__)

def publish_article(article, platform):
    logger.info(f"开始发布到 {platform}")
    # 发布逻辑
    logger.info(f"发布成功: {platform}")

项目总结

恭喜你完成了跨平台内容分发工具的开发!

你学到了

  • 各平台 API 的调研和选型
  • 浏览器自动化技术
  • 内容格式转换
  • Web 应用开发
  • Docker 部署

扩展方向

  • 添加定时发布功能
  • 支持更多平台
  • 添加数据统计
  • 开发浏览器扩展版本

← 返回用户界面 | 返回项目四

最近更新

基于 Apache 2.0 许可发布