巴青农资商城

conftest.py 3.9KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  1. # ============================================================
  2. # pytest 配置 — 全局 Fixtures & HTML 报告
  3. # ============================================================
  4. import pytest
  5. import datetime
  6. import os
  7. import random
  8. from api_client import ApiClient
  9. from config import (
  10. API_BASE_URL, ADMIN_USERNAME, ADMIN_PASSWORD, CAPTCHA_ENABLED,
  11. SELLER_USERNAME, SELLER_PASSWORD, TEST_PASSWORD,
  12. )
  13. # ============================================================
  14. # HTML 报告配置
  15. # ============================================================
  16. def pytest_html_report_title(report):
  17. report.title = "巴清农资商城 · 端到端全量测试报告"
  18. def pytest_configure(config):
  19. now = datetime.datetime.now().strftime("%Y-%m-%d %H:%M:%S")
  20. config._metadata = {
  21. "项目": "巴清农资商城 (Baqing Shop)",
  22. "测试时间": now,
  23. "测试类型": "端到端全量测试 (E2E)",
  24. "Python": os.popen("python --version 2>&1").read().strip(),
  25. "API地址": API_BASE_URL,
  26. "测试用例数": "124 条 (覆盖 63 个 Controller)",
  27. }
  28. for tag in ["admin_flow", "consumer_flow", "seller_flow", "full_chain"]:
  29. config.addinivalue_line("markers", tag)
  30. @pytest.hookimpl(hookwrapper=True)
  31. def pytest_runtest_makereport(item, call):
  32. outcome = yield
  33. report = outcome.get_result()
  34. if report.when in ("call", "setup"):
  35. doc = item.function.__doc__ or ""
  36. if doc:
  37. report.description = doc.strip()
  38. # ============================================================
  39. # 全局 Fixtures
  40. # ============================================================
  41. @pytest.fixture(scope="session")
  42. def client():
  43. """所有测试共享一个 HTTP 客户端(session 级别)"""
  44. c = ApiClient()
  45. yield c
  46. c.session.close()
  47. @pytest.fixture(scope="session")
  48. def admin_token(client):
  49. """管理员登录,返回 token(session 级别)"""
  50. if CAPTCHA_ENABLED:
  51. pytest.skip("验证码已开启,请在测试环境关闭验证码后再运行")
  52. resp = client.post("/login", json={
  53. "username": ADMIN_USERNAME,
  54. "password": ADMIN_PASSWORD,
  55. "code": "",
  56. "uuid": "",
  57. })
  58. data = client.assert_ok("管理员登录失败")
  59. token = data.get("token")
  60. assert token, f"登录返回中没有 token"
  61. client.set_admin_token(token)
  62. print(f"\n [SETUP] 管理员 {ADMIN_USERNAME} 登录成功")
  63. return token
  64. @pytest.fixture(scope="session")
  65. def member_token(client):
  66. """会员注册并登录(session 级别)"""
  67. mobile = f"138{random.randint(10000000, 99999999)}"
  68. # 注册
  69. resp = client.post("/api/member/register", json={
  70. "mobile": mobile,
  71. "password": TEST_PASSWORD,
  72. "confirmPassword": TEST_PASSWORD,
  73. "nickName": f"E2E测试_{mobile[-4:]}",
  74. "agreementAccepted": True,
  75. })
  76. data = client.assert_ok("会员注册失败")
  77. member_id = data.get("data")
  78. print(f"\n [SETUP] 会员注册成功 memberId={member_id}")
  79. # 登录
  80. resp = client.post("/api/member/login", json={
  81. "account": mobile,
  82. "password": TEST_PASSWORD,
  83. "agreementAccepted": True,
  84. })
  85. data = client.assert_ok("会员登录失败")
  86. token = data.get("token")
  87. assert token, "会员登录返回中没有 token"
  88. client.set_member_token(token)
  89. print(f" [SETUP] 会员登录成功 token={token[:16]}...")
  90. return token
  91. @pytest.fixture(scope="session")
  92. def seller_token(client):
  93. """商家登录(session 级别)"""
  94. if CAPTCHA_ENABLED:
  95. pytest.skip("验证码已开启")
  96. resp = client.post("/login", json={
  97. "username": SELLER_USERNAME,
  98. "password": SELLER_PASSWORD,
  99. "code": "",
  100. "uuid": "",
  101. })
  102. data = client.assert_ok("商家登录失败")
  103. token = data.get("token")
  104. assert token, "商家登录返回中没有 token"
  105. client.set_seller_token(token)
  106. print(f"\n [SETUP] 商家 {SELLER_USERNAME} 登录成功")
  107. return token