실행 맥락 선택

일부 테스트는 특정 환경이나 특정 파이프라인 또는 작업에 대해 실행되도록 설계되었습니다. 우리는 onlyexcept 메타데이터를 사용하여 테스트 실행 맥락을 지정할 수 있습니다.

사용 가능한 스위치

스위치 기능 유형
tld 최상위 도메인 일치기 설정 String
subdomain 서브도메인 일치기 설정 Array 또는 String
domain 도메인 일치기 설정 String
production 프로덕션 환경과 일치 Static
pipeline 파이프라인과 일치 Array 또는 Static
job 작업과 일치 Array 또는 Static

경고:
:production{ <switch>: 'value' }를 동시에 지정할 수 없습니다.
이 옵션들은 호환되지 않습니다. 프로덕션을 지정하려면 tlddomain을 독립적으로 제어할 수 있습니다.

예시

Only

지정된 맥락에서만 테스트를 실행합니다.

일치는 다음과 같습니다:

  • 환경에 대한 정규 표현식.
  • 파이프라인에 대한 문자열 일치.
  • 작업에 대한 정규 표현식 또는 문자열 일치.
  • 일반 조건에 대한 람다 또는 진리/거짓 값.
테스트 실행 맥락 일치
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
nightly 파이프라인 only: { pipeline: :nightly } “nightly scheduled pipeline”
nightlycanary 파이프라인 only: { pipeline: [:nightly, :canary] } “nightly scheduled pipeline”“canary”
ee:instance 작업 only: { job: 'ee:instance' } 모든 파이프라인의 ee:instance 작업
모든 quarantine 작업 only: { job: '.*quarantine' } 모든 파이프라인의 quarantine로 끝나는 작업
로컬 개발 환경 only: :local Runtime::Env.running_in_ci?가 false인 모든 환경
조건이 진리값으로 평가되는 모든 실행 only: { condition: -> { ENV['TEST_ENV'] == 'true' } } TEST_ENV가 true로 설정된 모든 실행
RSpec.describe 'Area' do
  it 'runs in any environment or pipeline' do; end
  it 'runs only in production environment', only: :production do; end

  it 'runs only in staging environment', only: { subdomain: :staging } do; end

  it 'runs in dev environment', only: { tld: '.org', domain: 'gitlab', subdomain: 'dev' } do; end

  it 'runs in prod and staging environments', only: { subdomain: /(staging.)?/, domain: 'gitlab' } {}

  it 'runs only in nightly pipeline', only: { pipeline: :nightly } do; end

  it 'runs in nightly and canary pipelines', only: { pipeline: [:nightly, :canary] } do; end

  it 'runs in specific environment matching condition', only: { condition: -> { ENV['TEST_ENV'] == 'true' } } do; end
end

Except

지정된 대로 제외하고 일반적인 컨텍스트에서 테스트를 실행합니다.

일치 항목 사용:

  • 환경에 대한 정규 표현식.
  • 파이프라인에 대한 문자열 일치.
  • 작업에 대한 정규 표현식 또는 문자열 일치.
  • 일반 조건에 대한 람다 또는 진리값/거짓값.
테스트 실행 컨텍스트 일치 항목
gitlab.com except: :production gitlab.com
staging.gitlab.com except: { subdomain: :staging } (staging).+.com
gitlab.com 및 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 및 domain.gitlab.com except: { subdomain: %i[staging domain] } (staging\|domain).+.com
nightly 파이프라인 only: { pipeline: :nightly } “nightly scheduled pipeline”
nightlycanary 파이프라인 only: { pipeline: [:nightly, :canary] } “nightly scheduled pipeline”“canary”
ee:instance 작업 except: { job: 'ee:instance' } 어떤 파이프라인에서든 ee:instance 작업
어떤 quarantine 작업 except: { job: '.*quarantine' } 어떤 파이프라인에서든 quarantine로 끝나는 작업
조건이 진리값으로 평가되지 않는 경우를 제외한 실행 except: { condition: -> { ENV['TEST_ENV'] == 'true' } } TEST_ENV가 true로 설정되지 않은 모든 실행
RSpec.describe 'Area' do

  it 'runs in any execution context except the production environment', except: :production do; end

  it 'runs in any execution context except the staging environment', except: { subdomain: :staging } do; end

  it 'runs in any execution context except the nightly pipeline', except: { pipeline: :nightly } do; end

  it 'runs in any execution context except the ee:instance job', except: { job: 'ee:instance' } do; end

  it 'runs in specific environment not matching condition', except: { condition: -> { ENV['TEST_ENV'] == 'true' } } do; end

end

사용 주의 사항

테스트에 before 또는 after 블록이 있는 경우, 외부 RSpec.describe 블록에 only 또는 except 메타데이터를 추가해야 합니다.

로컬 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 환경 변수를 사용하여 명시적으로 비활성화할 수 있습니다. 이는 로컬에서 테스트를 실행할 때 유용할 수 있습니다.