Chapter 10

Testing and quality

Learn how to use AI to improve testing efficiency and quality, and explore best practices for AI-assisted test case generation, test code writing, test optimization, and more.

Learn AI-driven testing using Sequential Thinking

The application of AI in testing involves multiple aspects, usingStructured thinking methodsCan help you systematically master:

1
Overview of test types
Quickly understand unit tests, integration tests, and E2E tests
2
AI-assisted test case generation
Generation of unit test, integration test, and E2E test cases
3
AI-assisted test code writing
Use of test frameworks, mock data generation, test data preparation
4
AI-assisted test optimization
Performance optimization, coverage analysis, test refactoring
5
AI-driven test automation
CI/CD integration, test report generation, defect prediction

Overview of test types

Quickly understand test types and provide background knowledge for AI practice.

Unit testing

Test a single function or component, with fast feedback and high coverage.

Integration testing

Test the collaboration of multiple modules and verify interfaces and data flows.

E2E testing

End-to-end testing, simulating real user interactions.

Performance testing

Test system performance and load capacity.

Best practices for AI in testing

Use AI to improve testing efficiency and quality, with AI-assisted practices across the full process from test case generation to test automation.

AI-assisted test case generation

Use AI to generate unit test cases

Describe the function’s behavior to AI and have it generate complete test cases:

Prompt template:

I need to generate unit test cases for the following function:

// utils/calculateTotal.ts
export function calculateTotal(items: Item[]): number {
  if (!items || items.length === 0) return 0
  return items.reduce((sum, item) => {
    if (item.price && item.quantity) {
      return sum + item.price * item.quantity
    }
    return sum
  }, 0)
}

interface Item {
  price?: number
  quantity?: number
}

Please use Jest/Vitest to generate test cases, including:
- Normal case tests
- Boundary case tests (empty array, null, undefined)
- Exceptional case tests (missing price or quantity)
- Organize tests using describe and it
- Use clear test descriptions

AI-generated test case examples

// utils/calculateTotal.test.ts
import { describe, it, expect } from 'vitest'
import { calculateTotal } from './calculateTotal'

describe('calculateTotal', () => {
  it('should return 0 for empty array', () => {
    expect(calculateTotal([])).toBe(0)
  })

  it('should return 0 for null or undefined', () => {
    expect(calculateTotal(null as any)).toBe(0)
    expect(calculateTotal(undefined as any)).toBe(0)
  })

  it('should calculate total for valid items', () => {
    const items = [
      { price: 10, quantity: 2 },
      { price: 5, quantity: 3 }
    ]
    expect(calculateTotal(items)).toBe(35) // 10*2 + 5*3 = 35
  })

  it('should ignore items without price', () => {
    const items = [
      { price: 10, quantity: 2 },
      { quantity: 3 }
    ]
    expect(calculateTotal(items)).toBe(20)
  })

  it('should ignore items without quantity', () => {
    const items = [
      { price: 10, quantity: 2 },
      { price: 5 }
    ]
    expect(calculateTotal(items)).toBe(20)
  })

  it('should handle zero price or quantity', () => {
    const items = [
      { price: 0, quantity: 2 },
      { price: 10, quantity: 0 }
    ]
    expect(calculateTotal(items)).toBe(0)
  })
})

AI-generated integration test scenarios

Prompt:

I need to generate integration test scenarios for the user registration API:

API: POST /api/users/register
Request body: { email, password, name }
Response: { id, email, name, createdAt }

Please generate test cases including:
- Successful registration scenario
- Email already exists scenario
- Insufficient password strength scenario
- Missing required field scenario
- Use the test database
- Clean up test data

AI generates the E2E test flow

Prompt:

I need to generate E2E tests for the shopping cart flow:

User flow:
1. Visit the homepage
2. Search for products
3. Add product to cart
4. View cart
5. Checkout
6. Fill in order information
7. Submit order

Please use Playwright to generate complete E2E test code, including:
- Page navigation
- Element locators
- User interactions
- Assertion checks
- Error handling

TDD practice basics

Keep the core concepts and focus on showing how AI assists TDD. Using AI can quickly generate test cases, implementation code, and refactoring suggestions.

Red

Write failing tests and define the expected behavior

Green

Write the minimal implementation to make the tests pass

Refactoring (Refactor)

Optimize code and keep tests passing

AI-assisted TDD

AI can help at every stage of TDD: generating test cases, rapid implementation, and refactoring suggestions. See the "AI-assisted TDD practice" section above for detailed examples and Prompt templates.

Basics of testing tools

Keep the introduction to core tools and focus on showing how AI helps with tool selection.

Jest

JavaScript testing framework with built-in assertions and mocks

Vitest

Fast test framework, native Vite support, TypeScript friendly

Playwright

E2E testing framework with multi-browser support

AI-assisted tool selection

Use AI to choose the right testing tools based on the project's needs. Describe the project characteristics to AI (Next.js, TypeScript, need E2E testing, etc.), and it will recommend the right combination of tools and configuration plan.

Quality assurance basics

Keep the core concepts, and focus on showing how AI helps quality assurance.

Test strategy

  • Test pyramid: a large number of unit tests + a moderate number of integration tests + a small number of E2E tests
  • Prioritize critical paths: prioritize testing core business logic
  • Boundary testing: Test boundary conditions and exceptional cases

Code coverage

  • Line coverage: proportion of lines of code executed
  • Branch coverage: Proportion of executed code branches
  • Goal: Key code 80%+, overall 60%+

AI-assisted quality assurance

AI can analyze test strategies, generate quality reports, and identify uncovered code paths. Refer to the sections above on "AI-assisted coverage analysis" and "AI-driven test automation" for detailed examples and Prompt templates.

Practical examples

Through real code examples, learn how to use AI to improve testing efficiency and quality.

AI-generated test cases

Complete test case generation example (Prompt + result):

// 1. Provide requirements to AI
Prompt: "Generate unit tests for the calculateTotal function, including normal cases, boundary cases, and exception cases..."

// 2. Test cases generated by AI
describe('calculateTotal', () => {
  it('should return 0 for empty array', () => {
    expect(calculateTotal([])).toBe(0)
  })
  
  it('should calculate total for valid items', () => {
    const items = [
      { price: 10, quantity: 2 },
      { price: 5, quantity: 3 }
    ]
    expect(calculateTotal(items)).toBe(35)
  })
  
  // ... more test cases
})

AI-optimized test code

Example of test code optimization (before and after optimization):

Before optimization (45 seconds)
// Each test calls the real API
describe('UserService', () => {
  it('should fetch user', async () => {
    const user = await fetchUser('1') // real API call
    expect(user.id).toBe('1')
  })
  
  it('should update user', async () => {
    const user = await fetchUser('1') // repeated call
    await updateUser('1', { name: 'New' })
    expect(user.name).toBe('New')
  })
})
After AI optimization (5 seconds)
// AI suggestion: use Mock instead of the real API
vi.mock('./api', () => ({
  fetchUser: vi.fn().mockResolvedValue({ id: '1', name: 'Test' }),
  updateUser: vi.fn().mockResolvedValue({ id: '1', name: 'New' })
}))

describe('UserService', () => {
  it('should fetch user', async () => {
    const user = await fetchUser('1')
    expect(user.id).toBe('1')
  })
  
  it('should update user', async () => {
    await updateUser('1', { name: 'New' })
    expect(updateUser).toHaveBeenCalledWith('1', { name: 'New' })
  })
})

AI analysis coverage

Coverage analysis and supplementary recommendation examples:

// AI coverage report
Coverage: 65%

Uncovered code:
- utils/validation.ts: 45%
  - Error handling branch of validateEmail is untested
  - Boundary cases of validatePassword are untested

AI-suggested test case additions:
it('should return false for invalid email', () => {
  expect(validateEmail('invalid')).toBe(false)
  expect(validateEmail('invalid@')).toBe(false)
  expect(validateEmail('@domain.com')).toBe(false)
})

Learning outcomes

After completing this chapter, you will:

  • 1Understand the overview of test types (unit tests, integration tests, E2E tests, performance tests) and their applicable scenarios
  • 2Master how to use AI to generate test cases (unit tests, integration tests, E2E test case generation)
  • 3Able to use AI to assist with test code writing (test framework syntax, mock data generation, test data preparation)
  • 4Able to use AI to analyze and optimize test performance (performance bottleneck analysis, test execution speed optimization, test refactoring)
  • 5Master the methods of AI-assisted coverage analysis (coverage report analysis, identifying uncovered code paths, suggesting additional test cases)
  • 6Can use AI-driven test automation (CI/CD configuration generation, test report analysis, test failure root cause prediction)
  • 7Master AI-assisted TDD practices (test case generation, minimal implementation code generation, refactoring suggestions)