testing

Compare original and translation side by side

🇺🇸

Original

English
🇨🇳

Translation

Chinese

Testing Software

软件测试

MCP Tools

MCP工具

Chrome DevTools (E2E testing):
  • Automate user flows in real browser
  • Capture screenshots for visual regression
  • Run Lighthouse for accessibility testing
  • Profile performance during test runs
Chrome DevTools (E2E测试):
  • 在真实浏览器中自动化用户流程
  • 捕获截图用于视觉回归测试
  • 运行Lighthouse进行可访问性测试
  • 在测试运行期间分析性能

Testing Pyramid

测试金字塔

  1. Unit Tests (Many): Fast, isolated, test single units
  2. Integration Tests (Some): Test component interactions
  3. E2E Tests (Few): Test complete user flows — use Chrome DevTools
  1. 单元测试(数量多):快速、独立,测试单个单元
  2. 集成测试(数量中等):测试组件间的交互
  3. E2E测试(数量少):测试完整用户流程 —— 使用Chrome DevTools

Workflows

工作流程

  • Analyze: Use Glob and Grep to identify untested code
  • Unit Tests: Cover all public functions
  • Edge Cases: Test boundaries and error conditions
  • Integration: Test external dependencies
  • E2E: Use Chrome DevTools for browser automation
  • Regression: Add test for each bug fix
  • 分析:使用Glob和Grep识别未测试代码
  • 单元测试:覆盖所有公共函数
  • 边界情况:测试边界值与错误场景
  • 集成测试:测试外部依赖
  • E2E测试:使用Chrome DevTools进行浏览器自动化
  • 回归测试:为每个bug修复添加测试

Test Quality Standards

测试质量标准

Deterministic

确定性

Tests must produce the same result every time.
测试每次运行必须产生相同结果。

Isolated

独立性

Tests should not depend on each other or shared state.
测试不应依赖彼此或共享状态。

Clear

清晰性

Test names should describe the behavior being tested.
测试名称应描述所测试的行为。

Test Patterns

测试模式

Arrange-Act-Assert (AAA) (Java + JUnit 5)

Arrange-Act-Assert (AAA) (Java + JUnit 5)

java
@ExtendWith(MockitoExtension.class)
class UserServiceTest {
    @Mock
    private EmailService emailService;

    @InjectMocks
    private UserService userService;

    @Test
    void registerUser_ValidEmail_SendsWelcomeEmail() {
        // Arrange
        String email = "test@example.com";
        ArgumentCaptor<Email> emailCaptor = ArgumentCaptor.forClass(Email.class);

        // Act
        userService.register(email);

        // Assert
        verify(emailService).send(emailCaptor.capture());
        Email sentEmail = emailCaptor.getValue();
        assertThat(sentEmail.getTo()).isEqualTo("test@example.com");
        assertThat(sentEmail.getSubject()).isEqualTo("Welcome!");
    }
}
java
@ExtendWith(MockitoExtension.class)
class UserServiceTest {
    @Mock
    private EmailService emailService;

    @InjectMocks
    private UserService userService;

    @Test
    void registerUser_ValidEmail_SendsWelcomeEmail() {
        // Arrange
        String email = "test@example.com";
        ArgumentCaptor<Email> emailCaptor = ArgumentCaptor.forClass(Email.class);

        // Act
        userService.register(email);

        // Assert
        verify(emailService).send(emailCaptor.capture());
        Email sentEmail = emailCaptor.getValue();
        assertThat(sentEmail.getTo()).isEqualTo("test@example.com");
        assertThat(sentEmail.getSubject()).isEqualTo("Welcome!");
    }
}

E2E Testing with Chrome DevTools

使用Chrome DevTools进行E2E测试

javascript
// Use Chrome DevTools MCP for browser automation
// - Navigate to pages
// - Fill forms and click buttons
// - Capture screenshots for visual regression
// - Run Lighthouse accessibility audits
// - Check console for errors
javascript
// Use Chrome DevTools MCP for browser automation
// - Navigate to pages
// - Fill forms and click buttons
// - Capture screenshots for visual regression
// - Run Lighthouse accessibility audits
// - Check console for errors

Commands (Java/Maven)

命令 (Java/Maven)

bash
undefined
bash
undefined

Run unit tests

Run unit tests

mvn test
mvn test

Run tests with coverage

Run tests with coverage

mvn verify
mvn verify

Run specific test class

Run specific test class

mvn test -Dtest=UserServiceTest
mvn test -Dtest=UserServiceTest

Run specific test method

Run specific test method

mvn test -Dtest=UserServiceTest#registerUser_ValidEmail_SendsWelcomeEmail
mvn test -Dtest=UserServiceTest#registerUser_ValidEmail_SendsWelcomeEmail

Generate coverage report

Generate coverage report

mvn jacoco:report
mvn jacoco:report

View coverage report

View coverage report

open target/site/jacoco/index.html
undefined
open target/site/jacoco/index.html
undefined

Finding Untested Code

查找未测试代码

Use Glob and Grep to identify gaps:
  1. Use Glob to find all source files and test files
  2. Check which source files have corresponding test files
  3. Use Grep to see if functions are referenced in tests
使用Glob和Grep识别测试缺口:
  1. 使用Glob查找所有源文件和测试文件
  2. 检查哪些源文件有对应的测试文件
  3. 使用Grep查看函数是否在测试中被引用