办公效率

软件自动化测试判定规则

提供待测函数及预期行为,生成断言、性质检查、差分对比或蜕变关系,用于自动判断结果是否正确。

# 测试预言机生成器

生成多种类型的自动化测试预言机,以验证软件行为是否正确。

## 预言机类型

1. **基于断言**:比较实际输出与预期输出
2. **基于属性**:验证应始终成立的不变量
3. **差分**:将新实现与参考实现进行比较
4. **蜕变**:测试输入与输出的变换关系

## 工作流程

### 第 1 步:分析被测函数

了解函数的作用及其预期行为。

**检查清单:**
- [ ] 阅读函数签名和文档字符串
- [ ] 确定输入参数及其类型
- [ ] 确定返回类型和可能的值
- [ ] 记录前置条件和后置条件
- [ ] 检查边界情况(空输入、null、边界值)

**分析示例:**

```python
# Python
def calculate_discount(price: float, discount_percent: int) -> float:
    """Calculate discounted price.

    Args:
        price: Original price (must be positive)
        discount_percent: Discount percentage (0-100)

    Returns:
        Discounted price
    """
    return price * (1 - discount_percent / 100)
```

**分析:**
- 输入:`price`(float,必须 > 0),`discount_percent`(int,0-100)
- 输出:float(折后价格)
- 不变量:结果应 ≤ 原价,结果应 ≥ 0
- 边界情况:0% 折扣、100% 折扣、边界值

### 第 2 步:生成基于断言的预言机

为常见情况创建明确的预期值断言。

**模板:**

```python
# Python (pytest)
def test_<function>_<scenario>():
    # Arrange
    input1 = <value>
    input2 = <value>
    expected = <calculated_expected_value>

    # Act
    actual = function_under_test(input1, input2)

    # Assert
    assert actual == expected, f"Expected {expected}, got {actual}"
```

```java
// Java (JUnit)
@Test
public void test<Function><Scenario>() {
    // Arrange
    Type input1 = <value>;
    Type input2 = <value>;
    Type expected = <calculated_expected_value>;

    // Act
    Type actual = functionUnderTest(input1, input2);

    // Assert
    assertEquals(expected, actual, "Expected and actual should match");
}
```

**示例:**

```python
def test_calculate_discount_50_percent():
    # Arrange
    price = 100.0
    discount = 50
    expected = 50.0

    # Act
    actual = calculate_discount(price, discount)

    # Assert
    assert actual == expected
    assert abs(actual - expected) < 0.01  # For floating point
```

**为以下情况生成预言机:**
- ✓ 典型情况(有效范围的中间值)
- ✓ 边界值(最小值/最大值)
- ✓ 边界情况(空值、零、null)
- ✓ 特殊值(数值类型的负数、无穷大)

### 第 3 步:生成基于属性的预言机

确定应始终成立的不变量和属性。

**常见属性:**

1. **范围属性**:输出处于预期范围内
2. **关系属性**:输出与输入之间存在特定关系
3. **守恒属性**:某些内容保持不变(例如列表长度)
4. **幂等性**:应用函数两次与应用一次的结果相同
5. **交换律**:输入顺序不影响结果
6. **结合律**:分组方式不影响结果

**模板(使用 hypothesis 的 Python):**

```python
from hypothesis import given, strategies as st

@given(st.floats(min_value=0.01, max_value=10000),
       st.integers(min_value=0, max_value=100))
def test_discount_properties(price, discount_percent):
    result = calculate_discount(price, discount_percent)

    # Property: Result should never exceed original price
    assert result <= price, "Discount should not increase price"

    # Property: Result should be non-negative
    assert result >= 0, "Price cannot be negative"

    # Property: 0% discount returns original price
    if discount_percent == 0:
        assert abs(result - price) < 0.01

    # Property: 100% discount returns 0
    if discount_percent == 100:
        assert abs(result) < 0.01
```

**模板(使用 JUnit Theories 的 Java):**

```java
@Theory
public void discountProperties(
    @ForAll @InRange(min = "0.01", max = "10000") double price,
    @ForAll @InRange(min = "0", max = "100") int discountPercent) {

    double result = calculateDiscount(price, discountPercent);

    // Property: Result should never exceed original price
    assertTrue(result <= price, "Discount should not increase price");

    // Property: Result should be non-negative
    assertTrue(result >= 0, "Price cannot be negative");
}
```

**通过以下问题确定属性:**
- 不精确计算输出,我能对它作出什么判断?
- 输入与输出之间必须满足哪些关系?
- 输出必须满足哪些约束?
- 哪些情况绝不应该发生?

有关详细的属性模式,请参阅 [references/property_patterns.md](references/property_patterns.md)。

### 第 4 步:生成差分预言机

将新实现与参考实现进行比较。

**适用场景:**
- 重构:新的优化版本与旧版本对比
- 迁移:新库或新语言与旧系统对比
- 修复缺陷:修补后的版本与未修补的版本对比

**模板:**

```python
# Python
def test_new_vs_legacy_implementation():
    # Test data
    test_cases = [
        (100.0, 10),
        (50.0, 25),
        (200.0, 0),
        (75.0, 100),
    ]

    for price, discount in test_cases:
        # Compare outputs
        legacy_result = legacy_calculate_discount(price, discount)
        new_result = calculate_discount(price, discount)

        assert abs(legacy_result - new_result) < 0.01, \
            f"Mismatch for ({price}, {discount}): " \
            f"legacy={legacy_result}, new={new_result}"
```

```java
// Java
@Test
public void testNewVsLegacyImplementation() {
    Object[][] testCases = {
        {100.0, 10},
        {50.0, 25},
        {200.0, 0},
        {75.0, 100}
    };

    for (Object[] testCase : testCases) {
        double price = (double) testCase[0];
        int discount = (int) testCase[1];

        double legacyResult = LegacyClass.calculateDiscount(price, discount);
        double newResult = calculateDiscount(price, discount);

        assertEquals(legacyResult, newResult, 0.01,
            String.format("Mismatch for (%f, %d)", price, discount));
    }
}
```

**最佳实践:**
- 生成多样化的测试数据(随机值、边界值、边界情况)
- 同时包含典型输入和不常见输入
- 记录差异以便调试
- 将性能差异视为可接受

### 第 5 步:生成蜕变预言机

创建测试对,使输入变换产生可预测的输出变换。

**蜕变关系:**

1. **加法关系**:f(x) + f(y) = f(x + y)
2. **乘法关系**:f(k × x) = k × f(x)
3. **排列关系**:f(permute(x)) = permute(f(x))
4. **子集关系**:f(subset(x)) ⊆ f(x)
5. **逆关系**:f(f⁻¹(x)) = x

**折扣函数示例:**

```python
# Python
def test_discount_metamorphic_double_price():
    """If price doubles, discount amount doubles."""
    price = 100.0
    discount_percent = 20

    result1 = calculate_discount(price, discount_percent)
    result2 = calculate_discount(price * 2, discount_percent)

    discount_amount1 = price - result1
    discount_amount2 = (price * 2) - result2

    # Metamorphic relation: doubling price doubles discount amount
    assert abs(discount_amount2 - 2 * discount_amount1) < 0.01

def test_discount_metamorphic_additive():
    """Applying discount to sum equals sum of individual discounts."""
    price1 = 50.0
    price2 = 30.0
    discount_percent = 15

    # Method 1: Discount on combined price
    combined_result = calculate_discount(price1 + price2, discount_percent)

    # Method 2: Sum of individual discounts
    individual_sum = (calculate_discount(price1, discount_percent) +
                      calculate_discount(price2, discount_percent))

    # Metamorphic relation: Should be equivalent
    assert abs(combined_result - individual_sum) < 0.01
```

**排序函数示例:**

```python
def test_sort_metamorphic_reverse():
    """Reversing then sorting gives same result as sorting."""
    input_list = [3, 1, 4, 1, 5, 9, 2, 6]

    result1 = sort(input_list)
    result2 = sort(list(reversed(input_list)))

    assert result1 == result2

def test_sort_metamorphic_duplicate():
    """Sorting list with duplicated elements maintains order."""
    input_list = [3, 1, 4]
    duplicated = input_list + input_list

    result = sort(duplicated)

    # Should be sorted version of original, doubled
    expected = sorted(input_list) + sorted(input_list)
    assert result == sorted(expected)
```

有关更多蜕变关系模式,请参阅 [references/metamorphic_patterns.md](references/metamorphic_patterns.md)。

### 第 6 步:组合预言机进行全面测试

结合使用多种类型的预言机,进行可靠验证。

**示例:calculate_discount 的完整测试套件:**

```python
import pytest
from hypothesis import given, strategies as st

# Assertion-based oracles
class TestDiscountAssertions:
    def test_50_percent_discount(self):
        assert calculate_discount(100.0, 50) == 50.0

    def test_no_discount(self):
        assert calculate_discount(100.0, 0) == 100.0

    def test_full_discount(self):
        assert calculate_discount(100.0, 100) == 0.0

# Property-based oracles
class TestDiscountProperties:
    @given(st.floats(min_value=0.01, max_value=10000),
           st.integers(min_value=0, max_value=100))
    def test_result_within_bounds(self, price, discount):
        result = calculate_discount(price, discount)
        assert 0 <= result <= price

    @given(st.floats(min_value=0.01, max_value=10000),
           st.integers(min_value=0, max_value=100))
    def test_monotonic_in_discount(self, price, discount):
        """Higher discount percentage means lower price."""
        if discount < 100:
            result1 = calculate_discount(price, discount)
            result2 = calculate_discount(price, discount + 1)
            assert result2 <= result1

# Differential oracles
class TestDiscountDifferential:
    @pytest.mark.parametrize("price,discount", [
        (100.0, 10), (50.0, 25), (200.0, 50)
    ])
    def test_vs_manual_calculation(self, price, discount):
        result = calculate_discount(price, discount)
        expected = price * (1 - discount / 100)
        assert abs(result - expected) < 0.01

# Metamorphic oracles
class TestDiscountMetamorphic:
    def test_double_price_doubles_discount_amount(self):
        price = 100.0
        discount = 20

        discount_amt1 = price - calculate_discount(price, discount)
        discount_amt2 = (price * 2) - calculate_discount(price * 2, discount)

        assert abs(discount_amt2 - 2 * discount_amt1) < 0.01
```

### 第 7 步:记录并验证预言机

确保预言机正确且文档完善。

**预言机文档模板:**

```python
def test_function_oracle_type():
    """Brief description of what this oracle verifies.

    Oracle Type: [Assertion-based|Property-based|Differential|Metamorphic]

    Rationale: Explain why this property/assertion should hold.

    Edge Cases Covered:
    - Case 1
    - Case 2
    """
    # Test implementation
    pass
```

**验证清单:**
- [ ] 预言机在正确实现上通过
- [ ] 预言机在故意破坏的实现上失败(变异测试)
- [ ] 预言机具有确定性(相同输入 → 相同结果)
- [ ] 预言机是独立的(不依赖其他测试的状态)
- [ ] 预言机提供清晰的失败消息
- [ ] 文档中记录了预言机的依据

**变异测试(验证预言机的有效性):**

```python
# Introduce deliberate bug to verify oracle catches it
def calculate_discount_buggy(price, discount_percent):
    # BUG: Wrong formula
    return price * discount_percent / 100  # Should be: price * (1 - discount_percent / 100)

# Oracle should fail on buggy version
def test_oracle_detects_bug():
    """Verify oracle catches the bug."""
    with pytest.raises(AssertionError):
        assert calculate_discount_buggy(100, 50) == 50.0  # This should fail
```

## 预言机选择指南

**在以下情况选择基于断言的预言机:**
- 预期输出易于计算
- 测试特定的已知场景
- 使用保存的示例进行回归测试

**在以下情况选择基于属性的预言机:**
- 预期输出难以计算,但属性明确
- 希望测试大量随机输入
- 测试应始终成立的不变量

**在以下情况选择差分预言机:**
- 重构或优化现有代码
- 迁移到新实现
- 存在参考实现

**在以下情况选择蜕变预言机:**
- 预期输出未知或难以计算
- 没有可用的参考实现
- 希望测试复杂的变换

## 提示

1. **从简单入手**:先使用基于断言的预言机,再按需添加其他类型
2. **组合预言机**:使用多种类型进行可靠验证
3. **测试测试本身**:使用变异测试验证预言机的有效性
4. **记录假设**:解释属性为什么应当成立
5. **处理浮点数**:比较浮点数时使用容差
6. **考虑性能**:基于属性的测试会运行多次迭代
7. **关注重要属性**:并非每个函数都需要所有类型的预言机

## 常见模式

有关按领域分类的详细预言机模式,请参阅:
- [references/property_patterns.md](references/property_patterns.md) - 常见的基于属性的预言机模式
- [references/metamorphic_patterns.md](references/metamorphic_patterns.md) - 蜕变关系目录

同类提示词

办公效率· n8n 工作流的对话模型

商品照片转棚拍广告生图指令

上传商品照片并提供说明文字,分析材质、配色和原图问题,写出保留商品外观的棚拍广告生图指令。

=你是一位专业产品摄影师和广告创意总监。分析上传的产品图片及其配文:{{ $('Telegram Trigger Received').item.json.message.caption }} 你的任务是创建一段详细的图像生成提示词,将这张产品照片转化为高端、商业级的广告视觉作品。 分析要求: 1. **产品识别**:识别产品类型、尺寸、类别、材质、配色和关键特征 2. **当前状态评估**:指出当前的光线、背景、构图和质量问题 3. **目标美学**:根据产品类别确定理想的广告风格(奢侈品、科技、食品、时尚等) 输出(严格使用 JSON 格式):
办公效率· n8n 工作流的对话模型

Veo 3营销视频分镜指令

填写视频需求、营销目标、图像描述和画幅,生成包含场景、运镜、动作、灯光与结尾的结构化制片指令。

=# Veo3 视频生成系统提示词 你是一名专业的视频提示词工程师,擅长为 Veo3 视频生成撰写细致、富有电影感的提示词。你的角色是分析用户输入,将其转化为全面的视频生成指令,以产出高质量、引人入胜的内容。 ## 输入分析 你将收到以下用户输入: - **AI Videos instructions**:视频生成的核心用户请求 - **Campaign objectives**:营销活动目标和说明 - **Images Description**:用户提供的视觉背景信息 - **Aspect Ratio**:要求的视频尺寸比例 ## 输出格式 你
可填 21 项
办公效率· n8n 工作流的对话模型

保留原品的社媒广告视觉方案

提供商品图和配文,生成保留原品外观的广告生图说明,广告文字仅从配文提取,缺项留空。

={ "system_prompt": "你是一位顶尖创意总监,专攻爆款产品广告,拥有15年以上奢侈品牌广告活动经验。将产品图片转化为商业级广告,让浏览者停下滑动并促进转化。", "instructions": { "core_rules": [ "保持原始产品100%不变——不得修改、替换或扭曲", "只提取和使用用户配文中提供的文字", "如果配文中没有相关文字,文字字段留空——绝不生成AI广告语", "专注于高端视觉强化和商业摄影技巧", "制作适合在Instagram展
办公效率· Claude Fable 5.2

用代码制作长纪录片流程

从题材调研、脚本和样帧审核开始,按章节生成三维分镜、旁白、环境声与可复现渲染。

<inputs> 向我询问: 1. 我的主题 2. 目标视频时长 3. 我是否已有脚本 如果我只给你一个主题,先研究该主题并撰写脚本。 开始制作任何内容之前,先向我展示: - 完整脚本 - 章节拆分 - 前 60 seconds 的分镜计划 - 6 张展示预期视觉方向的静帧 在我认可视觉效果之前,不要渲染完整视频。 </inputs> <story> 按照高品质 YouTube 纪录片的方式来写,不要写成 AI 科普解说。 故事必须始终向前推进。 每个部分都需要包含以下内容之一: - 一个新问题 - 一个揭示 - 一个矛盾 - 一个后果 - 一条证据
办公效率

同场景表情变化九宫格自拍

上传主角照片,可选搭档参考图或设定,并填写服装、背景或姿势偏好,生成同一布光和构图的九张变化。

# photo-booth ## 何时使用此 skill - 当用户希望根据参考图制作大头贴宫格、表情宫格、情侣自拍宫格、同步姿势组图或多种人像变体时使用。 - 当请求制作 3x3 宫格,且主体、房间、服装、构图和布光保持一致,仅表情或细微姿势发生变化时触发。 ## 必需输入 - 主要人物的主参考图。 - 可选的伴侣或角色参考图。 - 如果未提供伴侣参考图,需提供期望的伴侣设定。 - 用户指定的服装、背景、摄影时代、姿势清单或氛围。 如果缺少主参考图,请用户附上。如果伴侣设定或预期风格不明确,提出一个有针对性的问题。 ## 连续性规则 -
需参考图
办公效率· Claude Code / 动效编程

单镜头立体落地页飞越动画

提供落地页链接、品牌名、标志和两段收尾文案,可选配乐,制作卡片随镜头掠过而浮起的连续飞越短片。

<inputs> 向我索取:我的落地页链接、品牌名称和标志、由两部分组成的结尾文案(一句陈述,后接一小段斜体文字),以及一条 15 second 音轨(可选)。 </inputs> <direction> 以 60fps 对我的落地页进行 14.5 second 3D 飞越拍摄,1440x1152。页面平放在明亮的摄影棚地面上(中央为 #f9fbfb,角落为 #f2f3f6),周围有柔和宽阔的阴影,圆角为 24px。全程使用一个连续镜头,绝不切镜:镜头从首屏上方低处开始,以恒定速度沿页面向后滑行,同时朝页面上方看去;然后垂直升至正上方,将整个页面呈现为
✓