Help us learn about your current experience with the documentation. Take the survey.
执行上下文选择
某些测试被设计为在特定环境或特定的 pipelines 或作业中运行。我们可以使用 only 和 except 元数据来指定测试执行上下文。
可用的开关
| 开关 | 功能 | 类型 |
|---|---|---|
tld |
设置顶级域名匹配器 | String |
subdomain |
设置子域名匹配器 | Array 或 String |
domain |
设置域名匹配器 | String |
production |
匹配生产环境 | Static |
pipeline |
匹配 pipeline | Array 或 Static |
job |
匹配作业 | Array 或 Static |
你不能同时指定 :production 和 { <switch>: 'value' }。
这些选项是互斥的。如果你想指定生产环境,你可以独立控制 tld 和 domain。
示例
Only(仅)
仅在指定的上下文中运行测试。
匹配规则使用:
- 环境使用正则表达式匹配
- Pipeline 使用字符串匹配
- 作业使用正则表达式或字符串匹配
- 通用条件使用 Lambda 或真/假值
| 测试执行上下文 | 键 | 匹配 |
|---|---|---|
gitlab.com |
only: :production |
gitlab.com |
staging.gitlab.com |
only: { subdomain: :staging } |
(staging).+.com |
gitlab.com and staging.gitlab.com |
only: { subdomain: /(staging.)?/, domain: 'gitlab' } |
(staging.)?gitlab.com |
dev.gitlab.org |
only: { tld: '.org', domain: 'gitlab', subdomain: 'dev' } |
(dev).gitlab.org |
staging.gitlab.com and domain.gitlab.com |
only: { subdomain: %i[staging domain] } |
(staging|domain).+.com |
The nightly pipeline |
only: { pipeline: :nightly } |
“nightly scheduled pipeline” |
The nightly and canary pipelines |
only: { pipeline: [:nightly, :canary] } |
“nightly scheduled pipeline” and “canary” |
The ee:instance job |
only: { job: 'ee:instance' } |
任何 pipeline 中的 ee:instance 作业 |
Any quarantine job |
only: { job: '.*quarantine' } |
任何 pipeline 中以 quarantine 结尾的作业 |
| Local development environment | only: :local |
Runtime::Env.running_in_ci? 为 false 的任何环境 |
| Any run where condition evaluates to a truthy value | only: { condition: -> { ENV['TEST_ENV'] == 'true' } } |
TEST_ENV 设置为 true 的任何运行 |
RSpec.describe 'Area' do
it '在任意环境或 pipeline 中运行' do; end
it '仅在生产环境中运行', only: :production do; end
it '仅在暂存环境中运行', only: { subdomain: :staging } do; end
it '在开发环境中运行', only: { tld: '.org', domain: 'gitlab', subdomain: 'dev' } do; end
it '在生产环境和暂存环境中运行', only: { subdomain: /(staging.)?/, domain: 'gitlab' } {}
it '仅在 nightly pipeline 中运行', only: { pipeline: :nightly } do; end
it '在 nightly 和 canary pipelines 中运行', only: { pipeline: [:nightly, :canary] } do; end
it '在符合特定条件的环境中运行', only: { condition: -> { ENV['TEST_ENV'] == 'true' } } do; end
endExcept(除外)
在测试的典型上下文中运行,除非另有指定。
匹配规则使用:
- 环境使用正则表达式匹配
- Pipeline 使用字符串匹配
- 作业使用正则表达式或字符串匹配
- 通用条件使用 Lambda 或真/假值
| 测试执行上下文 | 键 | 匹配 |
|---|---|---|
gitlab.com |
except: :production |
gitlab.com |
staging.gitlab.com |
except: { subdomain: :staging } |
(staging).+.com |
gitlab.com and staging.gitlab.com |
except: { subdomain: /(staging.)?/, domain: 'gitlab' } |
(staging.)?gitlab.com |
dev.gitlab.org |
except: { tld: '.org', domain: 'gitlab', subdomain: 'dev' } |
(dev).gitlab.org |
staging.gitlab.com and domain.gitlab.com |
except: { subdomain: %i[staging domain] } |
(staging|domain).+.com |
The nightly pipeline |
only: { pipeline: :nightly } |
“nightly scheduled pipeline” |
The nightly and canary pipelines |
only: { pipeline: [:nightly, :canary] } |
“nightly scheduled pipeline” and “canary” |
The ee:instance job |
except: { job: 'ee:instance' } |
任何 pipeline 中的 ee:instance 作业 |
Any quarantine job |
except: { job: '.*quarantine' } |
任何 pipeline 中以 quarantine 结尾的作业 |
| Any run except where condition evaluates to a truthy value | except: { condition: -> { ENV['TEST_ENV'] == 'true' } } |
TEST_ENV 未设置为 true 的任何运行 |
RSpec.describe 'Area' do
it '在任意执行上下文中运行,除了生产环境', except: :production do; end
it '在任意执行上下文中运行,除了暂存环境', except: { subdomain: :staging } do; end
it '在任意执行上下文中运行,除了 nightly pipeline', except: { pipeline: :nightly } do; end
it '在任意执行上下文中运行,除了 ee:instance 作业', except: { job: 'ee:instance' } do; end
it '在不匹配特定条件的环境中运行', except: { condition: -> { ENV['TEST_ENV'] == 'true' } } do; end
end使用说明
如果测试有 before 或 after 块,你必须将 only 或 except 元数据添加到外层的 RSpec.describe 块中。
要在本地 GitLab 实例上运行标记为 only 的测试,你可以选择以下方法之一:
- 确保 不要 设置
CI_PROJECT_NAME或CI_JOB_NAME环境变量。 - 设置适当的变量以匹配元数据。例如,如果元数据是
only: { pipeline: :nightly },则设置CI_PROJECT_NAME=nightly。如果元数据是only: { job: 'ee:instance' },则设置CI_JOB_NAME=ee:instance。 - 临时移除元数据。
要在本地运行标记为 except 的测试,你可以选择:
- 确保 不要 设置
CI_PROJECT_NAME或CI_JOB_NAME环境变量。 - 临时移除元数据。
为特定环境隔离测试
与指定测试仅在特定环境中运行类似,也可以仅在测试针对特定环境运行时对其进行隔离。语法完全相同,只是 only: { ... } 哈希嵌套在 quarantine: { ... } 哈希中。例如,quarantine: { only: { subdomain: :staging } } 仅在针对 staging 运行时隔离测试。
可以使用 DISABLE_QUARANTINE 环境变量显式禁用隔离功能。这在本地运行测试时很有用。