场景
scenarios
配置允许你将一组数据与一组应在该数据上运行的测试组合在一起。当你想用同一组测试来测试广泛的输入时,这非常有用。
示例
以语言翻译应用为例。我们希望测试系统是否能将三个短语('Hello world'、'Good morning' 和 'How are you?')从英语准确翻译成三种不同的语言(西班牙语、法语和德语)。
You're a translator. Translate this into {{language}}: {{input}}
---
Speak in {{language}}: {{input}}
与其为每种组合创建单独的 tests
,我们可以创建一个 scenarios
,将这些数据和测试/断言组合在一起:
scenarios:
- config:
- vars:
language: Spanish
expectedHelloWorld: 'Hola mundo'
expectedGoodMorning: 'Buenos días'
expectedHowAreYou: '¿Cómo estás?'
- vars:
language: French
expectedHelloWorld: 'Bonjour le monde'
expectedGoodMorning: 'Bonjour'
expectedHowAreYou: 'Comment ça va?'
- vars:
language: German
expectedHelloWorld: 'Hallo Welt'
expectedGoodMorning: 'Guten Morgen'
expectedHowAreYou: 'Wie geht es dir?'
tests:
- description: Translated Hello World
vars:
input: 'Hello world'
assert:
- type: similar
value: '{{expectedHelloWorld}}'
threshold: 0.90
- description: Translated Good Morning
vars:
input: 'Good morning'
assert:
- type: similar
value: '{{expectedGoodMorning}}'
threshold: 0.90
- description: Translated How are you?
vars:
input: 'How are you?'
assert:
- type: similar
value: '{{expectedHowAreYou}}'
threshold: 0.90
这将生成每种语言和输入短语组合的测试矩阵,并在每个组合上运行相同的断言集。
此示例的完整源代码在 examples/multiple-translations-scenarios
中。
配置
scenarios
配置是一个 Scenario
对象的数组。每个 Scenario
有两个主要部分:
config
:一个vars
对象的数组。每个vars
对象代表将传递给测试的一组变量。tests
:一个TestCase
对象的数组。这些是将为config
中的每组变量运行的测试。
以下是 Scenario
的结构:
属性 | 类型 | 必需 | 描述 |
---|---|---|---|
description | string | 否 | 可选的测试描述 |
config | Partial<TestCase>[] | 是 | 变量集的数组。每个集将通过测试运行。 |
tests | TestCase[] | 是 | 在每组变量上运行的测试。 |
场景也可以从外部文件加载。要引用外部文件,请使用 file://
前缀:
scenarios:
- file://path/to/your/scenario.yaml
外部文件应遵循与内联场景相同的结构。
此功能允许你轻松运行广泛的测试,而无需手动创建每个测试。它还使你的配置文件更简洁、更易于阅读。