Token导航 LogoToken导航

Agent Skill · 前端设计

testing-expert(测试专家)

用于辅助安全审计、权限检查、凭据风险、认证流程和常见漏洞排查。它适合让 Agent 梳理敏感配置、检查依赖风险、分析鉴权逻辑或生成安全复核清单。使用时不能把工具输出直接当最终结论,涉及密钥、令牌、用户数据或生产系统时,应先确认最小权限、脱敏方式和操作边界。

testing-expert 是前端设计类 Agent Skill,可用宿主为 Codex、Claude、Cursor、Gemini CLI,通过GitHub 仓库获取,权限为需要联网;许可证为 MIT,第三方扫描已通过,按安装量在前端设计类的 10,216 个 Skill 里排第 2,227。

总安装 2,596周安装 105GitHub Stars 19下载 815核验 2026-05-01

安装说明

本站只整理中文说明,不托管安装包,也不代用户安装。
GitHub
许可证
MIT

安装方式

通过对话安装

复制提示词发给支持本地命令或 Skills 的 AI 助手,先确认命令和权限,再让它执行。

请帮我安装这个 Agent Skill:testing-expert(测试专家)
来源仓库:https://github.com/personamanagmentlayer/pcl
仓库路径:skills/testing-expert
安装命令:
npx skills add https://github.com/personamanagmentlayer/pcl --skill testing-expert
安装前请先检查当前环境是否支持对应 CLI,并向我确认将要执行的命令、安装目录、联网范围和文件读写权限;确认后再执行。

命令行安装

复制命令到本机终端执行。不同来源提供的安装方式可能略有差异;本站展示可直接复制的安装命令,安装前请核对来源页面。

skills.shnpx skills
npx skills add https://github.com/personamanagmentlayer/pcl --skill testing-expert

这个 Skill 是做什么的

前端设计类 Skill 关注页面结构、视觉规范和组件写法,输出通常是可运行的界面代码。

用于深度审查系统安全与权限控制机制。适用宿主包括 Codex、Claude、Cursor、Gemini CLI,接入前应确认版本、权限和运行环境要求。适合梳理敏感配置、依赖漏洞与认证流程缺陷。提供常见攻击向量的检测清单与加固方案。不能将工具输出直接视为最终结论需人工复核。

testing-expert 属于前端设计类 Skill,可作为该场景下的辅助能力补充。

来源仓库内路径:skills/testing-expert

上手步骤

4 步
  1. 1

    确认自己的宿主工具

    公开资料标注支持 Codex、Claude、Cursor、Gemini CLI;先确认自己用的是其中之一,再按对应工具的 Skills 目录放置。

  2. 2

    拿到安装命令

    可以直接复制的命令是 npx skills add https://github.com/personamanagmentlayer/pcl --skill testing-expe…;命令里的占位符换成自己的路径或密钥再执行。

  3. 3

    安装后重启宿主

    命令执行完要完全退出宿主工具再打开,让新装的 Skill 被重新扫描到,否则列表里可能看不到。

  4. 4

    按最小权限试用

    它需要需要联网,先在测试目录或测试账号里跑,确认没问题再放到正式项目。

常见问题

6 条

testing-expert 怎么安装?

在终端执行 npx skills add https://github.com/personamanagmentlayer/pcl --skill testing-expert。装完完全退出宿主工具再打开,让它重新扫描本地 Skill。

testing-expert 支持哪些 AI 工具?

公开资料标注支持 Codex、Claude、Cursor、Gemini CLI。其他工具只要能读取本地 Skill 目录,一般也能手动放进去,但要自己确认触发方式是否一致。

testing-expert 需要什么权限?

权限级别是需要联网。该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

testing-expert 是免费的吗?

来源标注的许可证是 MIT。本站不代收任何费用,也不托管安装包;能否商用、要不要署名,以许可证原文为准。

testing-expert 用的人多吗?

公开数据是累计安装 2,596 次,近一周安装 105 次,下载 815 次,GitHub 19 星。这些数字来自第三方目录统计,只能当作热度参考,不代表质量或安全性。

testing-expert 安全吗?

第三方扫描结果为通过,但这只覆盖已知的静态检查项,不能替代自己读一遍源码和安装命令。

值得选的地方和要先想清楚的地方

值得选的地方

  • 同时适配 4 种工具,换客户端不用重新找替代品。
  • 3 个目录同时收录,说明这个 Skill 流传比较广。
  • 许可证明确为 MIT,商用和分发边界好判断。
  • 第三方扫描已通过,常见的高风险行为没有命中。

先想清楚的地方

  • 第三方目录整理,版本更新和行为细节以来源仓库为准。

适不适合你

更适合

  • 已经在用 Codex,想直接扩展它能力的人。
  • 需要处理前端设计类任务、又不想每次都手写提示词的人。
  • 需要许可证清楚、并且有第三方扫描结果的团队项目。

不太适合

  • 需要厂商级技术支持和 SLA 的生产系统。

前端设计类 Skill 对比

6 个同类
Skill总安装GitHub 星权限许可证扫描
frontend-design

前端设计 · Codex、Claude

8,823,360126,072只读MIT通过
vercel-composition-patterns

Vercel 组合模式 · Codex、Claude

3,858,81625,893需要联网MIT通过
vercel-react-native-skills

Vercel React native skills 前端 · Codex、Claude

2,468,66425,878只读MIT通过
adapt

adapt 搜索 · Codex、Claude

2,027,04023,125操作浏览器未明确通过
bolder

bolder 分析 · Codex、Claude

2,004,28823,130只读未明确通过
testing-expert

测试专家 · 本页

2,59619需要联网MIT通过

按安装量取同分类前几名;数字来自第三方目录统计,只作热度参考,不代表质量结论。

SKILL.md 原文

Testing Expert

You are an expert in software testing with deep knowledge of testing methodologies, frameworks, and best practices. You write comprehensive test suites that ensure code quality, prevent regressions, and document expected behavior.

Core Expertise

Testing Fundamentals

Test Pyramid:

        /\
       /E2E\        <- Few, slow, expensive
      /------\
     /  API  \      <- More, medium speed
    /--------\
   /   Unit   \     <- Many, fast, cheap
  /------------\

Testing Principles:

  1. Fast: Tests should run quickly
  2. Isolated: Tests should not depend on each other
  3. Repeatable: Same input = same output
  4. Self-checking: Tests assert their own results
  5. Timely: Write tests before or with code (TDD)

Test Coverage Goals:

  • Unit tests: 80-90% coverage
  • Integration tests: Critical paths
  • E2E tests: User journeys
  • Focus on important code, not 100% coverage

Unit Testing

JavaScript/TypeScript (Vitest):

import { describe, it, expect, vi, beforeEach, afterEach } from 'vitest';
import { UserService } from './user-service';
import { Database } from './database';

describe('UserService', () => {
  let service: UserService;
  let mockDb: Database;

  beforeEach(() => {
    mockDb = {
      query: vi.fn(),
      execute: vi.fn(),
    } as any;
    service = new UserService(mockDb);
  });

  afterEach(() => {
    vi.clearAllMocks();
  });

  describe('getUser', () => {
    it('should return user when found', async () => {
      // Arrange
      const mockUser = { id: 1, name: 'Alice', email: 'alice@example.com' };
      mockDb.query.mockResolvedValue([mockUser]);

      // Act
      const result = await service.getUser(1);

      // Assert
      expect(result).toEqual(mockUser);
      expect(mockDb.query).toHaveBeenCalledWith(
        'SELECT * FROM users WHERE id = ?',
        [1]
      );
    });

    it('should return null when user not found', async () => {
      mockDb.query.mockResolvedValue([]);

      const result = await service.getUser(999);

      expect(result).toBeNull();
    });

    it('should throw error on database failure', async () => {
      mockDb.query.mockRejectedValue(new Error('Database error'));

      await expect(service.getUser(1)).rejects.toThrow('Database error');
    });
  });

  describe('createUser', () => {
    it('should create user with valid data', async () => {
      const userData = { name: 'Bob', email: 'bob@example.com' };
      mockDb.execute.mockResolvedValue({ insertId: 2 });

      const result = await service.createUser(userData);

      expect(result).toEqual({ id: 2, ...userData });
      expect(mockDb.execute).toHaveBeenCalledWith(
        'INSERT INTO users (name, email) VALUES (?, ?)',
        ['Bob', 'bob@example.com']
      );
    });

    it('should validate email format', async () => {
      const userData = { name: 'Bob', email: 'invalid-email' };

      await expect(service.createUser(userData)).rejects.toThrow(
        'Invalid email format'
      );
      expect(mockDb.execute).not.toHaveBeenCalled();
    });
  });
});

// Parametrized tests
describe('validateEmail', () => {
  it.each([
    ['test@example.com', true],
    ['user+tag@domain.co.uk', true],
    ['invalid', false],
    ['@example.com', false],
    ['test@', false],
    ['', false],
  ])('should validate "%s" as %s', (email, expected) => {
    expect(validateEmail(email)).toBe(expected);
  });
});

Python (Pytest):

import pytest
from unittest.mock import Mock, patch, MagicMock
from user_service import UserService
from database import Database

class TestUserService:
    @pytest.fixture
    def mock_db(self):
        return Mock(spec=Database)

    @pytest.fixture
    def service(self, mock_db):
        return UserService(mock_db)

    def test_get_user_found(self, service, mock_db):
        # Arrange
        mock_user = {'id': 1, 'name': 'Alice', 'email': 'alice@example.com'}
        mock_db.query.return_value = [mock_user]

        # Act
        result = service.get_user(1)

        # Assert
        assert result == mock_user
        mock_db.query.assert_called_once_with(
            'SELECT * FROM users WHERE id = ?',
            (1,)
        )

    def test_get_user_not_found(self, service, mock_db):
        mock_db.query.return_value = []

        result = service.get_user(999)

        assert result is None

    def test_get_user_database_error(self, service, mock_db):
        mock_db.query.side_effect = Exception('Database error')

        with pytest.raises(Exception, match='Database error'):
            service.get_user(1)

    def test_create_user_valid(self, service, mock_db):
        user_data = {'name': 'Bob', 'email': 'bob@example.com'}
        mock_db.execute.return_value = {'insert_id': 2}

        result = service.create_user(user_data)

        assert result == {'id': 2, **user_data}
        mock_db.execute.assert_called_once()

    @pytest.mark.parametrize('email,expected', [
        ('test@example.com', True),
        ('user+tag@domain.co.uk', True),
        ('invalid', False),
        ('@example.com', False),
        ('test@', False),
        ('', False),
    ])
    def test_validate_email(self, email, expected):
        assert validate_email(email) == expected

    @pytest.mark.asyncio
    async def test_async_function(self, service):
        result = await service.fetch_user_async(1)
        assert result is not None

Go:

package user

import (
    "testing"
    "errors"
    "github.com/stretchr/testify/assert"
    "github.com/stretchr/testify/mock"
)

// Mock database
type MockDatabase struct {
    mock.Mock
}

func (m *MockDatabase) Query(query string, args ...interface{}) ([]User, error) {
    ret := m.Called(query, args)
    return ret.Get(0).([]User), ret.Error(1)
}

func TestGetUser(t *testing.T) {
    // Arrange
    mockDB := new(MockDatabase)
    service := NewUserService(mockDB)
    expectedUser := User{ID: 1, Name: "Alice", Email: "alice@example.com"}

    mockDB.On("Query", "SELECT * FROM users WHERE id = ?", 1).
        Return([]User{expectedUser}, nil)

    // Act
    user, err := service.GetUser(1)

    // Assert
    assert.NoError(t, err)
    assert.Equal(t, expectedUser, user)
    mockDB.AssertExpectations(t)
}

func TestGetUserNotFound(t *testing.T) {
    mockDB := new(MockDatabase)
    service := NewUserService(mockDB)

    mockDB.On("Query", "SELECT * FROM users WHERE id = ?", 999).
        Return([]User{}, nil)

    user, err := service.GetUser(999)

    assert.NoError(t, err)
    assert.Nil(t, user)
}

// Table-driven tests
func TestValidateEmail(t *testing.T) {
    tests := []struct {
        name     string
        email    string
        expected bool
    }{
        {"valid email", "test@example.com", true},
        {"valid with plus", "user+tag@example.com", true},
        {"invalid no @", "invalid", false},
        {"invalid no domain", "test@", false},
        {"empty", "", false},
    }

    for _, tt := range tests {
        t.Run(tt.name, func(t *testing.T) {
            result := ValidateEmail(tt.email)
            assert.Equal(t, tt.expected, result)
        })
    }
}

// Benchmarks
func BenchmarkGetUser(b *testing.B) {
    mockDB := new(MockDatabase)
    service := NewUserService(mockDB)
    mockDB.On("Query", mock.Anything, mock.Anything).
        Return([]User{{ID: 1, Name: "Test"}}, nil)

    b.ResetTimer()
    for i := 0; i < b.N; i++ {
        service.GetUser(1)
    }
}

Java (JUnit 5):

import org.junit.jupiter.api.*;
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.provider.ValueSource;
import org.junit.jupiter.params.provider.CsvSource;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;

class UserServiceTest {
    private UserService service;
    private Database mockDb;

    @BeforeEach
    void setUp() {
        mockDb = mock(Database.class);
        service = new UserService(mockDb);
    }

    @AfterEach
    void tearDown() {
        reset(mockDb);
    }

    @Test
    @DisplayName("Should return user when found")
    void shouldReturnUserWhenFound() {
        // Arrange
        User expectedUser = new User(1, "Alice", "alice@example.com");
        when(mockDb.query(anyString(), eq(1)))
            .thenReturn(List.of(expectedUser));

        // Act
        User result = service.getUser(1);

        // Assert
        assertNotNull(result);
        assertEquals(expectedUser.getName(), result.getName());
        verify(mockDb).query(
            "SELECT * FROM users WHERE id = ?",
            1
        );
    }

    @Test
    @DisplayName("Should return null when user not found")
    void shouldReturnNullWhenNotFound() {
        when(mockDb.query(anyString(), anyInt()))
            .thenReturn(Collections.emptyList());

        User result = service.getUser(999);

        assertNull(result);
    }

    @Test
    @DisplayName("Should throw exception on database error")
    void shouldThrowOnDatabaseError() {
        when(mockDb.query(anyString(), anyInt()))
            .thenThrow(new DatabaseException("Connection failed"));

        assertThrows(DatabaseException.class, () -> {
            service.getUser(1);
        });
    }

    @ParameterizedTest
    @ValueSource(strings = {
        "test@example.com",
        "user+tag@example.com"
    })
    @DisplayName("Should accept valid emails")
    void shouldAcceptValidEmails(String email) {
        assertTrue(service.validateEmail(email));
    }

    @ParameterizedTest
    @CsvSource({
        "invalid, false",
        "@example.com, false",
        "test@, false",
        "'', false"
    })
    @DisplayName("Should reject invalid emails")
    void shouldRejectInvalidEmails(String email, boolean expected) {
        assertEquals(expected, service.validateEmail(email));
    }

    @Nested
    @DisplayName("Create user tests")
    class CreateUserTests {
        @Test
        void shouldCreateUserWithValidData() {
            UserData data = new UserData("Bob", "bob@example.com");
            when(mockDb.execute(anyString(), any()))
                .thenReturn(2);

            User result = service.createUser(data);

            assertNotNull(result);
            assertEquals(2, result.getId());
            assertEquals("Bob", result.getName());
        }

        @Test
        void shouldValidateEmailBeforeCreating() {
            UserData data = new UserData("Bob", "invalid-email");

            assertThrows(ValidationException.class, () -> {
                service.createUser(data);
            });

            verify(mockDb, never()).execute(anyString(), any());
        }
    }
}

Integration Testing

API Integration Tests (Supertest + Express):

import request from 'supertest';
import { app } from '../app';
import { database } from '../database';

describe('User API Integration Tests', () => {
  beforeAll(async () => {
    await database.connect();
  });

  afterAll(async () => {
    await database.disconnect();
  });

  beforeEach(async () => {
    await database.clear();
  });

  describe('POST /api/users', () => {
    it('should create a new user', async () => {
      const userData = {
        name: 'Alice',
        email: 'alice@example.com',
        age: 30,
      };

      const response = await request(app)
        .post('/api/users')
        .send(userData)
        .expect(201);

      expect(response.body).toMatchObject({
        id: expect.any(Number),
        name: 'Alice',
        email: 'alice@example.com',
        age: 30,
      });

      // Verify in database
      const users = await database.query('SELECT * FROM users WHERE id = ?', [
        response.body.id,
      ]);
      expect(users).toHaveLength(1);
      expect(users[0].name).toBe('Alice');
    });

    it('should return 400 for invalid email', async () => {
      const response = await request(app)
        .post('/api/users')
        .send({ name: 'Bob', email: 'invalid' })
        .expect(400);

      expect(response.body).toHaveProperty('error');
      expect(response.body.error).toContain('email');
    });
  });

  describe('GET /api/users/:id', () => {
    it('should return user by id', async () => {
      // Create user in database
      const userId = await database.execute(
        'INSERT INTO users (name, email) VALUES (?, ?)',
        ['Charlie', 'charlie@example.com']
      );

      const response = await request(app)
        .get(`/api/users/${userId}`)
        .expect(200);

      expect(response.body).toMatchObject({
        id: userId,
        name: 'Charlie',
        email: 'charlie@example.com',
      });
    });

    it('should return 404 for non-existent user', async () => {
      await request(app).get('/api/users/999').expect(404);
    });
  });
});

Database Integration Tests (Python + SQLAlchemy):

import pytest
from sqlalchemy import create_engine
from sqlalchemy.orm import sessionmaker
from models import Base, User
from repositories import UserRepository

@pytest.fixture(scope='module')
def engine():
    # Use in-memory SQLite for tests
    engine = create_engine('sqlite:///:memory:')
    Base.metadata.create_all(engine)
    yield engine
    engine.dispose()

@pytest.fixture
def db_session(engine):
    Session = sessionmaker(bind=engine)
    session = Session()
    yield session
    session.rollback()
    session.close()

@pytest.fixture
def user_repository(db_session):
    return UserRepository(db_session)

class TestUserRepository:
    def test_create_user(self, user_repository, db_session):
        user = user_repository.create(
            name='Alice',
            email='alice@example.com'
        )

        assert user.id is not None
        assert user.name == 'Alice'

        # Verify in database
        db_user = db_session.query(User).filter_by(id=user.id).first()
        assert db_user is not None
        assert db_user.name == 'Alice'

    def test_find_by_email(self, user_repository, db_session):
        # Create user
        user_repository.create(name='Bob', email='bob@example.com')

        # Find by email
        found = user_repository.find_by_email('bob@example.com')

        assert found is not None
        assert found.name == 'Bob'

    def test_update_user(self, user_repository):
        user = user_repository.create(name='Charlie', email='charlie@example.com')

        user_repository.update(user.id, name='Charles')

        updated = user_repository.find_by_id(user.id)
        assert updated.name == 'Charles'

End-to-End Testing

Playwright (Modern E2E):

import { test, expect } from '@playwright/test';

test.describe('User Registration Flow', () => {
  test.beforeEach(async ({ page }) => {
    await page.goto('http://localhost:3000');
  });

  test('should register new user successfully', async ({ page }) => {
    // Navigate to registration
    await page.click('text=Sign Up');

    // Fill form
    await page.fill('input[name="name"]', 'Alice');
    await page.fill('input[name="email"]', 'alice@example.com');
    await page.fill('input[name="password"]', 'SecurePass123!');
    await page.fill('input[name="confirmPassword"]', 'SecurePass123!');

    // Submit form
    await page.click('button[type="submit"]');

    // Wait for success message
    await expect(page.locator('text=Registration successful')).toBeVisible();

    // Verify redirect to dashboard
    await expect(page).toHaveURL(/.*dashboard/);
    await expect(page.locator('text=Welcome, Alice')).toBeVisible();
  });

  test('should show error for invalid email', async ({ page }) => {
    await page.click('text=Sign Up');
    await page.fill('input[name="email"]', 'invalid-email');
    await page.fill('input[name="password"]', 'password');

    await page.click('button[type="submit"]');

    await expect(page.locator('text=Invalid email format')).toBeVisible();
  });

  test('should show error for weak password', async ({ page }) => {
    await page.click('text=Sign Up');
    await page.fill('input[name="password"]', '123');

    await page.click('button[type="submit"]');

    await expect(
      page.locator('text=Password must be at least 8 characters')
    ).toBeVisible();
  });
});

test.describe('User Login Flow', () => {
  test('should login with valid credentials', async ({ page }) => {
    await page.goto('http://localhost:3000/login');

    await page.fill('input[name="email"]', 'test@example.com');
    await page.fill('input[name="password"]', 'TestPass123!');
    await page.click('button[type="submit"]');

    await expect(page).toHaveURL(/.*dashboard/);
  });

  test('should show error for invalid credentials', async ({ page }) => {
    await page.goto('http://localhost:3000/login');

    await page.fill('input[name="email"]', 'test@example.com');
    await page.fill('input[name="password"]', 'WrongPassword');
    await page.click('button[type="submit"]');

    await expect(page.locator('text=Invalid credentials')).toBeVisible();
  });
});

Cypress:

describe('Shopping Cart', () => {
  beforeEach(() => {
    cy.visit('/');
    cy.login('user@example.com', 'password');
  });

  it('should add product to cart', () => {
    cy.get('[data-testid="product-1"]').click();
    cy.get('[data-testid="add-to-cart"]').click();

    cy.get('[data-testid="cart-icon"]').should('contain', '1');

    cy.get('[data-testid="cart-icon"]').click();
    cy.get('[data-testid="cart-items"]')
      .should('have.length', 1)
      .first()
      .should('contain', 'Product Name');
  });

  it('should complete checkout process', () => {
    // Add products
    cy.get('[data-testid="product-1"]').click();
    cy.get('[data-testid="add-to-cart"]').click();

    // Go to cart
    cy.get('[data-testid="cart-icon"]').click();

    // Proceed to checkout
    cy.get('[data-testid="checkout-button"]').click();

    // Fill shipping info
    cy.get('input[name="address"]').type('123 Main St');
    cy.get('input[name="city"]').type('New York');
    cy.get('input[name="zip"]').type('10001');

    // Fill payment info
    cy.get('input[name="cardNumber"]').type('4111111111111111');
    cy.get('input[name="expiry"]').type('12/25');
    cy.get('input[name="cvv"]').type('123');

    // Submit order
    cy.get('[data-testid="place-order"]').click();

    // Verify success
    cy.get('[data-testid="order-success"]').should('be.visible');
    cy.url().should('include', '/order-confirmation');
  });
});

// Custom commands
Cypress.Commands.add('login', (email: string, password: string) => {
  cy.session([email, password], () => {
    cy.visit('/login');
    cy.get('input[name="email"]').type(email);
    cy.get('input[name="password"]').type(password);
    cy.get('button[type="submit"]').click();
    cy.url().should('include', '/dashboard');
  });
});

Test-Driven Development (TDD)

TDD Workflow:

1. Write failing test (RED)
2. Write minimum code to pass (GREEN)
3. Refactor code (REFACTOR)
4. Repeat

TDD Example:

// Step 1: Write failing test (RED)
describe('Calculator', () => {
  it('should add two numbers', () => {
    const calc = new Calculator();
    expect(calc.add(2, 3)).toBe(5);
  });
});

// Test fails: Calculator class doesn't exist

// Step 2: Write minimum code (GREEN)
class Calculator {
  add(a: number, b: number): number {
    return a + b;
  }
}

// Test passes

// Step 3: Add more tests
it('should subtract two numbers', () => {
  const calc = new Calculator();
  expect(calc.subtract(5, 3)).toBe(2);
});

// Test fails: subtract method doesn't exist

// Step 4: Implement
class Calculator {
  add(a: number, b: number): number {
    return a + b;
  }

  subtract(a: number, b: number): number {
    return a - b;
  }
}

// Test passes

// Step 5: Refactor if needed
// Code is simple, no refactoring needed

Behavior-Driven Development (BDD)

Cucumber/Gherkin:

Feature: User Authentication
  As a user
  I want to log in to the application
  So that I can access my account

  Background:
    Given the user "alice@example.com" exists with password "SecurePass123"

  Scenario: Successful login
    Given I am on the login page
    When I enter email "alice@example.com"
    And I enter password "SecurePass123"
    And I click the login button
    Then I should be redirected to the dashboard
    And I should see "Welcome, Alice"

  Scenario: Failed login with wrong password
    Given I am on the login page
    When I enter email "alice@example.com"
    And I enter password "WrongPassword"
    And I click the login button
    Then I should see an error message "Invalid credentials"
    And I should remain on the login page

  Scenario Outline: Email validation
    Given I am on the registration page
    When I enter email "<email>"
    Then I should see "<message>"

    Examples:
      | email              | message                   |
      | alice@example.com  |                           |
      | invalid            | Invalid email format      |
      | @example.com       | Invalid email format      |
      |                    | Email is required         |

Step Definitions:

import { Given, When, Then } from '@cucumber/cucumber';
import { expect } from '@playwright/test';

Given('the user {string} exists with password {string}', async function (email, password) {
  await this.database.createUser({ email, password });
});

Given('I am on the login page', async function () {
  await this.page.goto('http://localhost:3000/login');
});

When('I enter email {string}', async function (email) {
  await this.page.fill('input[name="email"]', email);
});

When('I enter password {string}', async function (password) {
  await this.page.fill('input[name="password"]', password);
});

When('I click the login button', async function () {
  await this.page.click('button[type="submit"]');
});

Then('I should be redirected to the dashboard', async function () {
  await expect(this.page).toHaveURL(/.*dashboard/);
});

Then('I should see {string}', async function (text) {
  await expect(this.page.locator(`text=${text}`)).toBeVisible();
});

Best Practices

1. AAA Pattern (Arrange-Act-Assert)

test('should calculate total price', () => {
  // Arrange - Set up test data
  const cart = new ShoppingCart();
  cart.addItem({ name: 'Book', price: 10 });
  cart.addItem({ name: 'Pen', price: 2 });

  // Act - Execute the behavior
  const total = cart.calculateTotal();

  // Assert - Verify the outcome
  expect(total).toBe(12);
});

2. Test Independence

// Bad - tests depend on order
test('create user', () => {
  userId = createUser('Alice'); // Global state
});

test('get user', () => {
  const user = getUser(userId); // Depends on previous test
  expect(user.name).toBe('Alice');
});

// Good - each test is independent
test('create user', () => {
  const userId = createUser('Alice');
  expect(userId).toBeGreaterThan(0);
});

test('get user', () => {
  const userId = createUser('Bob'); // Own setup
  const user = getUser(userId);
  expect(user.name).toBe('Bob');
});

3. Test Naming

// Bad
test('test1', () => { ... });
test('user test', () => { ... });

// Good - descriptive names
test('should return user when ID exists', () => { ... });
test('should throw error when ID is negative', () => { ... });
test('should create user with valid email', () => { ... });

4. One Assertion Per Test (Generally)

// Acceptable for related assertions
test('should create user with correct data', () => {
  const user = createUser({ name: 'Alice', email: 'alice@example.com' });

  expect(user.id).toBeGreaterThan(0);
  expect(user.name).toBe('Alice');
  expect(user.email).toBe('alice@example.com');
  expect(user.createdAt).toBeInstanceOf(Date);
});

// Better - split if testing different behaviors
test('should assign ID to new user', () => {
  const user = createUser({ name: 'Alice', email: 'alice@example.com' });
  expect(user.id).toBeGreaterThan(0);
});

test('should set creation timestamp', () => {
  const user = createUser({ name: 'Alice', email: 'alice@example.com' });
  expect(user.createdAt).toBeInstanceOf(Date);
});

5. Use Test Doubles Appropriately

// Stub - Returns predefined values
const stub = {
  getUser: () => ({ id: 1, name: 'Alice' }),
};

// Mock - Records interactions and can verify them
const mock = vi.fn().mockReturnValue({ id: 1, name: 'Alice' });
service.getUser(1);
expect(mock).toHaveBeenCalledWith(1);

// Spy - Wraps real object and records calls
const spy = vi.spyOn(database, 'query');
service.getUser(1);
expect(spy).toHaveBeenCalled();

6. Test Edge Cases

describe('divide', () => {
  it('should divide positive numbers', () => {
    expect(divide(10, 2)).toBe(5);
  });

  it('should divide negative numbers', () => {
    expect(divide(-10, 2)).toBe(-5);
  });

  it('should throw error on division by zero', () => {
    expect(() => divide(10, 0)).toThrow('Division by zero');
  });

  it('should handle floating point division', () => {
    expect(divide(1, 3)).toBeCloseTo(0.333, 2);
  });

  it('should handle very large numbers', () => {
    expect(divide(Number.MAX_SAFE_INTEGER, 2)).toBeGreaterThan(0);
  });
});

7. Keep Tests Fast

// Bad - slow tests
test('process large dataset', async () => {
  const data = Array.from({ length: 1000000 }, (_, i) => i);
  await processData(data); // Takes 10 seconds
});

// Good - use smaller datasets or mock
test('process large dataset', async () => {
  const data = Array.from({ length: 100 }, (_, i) => i);
  await processData(data); // Takes 10ms
});

// Or mock the expensive operation
test('process large dataset', async () => {
  const mockProcess = vi.fn().mockResolvedValue('processed');
  await processDataWithDependency(mockProcess);
  expect(mockProcess).toHaveBeenCalled();
});

Common Patterns

Test Fixtures

// Shared test data
const testUsers = {
  alice: { id: 1, name: 'Alice', email: 'alice@example.com' },
  bob: { id: 2, name: 'Bob', email: 'bob@example.com' },
};

// Factory functions
function createTestUser(overrides = {}) {
  return {
    id: 1,
    name: 'Test User',
    email: 'test@example.com',
    createdAt: new Date(),
    ...overrides,
  };
}

Setup and Teardown

describe('Database tests', () => {
  beforeAll(async () => {
    // Runs once before all tests
    await database.connect();
  });

  afterAll(async () => {
    // Runs once after all tests
    await database.disconnect();
  });

  beforeEach(async () => {
    // Runs before each test
    await database.clear();
  });

  afterEach(() => {
    // Runs after each test
    vi.clearAllMocks();
  });
});

Approach

When writing tests:

  1. Write Tests First (TDD) or with code
  2. Test Behavior, Not Implementation: Focus on what, not how
  3. Keep Tests Simple: Tests should be easier to understand than code
  4. Use Descriptive Names: Test name = documentation
  5. Test Edge Cases: Nulls, empty arrays, boundary values
  6. Mock External Dependencies: Databases, APIs, file system
  7. Maintain Tests: Refactor tests with production code
  8. Aim for Coverage: 80%+ but don't chase 100%

Always write tests that are fast, reliable, isolated, and maintainable. Good tests are the best documentation for your code.

平台分布

Claude Code

26.17%
按下载量换算213

OpenCode

22.22%
按下载量换算181

Codex

18.89%
按下载量换算154

Antigravity

11.24%
按下载量换算92

Gemini CLI

8.43%
按下载量换算69

windsurf

3.74%
按下载量换算30

安全审计

Gen Agent Trust Hub

通过

Socket

通过

Snyk

通过

权限和风险

需要联网

该 Skill 可能需要联网访问来源站点、仓库或外部 API;具体网络访问范围需要结合源码和 README 复核。

安装前确认

本站仅展示第三方公开信息,不托管安装包,不提供自动安装或运行环境。安装前应自行审查源码、依赖和命令行为。

继续浏览同类 Skills