本文档描述了学生知识图谱可视化系统的测试策略、测试用例和运行方法。系统采用PHPUnit进行单元测试和集成测试,确保代码质量和功能正确性。
FilamentAdmin/tests/
├── Unit/
│ ├── StudentKnowledgeGraphTest.php # 页面和组件单元测试
│ └── Services/
│ └── KnowledgeGraphServiceTest.php # 服务层单元测试
└── Feature/
└── StudentKnowledgeGraphIntegrationTest.php # 集成测试
测试范围:
StudentKnowledgeGraphPage)StudentKnowledgeGraph)测试用例:
测试范围:
KnowledgeGraphService 服务类测试用例:
测试范围:
测试用例:
cd /Volumes/T9/code/math/apis/FilamentAdmin
./run-tests.sh
# 运行所有测试
php artisan test
# 运行特定测试文件
php artisan test --filter=StudentKnowledgeGraphTest
php artisan test --filter=KnowledgeGraphServiceTest
php artisan test --filter=StudentKnowledgeGraphIntegrationTest
# 运行特定测试套件
php artisan test --testsuite=Unit
php artisan test --testsuite=Feature
# 运行特定测试方法
php artisan test --filter="it_initializes_with_correct_base_url"
# 运行单元测试
php artisan test --testsuite=Unit --filter="KnowledgeGraph"
# 运行集成测试
php artisan test --testsuite=Feature --filter="StudentKnowledgeGraph"
Student::create([
'student_id' => 1001,
'name' => '张三',
'grade' => '高一',
'class_name' => '1班',
'teacher_id' => 1,
]);
'masteries' => [
[
'kp_code' => 'R01',
'mastery_level' => 0.85,
'confidence_level' => 0.8,
],
[
'kp_code' => 'R02',
'mastery_level' => 0.72,
'confidence_level' => 0.75,
],
],
Http::fake([
'localhost:5010/api/mastery/*' => Http::response([
'masteries' => [...],
], 200),
]);
// 页面内容断言
$this->assertSee('学生知识图谱');
$this->assertSee('选择学生');
// 数据断言
$this->assertNotNull($component->selectedStudent);
$this->assertEquals('张三', $component->selectedStudent->name);
$this->assertCount(2, $component->students);
// 数组结构断言
$this->assertArrayHasKey('nodes', $component->knowledgePoints);
$this->assertArrayHasKey('links', $component->knowledgePoints);
// 响应状态断言
$response->assertStatus(200);
$response->assertOk();
Http::fake([
'localhost:5010/*' => Http::response($data, 200),
]);
Http::fake([
'localhost:5010/*' => Http::throw(new \Exception('API Error')),
]);
<phpunit xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:noNamespaceSchemaLocation="https://schema.phpunit.de/11.0/phpunit.xsd"
bootstrap="tests/bootstrap.php"
colors="true">
<testsuites>
<testsuite name="Unit">
<directory suffix="Test.php">./tests/Unit</directory>
</testsuite>
<testsuite name="Feature">
<directory suffix="Test.php">./tests/Feature</directory>
</testsuite>
</testsuites>
</phpunit>
# 生成HTML覆盖率报告
php artisan test --coverage-html coverage/
# 查看覆盖率概览
php artisan test --coverage
it_ 开头或 test_ 前缀明确测试意图和行为
public function it_returns_correct_mastery_colors()
public function test_loads_student_data_successfully()
setUp() 和 tearDown() 方法测试后清理数据
protected function setUp(): void
{
parent::setUp();
$this->service = new KnowledgeGraphService();
}
避免硬编码数据
use LazilyRefreshDatabase;
class StudentKnowledgeGraphTest extends TestCase
{
use LazilyRefreshDatabase;
}
模拟异常情况
Http::fake([
'localhost:5010/*' => Http::response($mockData, 200),
]);
包含错误情况的测试
// 好的测试
$this->assertEquals('#10b981', $color);
// 避免
$this->assertTrue($color === '#10b981');
创建 .github/workflows/tests.yml:
name: Tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v2
- name: Setup PHP
uses: shivammathur/setup-php@v2
with:
php-version: '8.2'
- name: Install dependencies
run: composer install --no-progress
- name: Run tests
run: ./run-tests.sh
问题1: 测试失败 - 数据库未找到
# 解决方案:确保测试数据库配置正确
php artisan migrate --env=testing
问题2: Mock不生效
# 解决方案:检查URL匹配模式
Http::fake([
'localhost:5010/*' => Http::response($data, 200), // 使用通配符
]);
问题3: Livewire测试失败
// 解决方案:使用正确的测试方法
$this->livewire(Component::class)
->set('property', 'value')
->call('method')
->assertSee('Expected Text');
问题4: 权限错误
# 解决方案:确保测试文件可读
chmod -R 755 tests/
// 使用dd()调试
$component = new StudentKnowledgeGraph();
dd($component->students);
// 使用Log调试
\Log::info('Debug info', $context);
// 跳过某些测试
public function test_pending_feature(): void
{
$this->markTestSkipped('Feature pending implementation');
}
| 日期 | 版本 | 更新内容 |
|---|---|---|
| 2025-11-23 | v1.0 | 初始版本,添加基本测试 |
| 2025-11-23 | v1.1 | 添加集成测试和测试运行脚本 |
提交新测试时,请确保:
维护者: Claude Code 最后更新: 2025-11-23