feat: 重构测试框架并优化代码结构
refactor(tests): 将e2e_tests迁移到tests_suite和api_integration_tests style: 为Java类添加文档注释 docs: 更新.gitignore和配置文件 test: 添加性能测试和Playwright测试脚本 chore: 清理旧测试文件和配置
This commit is contained in:
@@ -0,0 +1,47 @@
|
||||
import pytest
|
||||
from typing import Generator, Dict, Any
|
||||
from httpx import AsyncClient, Client
|
||||
from playwright.sync_api import Page, BrowserContext
|
||||
from config import settings
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def api_base_url() -> str:
|
||||
return settings.API_BASE_URL
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def web_base_url() -> str:
|
||||
return settings.WEB_BASE_URL
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def api_client(api_base_url: str) -> Generator[Client, None, None]:
|
||||
with Client(base_url=api_base_url, timeout=settings.REQUEST_TIMEOUT) as client:
|
||||
yield client
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def async_api_client(api_base_url: str) -> Generator[AsyncClient, None, None]:
|
||||
async with AsyncClient(base_url=api_base_url, timeout=settings.REQUEST_TIMEOUT) as client:
|
||||
yield client
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def auth_headers(api_client: Client) -> Dict[str, str]:
|
||||
response = api_client.post(
|
||||
"/api/auth/login",
|
||||
json={
|
||||
"username": settings.TEST_USERNAME,
|
||||
"password": settings.TEST_PASSWORD
|
||||
}
|
||||
)
|
||||
data = response.json()
|
||||
token = data.get("token", data.get("access_token", ""))
|
||||
return {"Authorization": f"Bearer {token}"}
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def authenticated_client(api_client: Client, auth_headers: Dict[str, str]) -> Client:
|
||||
api_client.headers.update(auth_headers)
|
||||
return api_client
|
||||
@@ -0,0 +1,58 @@
|
||||
import pytest
|
||||
from typing import Generator, Dict, Any
|
||||
from faker import Faker
|
||||
from test_utils.data_manager.data_generator import UserDataFactory
|
||||
|
||||
|
||||
fake = Faker("zh_CN")
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def test_user_data() -> Dict[str, Any]:
|
||||
return UserDataFactory.create_user_data()
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def test_role_data() -> Dict[str, Any]:
|
||||
return {
|
||||
"role_name": fake.word(),
|
||||
"role_key": fake.word(),
|
||||
"description": fake.sentence(),
|
||||
"status": "0",
|
||||
"permissions": []
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def test_menu_data() -> Dict[str, Any]:
|
||||
return {
|
||||
"menu_name": fake.word(),
|
||||
"parent_id": 0,
|
||||
"order_num": fake.random_int(1, 100),
|
||||
"path": f"/{fake.word()}",
|
||||
"component": f"{fake.word()}/{fake.word()}",
|
||||
"menu_type": "C",
|
||||
"visible": "0",
|
||||
"status": "0"
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def test_dict_data() -> Dict[str, Any]:
|
||||
return {
|
||||
"dict_name": fake.word(),
|
||||
"dict_type": fake.word(),
|
||||
"status": "0",
|
||||
"remark": fake.sentence()
|
||||
}
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def test_config_data() -> Dict[str, Any]:
|
||||
return {
|
||||
"config_name": fake.word(),
|
||||
"config_key": f"sys.{fake.word()}",
|
||||
"config_value": fake.word(),
|
||||
"config_type": "Y",
|
||||
"remark": fake.sentence()
|
||||
}
|
||||
@@ -0,0 +1,36 @@
|
||||
import pytest
|
||||
from typing import Generator
|
||||
import psycopg2
|
||||
from psycopg2.extras import RealDictCursor
|
||||
from config import settings
|
||||
|
||||
|
||||
@pytest.fixture(scope="session")
|
||||
def db_connection():
|
||||
conn = psycopg2.connect(
|
||||
host=settings.DB_HOST,
|
||||
port=settings.DB_PORT,
|
||||
database=settings.DB_NAME,
|
||||
user=settings.DB_USERNAME,
|
||||
password=settings.DB_PASSWORD,
|
||||
cursor_factory=RealDictCursor
|
||||
)
|
||||
yield conn
|
||||
conn.close()
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def db_cursor(db_connection):
|
||||
cursor = db_connection.cursor()
|
||||
yield cursor
|
||||
cursor.close()
|
||||
db_connection.rollback()
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def clean_test_data(db_cursor):
|
||||
yield
|
||||
tables = ["sys_user", "sys_role", "sys_menu", "sys_dict", "sys_config"]
|
||||
for table in tables:
|
||||
db_cursor.execute(f"DELETE FROM {table} WHERE username LIKE 'test_%'")
|
||||
db_cursor.connection.commit()
|
||||
@@ -0,0 +1,26 @@
|
||||
import pytest
|
||||
from typing import Generator
|
||||
from playwright.sync_api import Page, Browser, BrowserContext
|
||||
from config import settings
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def page(browser: Browser) -> Generator[Page, None, None]:
|
||||
context = browser.new_context(
|
||||
viewport={"width": 1280, "height": 720},
|
||||
locale="zh-CN"
|
||||
)
|
||||
page = context.new_page()
|
||||
page.set_default_timeout(30000)
|
||||
yield page
|
||||
context.close()
|
||||
|
||||
|
||||
@pytest.fixture(scope="function")
|
||||
def authenticated_page(page: Page, web_base_url: str) -> Page:
|
||||
page.goto(f"{web_base_url}/login")
|
||||
page.fill("input[name='username']", settings.TEST_USERNAME)
|
||||
page.fill("input[name='password']", settings.TEST_PASSWORD)
|
||||
page.click("button[type='submit']")
|
||||
page.wait_for_url(f"{web_base_url}/**")
|
||||
return page
|
||||
Reference in New Issue
Block a user