08ea5fbe98
添加用户管理视图、API和状态管理文件
293 lines
11 KiB
TypeScript
293 lines
11 KiB
TypeScript
import { test, expect } from '../test-fixtures';
|
|
import { TestCoverageReporter } from '../core/test-coverage-reporter';
|
|
|
|
test.describe('TestCoverageReporter - 使用示例', () => {
|
|
let coverageReporter: TestCoverageReporter;
|
|
|
|
test.beforeEach(() => {
|
|
coverageReporter = new TestCoverageReporter();
|
|
coverageReporter.startCoverage();
|
|
});
|
|
|
|
test.afterEach(() => {
|
|
coverageReporter.endCoverage();
|
|
});
|
|
|
|
test('示例1: 基本使用 - 记录测试结果', async ({ page, testLogger }) => {
|
|
testLogger.startStep('测试登录功能');
|
|
|
|
await page.goto('https://example.com/login');
|
|
|
|
const testResult = 'passed';
|
|
const testDuration = 1500;
|
|
const testTags = ['@smoke', '@critical'];
|
|
const testFile = 'login.spec.ts';
|
|
|
|
coverageReporter.recordTestResult(
|
|
'LoginTests',
|
|
'登录功能测试',
|
|
testResult,
|
|
testDuration,
|
|
testTags,
|
|
testFile
|
|
);
|
|
|
|
testLogger.endStep('测试登录功能', testResult);
|
|
});
|
|
|
|
test('示例2: 批量记录测试结果', async ({ page, testLogger }) => {
|
|
const testCases = [
|
|
{ name: '用户名验证', status: 'passed' as const, duration: 800, tags: ['@smoke'] },
|
|
{ name: '密码验证', status: 'passed' as const, duration: 900, tags: ['@smoke'] },
|
|
{ name: '登录按钮点击', status: 'passed' as const, duration: 500, tags: ['@smoke'] },
|
|
{ name: '登录成功跳转', status: 'failed' as const, duration: 2000, tags: ['@critical'] },
|
|
{ name: '错误提示显示', status: 'passed' as const, duration: 600, tags: ['@normal'] }
|
|
];
|
|
|
|
testLogger.startStep('批量记录测试结果');
|
|
|
|
testCases.forEach((testCase, index) => {
|
|
coverageReporter.recordTestResult(
|
|
'LoginTests',
|
|
testCase.name,
|
|
testCase.status,
|
|
testCase.duration,
|
|
testCase.tags,
|
|
'login.spec.ts'
|
|
);
|
|
|
|
testLogger.info(`测试用例 ${index + 1}: ${testCase.name} - ${testCase.status}`);
|
|
});
|
|
|
|
testLogger.endStep('批量记录测试结果', 'completed');
|
|
});
|
|
|
|
test('示例3: 多个测试套件', async ({ page, testLogger }) => {
|
|
const suites = [
|
|
{ name: 'LoginTests', tests: 3 },
|
|
{ name: 'DashboardTests', tests: 5 },
|
|
{ name: 'UserManagementTests', tests: 4 }
|
|
];
|
|
|
|
testLogger.startStep('测试多个测试套件');
|
|
|
|
suites.forEach(suite => {
|
|
for (let i = 1; i <= suite.tests; i++) {
|
|
coverageReporter.recordTestResult(
|
|
suite.name,
|
|
`${suite.name} - 测试${i}`,
|
|
'passed',
|
|
1000,
|
|
['@regression'],
|
|
`${suite.name.toLowerCase()}.spec.ts`
|
|
);
|
|
}
|
|
|
|
testLogger.info(`${suite.name}: ${suite.tests}个测试用例`);
|
|
});
|
|
|
|
testLogger.endStep('测试多个测试套件', 'completed');
|
|
});
|
|
|
|
test('示例4: 生成覆盖率报告', async ({ page, testLogger }) => {
|
|
testLogger.startStep('生成覆盖率报告');
|
|
|
|
coverageReporter.recordTestResult('ExampleTests', '测试1', 'passed', 1000, ['@smoke'], 'example.spec.ts');
|
|
coverageReporter.recordTestResult('ExampleTests', '测试2', 'passed', 1000, ['@smoke'], 'example.spec.ts');
|
|
coverageReporter.recordTestResult('ExampleTests', '测试3', 'failed', 2000, ['@critical'], 'example.spec.ts');
|
|
coverageReporter.recordTestResult('ExampleTests', '测试4', 'skipped', 0, ['@normal'], 'example.spec.ts');
|
|
|
|
const coverage = coverageReporter.getCoverage();
|
|
|
|
testLogger.info(`总测试数: ${coverage.totalTests}`);
|
|
testLogger.info(`通过测试: ${coverage.passedTests}`);
|
|
testLogger.info(`失败测试: ${coverage.failedTests}`);
|
|
testLogger.info(`跳过测试: ${coverage.skippedTests}`);
|
|
testLogger.info(`通过率: ${coverage.passRate.toFixed(2)}%`);
|
|
|
|
testLogger.endStep('生成覆盖率报告', 'completed');
|
|
});
|
|
|
|
test('示例5: 导出不同格式的报告', async ({ page, testLogger }) => {
|
|
testLogger.startStep('导出不同格式的报告');
|
|
|
|
coverageReporter.recordTestResult('ExampleTests', '测试用例', 'passed', 1000, ['@smoke'], 'example.spec.ts');
|
|
|
|
const jsonReport = coverageReporter.exportCoverage('json');
|
|
const htmlReport = coverageReporter.exportCoverage('html');
|
|
const markdownReport = coverageReporter.exportCoverage('markdown');
|
|
|
|
testLogger.info('JSON报告长度:', jsonReport.length);
|
|
testLogger.info('HTML报告长度:', htmlReport.length);
|
|
testLogger.info('Markdown报告长度:', markdownReport.length);
|
|
|
|
testLogger.endStep('导出不同格式的报告', 'completed');
|
|
});
|
|
|
|
test('示例6: 获取特定套件的覆盖率', async ({ page, testLogger }) => {
|
|
testLogger.startStep('获取特定套件的覆盖率');
|
|
|
|
coverageReporter.recordTestResult('LoginTests', '测试1', 'passed', 1000, ['@smoke'], 'login.spec.ts');
|
|
coverageReporter.recordTestResult('LoginTests', '测试2', 'passed', 1000, ['@smoke'], 'login.spec.ts');
|
|
coverageReporter.recordTestResult('DashboardTests', '测试1', 'passed', 1000, ['@regression'], 'dashboard.spec.ts');
|
|
|
|
const loginSuite = coverageReporter.getSuiteCoverage('LoginTests');
|
|
const dashboardSuite = coverageReporter.getSuiteCoverage('DashboardTests');
|
|
|
|
if (loginSuite) {
|
|
testLogger.info(`LoginTests套件:`);
|
|
testLogger.info(` 总测试数: ${loginSuite.totalTests}`);
|
|
testLogger.info(` 通过测试: ${loginSuite.passedTests}`);
|
|
testLogger.info(` 通过率: ${loginSuite.passRate.toFixed(2)}%`);
|
|
}
|
|
|
|
if (dashboardSuite) {
|
|
testLogger.info(`DashboardTests套件:`);
|
|
testLogger.info(` 总测试数: ${dashboardSuite.totalTests}`);
|
|
testLogger.info(` 通过测试: ${dashboardSuite.passedTests}`);
|
|
testLogger.info(` 通过率: ${dashboardSuite.passRate.toFixed(2)}%`);
|
|
}
|
|
|
|
testLogger.endStep('获取特定套件的覆盖率', 'completed');
|
|
});
|
|
|
|
test('示例7: 与实际测试集成', async ({ page, helpers, testLogger }) => {
|
|
testLogger.startStep('实际测试集成示例');
|
|
|
|
await page.goto('https://example.com');
|
|
|
|
const startTime = Date.now();
|
|
|
|
try {
|
|
await helpers.assertion.assertElementVisible(page, 'h1', '页面标题应该可见');
|
|
coverageReporter.recordTestResult('PageTests', '页面标题可见性测试', 'passed', Date.now() - startTime, ['@smoke'], 'page.spec.ts');
|
|
} catch (error) {
|
|
coverageReporter.recordTestResult('PageTests', '页面标题可见性测试', 'failed', Date.now() - startTime, ['@smoke'], 'page.spec.ts');
|
|
}
|
|
|
|
try {
|
|
const title = await page.title();
|
|
await helpers.assertion.assertTitle(page, /Example/, '页面标题应该包含Example');
|
|
coverageReporter.recordTestResult('PageTests', '页面标题内容测试', 'passed', Date.now() - startTime, ['@smoke'], 'page.spec.ts');
|
|
} catch (error) {
|
|
coverageReporter.recordTestResult('PageTests', '页面标题内容测试', 'failed', Date.now() - startTime, ['@smoke'], 'page.spec.ts');
|
|
}
|
|
|
|
testLogger.endStep('实际测试集成示例', 'completed');
|
|
});
|
|
|
|
test('示例8: 完整的测试流程', async ({ page, helpers, testLogger }) => {
|
|
const suiteName = 'CompleteTestFlow';
|
|
const testFile = 'complete-flow.spec.ts';
|
|
|
|
testLogger.startStep('完整的测试流程');
|
|
|
|
const testSteps = [
|
|
{ name: '打开登录页面', url: '/login', tags: ['@smoke'] },
|
|
{ name: '输入用户名', tags: ['@smoke'] },
|
|
{ name: '输入密码', tags: ['@smoke'] },
|
|
{ name: '点击登录按钮', tags: ['@critical'] },
|
|
{ name: '验证登录成功', tags: ['@critical'] }
|
|
];
|
|
|
|
for (const step of testSteps) {
|
|
const startTime = Date.now();
|
|
testLogger.startStep(step.name);
|
|
|
|
try {
|
|
if (step.url) {
|
|
await page.goto(`https://example.com${step.url}`);
|
|
}
|
|
|
|
await page.waitForTimeout(100);
|
|
|
|
coverageReporter.recordTestResult(
|
|
suiteName,
|
|
step.name,
|
|
'passed',
|
|
Date.now() - startTime,
|
|
step.tags,
|
|
testFile
|
|
);
|
|
|
|
testLogger.endStep(step.name, 'passed');
|
|
} catch (error) {
|
|
coverageReporter.recordTestResult(
|
|
suiteName,
|
|
step.name,
|
|
'failed',
|
|
Date.now() - startTime,
|
|
step.tags,
|
|
testFile
|
|
);
|
|
|
|
testLogger.endStep(step.name, 'failed');
|
|
}
|
|
}
|
|
|
|
testLogger.endStep('完整的测试流程', 'completed');
|
|
});
|
|
|
|
test('示例9: 测试覆盖率阈值检查', async ({ page, testLogger }) => {
|
|
const threshold = 80;
|
|
|
|
testLogger.startStep('测试覆盖率阈值检查');
|
|
|
|
coverageReporter.recordTestResult('ThresholdTests', '测试1', 'passed', 1000, ['@smoke'], 'threshold.spec.ts');
|
|
coverageReporter.recordTestResult('ThresholdTests', '测试2', 'passed', 1000, ['@smoke'], 'threshold.spec.ts');
|
|
coverageReporter.recordTestResult('ThresholdTests', '测试3', 'passed', 1000, ['@smoke'], 'threshold.spec.ts');
|
|
coverageReporter.recordTestResult('ThresholdTests', '测试4', 'failed', 1000, ['@critical'], 'threshold.spec.ts');
|
|
coverageReporter.recordTestResult('ThresholdTests', '测试5', 'passed', 1000, ['@smoke'], 'threshold.spec.ts');
|
|
|
|
const coverage = coverageReporter.getCoverage();
|
|
|
|
testLogger.info(`通过率: ${coverage.passRate.toFixed(2)}%`);
|
|
testLogger.info(`阈值: ${threshold}%`);
|
|
|
|
if (coverage.passRate >= threshold) {
|
|
testLogger.info('✅ 通过率满足阈值要求');
|
|
} else {
|
|
testLogger.error('❌ 通过率不满足阈值要求');
|
|
}
|
|
|
|
testLogger.endStep('测试覆盖率阈值检查', 'completed');
|
|
});
|
|
|
|
test('示例10: 测试结果统计', async ({ page, testLogger }) => {
|
|
testLogger.startStep('测试结果统计');
|
|
|
|
const totalTests = 20;
|
|
const passedTests = 15;
|
|
const failedTests = 3;
|
|
const skippedTests = 2;
|
|
|
|
for (let i = 1; i <= passedTests; i++) {
|
|
coverageReporter.recordTestResult('StatsTests', `通过测试${i}`, 'passed', 1000, ['@smoke'], 'stats.spec.ts');
|
|
}
|
|
|
|
for (let i = 1; i <= failedTests; i++) {
|
|
coverageReporter.recordTestResult('StatsTests', `失败测试${i}`, 'failed', 1000, ['@critical'], 'stats.spec.ts');
|
|
}
|
|
|
|
for (let i = 1; i <= skippedTests; i++) {
|
|
coverageReporter.recordTestResult('StatsTests', `跳过测试${i}`, 'skipped', 0, ['@normal'], 'stats.spec.ts');
|
|
}
|
|
|
|
const coverage = coverageReporter.getCoverage();
|
|
|
|
testLogger.info('测试结果统计:');
|
|
testLogger.info(` 总测试数: ${coverage.totalTests} (期望: ${totalTests})`);
|
|
testLogger.info(` 通过测试: ${coverage.passedTests} (期望: ${passedTests})`);
|
|
testLogger.info(` 失败测试: ${coverage.failedTests} (期望: ${failedTests})`);
|
|
testLogger.info(` 跳过测试: ${coverage.skippedTests} (期望: ${skippedTests})`);
|
|
testLogger.info(` 通过率: ${coverage.passRate.toFixed(2)}%`);
|
|
|
|
expect(coverage.totalTests).toBe(totalTests);
|
|
expect(coverage.passedTests).toBe(passedTests);
|
|
expect(coverage.failedTests).toBe(failedTests);
|
|
expect(coverage.skippedTests).toBe(skippedTests);
|
|
|
|
testLogger.endStep('测试结果统计', 'completed');
|
|
});
|
|
});
|