programing tip

Rspec에서 특정 테스트 만 실행하려면 어떻게합니까?

itbloger 2020. 6. 9. 08:17
반응형

Rspec에서 특정 테스트 만 실행하려면 어떻게합니까?


주어진 레이블로 테스트 만 실행할 수있는 방법이 있다고 생각합니다. 아는 사람 있나요?


문서를 찾기는 쉽지 않지만 해시로 예제에 태그를 지정할 수 있습니다. 예 :

# spec/my_spec.rb
describe SomeContext do
  it "won't run this" do
    raise "never reached"
  end

  it "will run this", :focus => true do
    1.should == 1
  end
end

$ rspec --tag focus spec/my_spec.rb

GitHub에 대한 추가 정보 . (더 나은 링크를 가진 사람은 조언하십시오)

(최신 정보)

RSpec은 이제 여기에 훌륭하게 문서화되어 있습니다 . 자세한 내용은 --tag 옵션 섹션을 참조하십시오.

v2.6부터 이러한 종류의 태그는 구성 옵션을 포함하여 훨씬 간단하게 표현할 수 있습니다 treat_symbols_as_metadata_keys_with_true_values.

describe "Awesome feature", :awesome do

어디 :awesome그것 인 것처럼 처리됩니다 :awesome => true.

또한 '초점'테스트를 자동으로 실행하도록 RSpec을 구성하는 방법에 대해서는 이 답변참조하십시오 . 이것은 Guard 와 특히 잘 작동합니다 .


--example (또는 -e) 옵션을 사용하여 특정 문자열이 포함 된 모든 테스트를 실행할 수 있습니다 .

rspec spec/models/user_spec.rb -e "User is admin"

나는 그것을 가장 많이 사용합니다.


당신의 spec_helper.rb:

RSpec.configure do |config|
    config.filter_run focus: true
    config.run_all_when_everything_filtered = true
end

그런 다음 사양에 :

it 'can do so and so', focus: true do
    # This is the only test that will run
end

다음과 같이 'fit'으로 테스트에 집중하거나 'xit'으로 제외 할 수 있습니다.

fit 'can do so and so' do
    # This is the only test that will run
end

또는 줄 번호를 전달할 rspec spec/my_spec.rb:75수 있습니다.-줄 번호는 단일 사양 또는 컨텍스트 / 설명 블록을 가리킬 수 있습니다 (해당 블록의 모든 사양을 실행)


콜론과 함께 여러 줄 번호를 묶을 수도 있습니다.

$ rspec ./spec/models/company_spec.rb:81:82:83:103

산출:

Run options: include {:locations=>{"./spec/models/company_spec.rb"=>[81, 82, 83, 103]}}

RSpec에 2.4으로 (내 생각) 당신은 앞에 추가 할 수 있습니다 f또는 xit, specify, describecontext:

fit 'run only this example' do ... end
xit 'do not run this example' do ... end

http://rdoc.info/github/rspec/rspec-core/RSpec/Core/ExampleGroup#fit-class_method http://rdoc.info/github/rspec/rspec-core/RSpec/Core/ExampleGroup#xit-class_method

config.filter_run focus: trueconfig.run_all_when_everything_filtered = true안에 있어야 합니다 spec_helper.rb.


최신 RSpec 버전에서는 지원을 구성하기가 훨씬 쉽습니다 fit.

# spec_helper.rb

# PREFERRED
RSpec.configure do |c|
  c.filter_run_when_matching :focus
end

# DEPRECATED
RSpec.configure do |c|
  c.filter_run focus: true
  c.run_all_when_everything_filtered = true
end

보다:

https://relishapp.com/rspec/rspec-core/docs/filtering/filter-run-when-matching

https://relishapp.com/rspec/rspec-core/v/3-7/docs/configuration/run-all-when-everything-filtered


또한 focus: true기본적으로 사양을 실행할 수 있습니다

spec / spec_helper.rb

RSpec.configure do |c|
  c.filter_run focus: true
  c.run_all_when_everything_filtered = true
end

그런 다음 간단히 실행

$ rspec

집중된 테스트 만 실행됩니다

그런 다음 focus: true모든 테스트 를 제거하면 다시 실행하십시오.

자세한 정보 : https://www.relishapp.com/rspec/rspec-core/v/2-6/docs/filtering/inclusion-filters


로 실행할 수 있습니다 rspec spec/models/user_spec.rb -e "SomeContext won't run this".

참고 URL : https://stackoverflow.com/questions/5069677/how-do-i-run-only-specific-tests-in-rspec

반응형