RSpec - 過濾



您可能希望在閱讀本節之前閱讀有關 RSpec 元資料的部分,因為事實證明,RSpec 過濾基於 RSpec 元資料。

假設您有一個規範檔案,其中包含兩種型別的測試(示例):正向功能測試和負向(錯誤)測試。讓我們這樣定義它們:

RSpec.describe "An Example Group with positive and negative Examples" do 
   context 'when testing Ruby\'s build-in math library' do
      
      it 'can do normal numeric operations' do 
         expect(1 + 1).to eq(2) 
      end 
      
      it 'generates an error when expected' do
         expect{1/0}.to raise_error(ZeroDivisionError) 
      end
      
   end 
end

現在,將以上文字儲存為名為“filter_spec.rb”的檔案,然後使用以下命令執行它:

rspec filter_spec.rb

您將看到如下所示的輸出:

.. 
Finished in 0.003 seconds (files took 0.11201 seconds to load) 
2 examples, 0 failures

現在,如果我們只想重新執行此檔案中的正向測試怎麼辦?或者只有負向測試?我們可以使用 RSpec 過濾器輕鬆做到這一點。將以上程式碼更改為:

RSpec.describe "An Example Group with positive and negative Examples" do 
   context 'when testing Ruby\'s build-in math library' do
      
      it 'can do normal numeric operations', positive: true do 
         expect(1 + 1).to eq(2) 
      end 
      
      it 'generates an error when expected', negative: true do 
         expect{1/0}.to raise_error(ZeroDivisionError) 
      end
      
   end 
end

儲存您對 filter_spec.rb 的更改並執行此略有不同的命令:

rspec --tag positive filter_spec.rb

現在,您將看到如下所示的輸出:

Run options: include {:positive=>true} 
. 
Finished in 0.001 seconds (files took 0.11401 seconds to load) 
1 example, 0 failures

透過指定 --tag positive,我們告訴 RSpec 只執行定義了 :positive 元資料變數的示例。我們可以透過執行以下命令對負向測試執行相同的操作:

rspec --tag negative filter_spec.rb

請記住,這些僅僅是示例,您可以使用任何您想要的名稱指定過濾器。

RSpec 格式化程式

格式化程式允許 RSpec 以不同的方式顯示測試的輸出。讓我們建立一個包含以下程式碼的新 RSpec 檔案:

RSpec.describe "A spec file to demonstrate how RSpec Formatters work" do 
   context 'when running some tests' do 
      
      it 'the test usually calls the expect() method at least once' do 
         expect(1 + 1).to eq(2) 
      end
      
   end 
end

現在,將其儲存到名為 formatter_spec.rb 的檔案中並執行此 RSpec 命令:

rspec formatter_spec.rb

您應該看到如下所示的輸出:

. 
Finished in 0.002 seconds (files took 0.11401 seconds to load) 
1 example, 0 failures

現在執行相同的命令,但這次指定一個格式化程式,如下所示:

rspec --format progress formatter_spec.rb

您應該這次看到相同的輸出:

. 
Finished in 0.002 seconds (files took 0.11401 seconds to load) 
1 example, 0 failures

原因是“progress”格式化程式是預設格式化程式。接下來讓我們嘗試一個不同的格式化程式,嘗試執行此命令:

rspec --format doc formatter_spec.rb

現在您應該看到此輸出:

A spec file to demonstrate how RSpec Formatters work 
   when running some tests 
      the test usually calls the expect() method at least once
Finished in 0.002 seconds (files took 0.11401 seconds to load) 
1 example, 0 failures

如您所見,使用“doc”格式化程式的輸出完全不同。此格式化程式以類似文件的樣式呈現輸出。您可能想知道當測試(示例)失敗時這些選項是什麼樣的。讓我們更改formatter_spec.rb中的程式碼使其如下所示:

RSpec.describe "A spec file to demonstrate how RSpec Formatters work" do 
   context 'when running some tests' do 
      
      it 'the test usually calls the expect() method at least once' do 
         expect(1 + 1).to eq(1) 
      end
      
   end 
end

期望expect(1 + 1).to eq(1)應該失敗。儲存您的更改並重新執行以上命令:

rspec --format progress formatter_spec.rb並記住,由於“progress”格式化程式是預設的,因此您可以只執行:rspec formatter_spec.rb。您應該看到此輸出:

F 
Failures:
1) A spec file to demonstrate how RSpec Formatters work when running some tests 
the test usually calls the expect() method at least once
   Failure/Error: expect(1 + 1).to eq(1)
	
      expected: 1
         got: 2
			  
      (compared using ==)			  
   # ./formatter_spec.rb:4:in `block (3 levels) in <top (required)>'

Finished in 0.016 seconds (files took 0.11201 seconds to load)
1 example, 1 failure
Failed examples:

rspec ./formatter_spec.rb:3 # A spec file to demonstrate how RSpec 
   Formatters work when running some tests the test usually calls 
   the expect() method at least once

現在,讓我們嘗試 doc 格式化程式,執行此命令:

rspec --format doc formatter_spec.rb

現在,在測試失敗的情況下,您應該看到此輸出:

A spec file to demonstrate how RSpec Formatters work
   when running some tests
      the test usually calls the expect() method at least once (FAILED - 1)
		
Failures:

1) A spec file to demonstrate how RSpec Formatters work when running some
   tests the test usually calls the expect() method at least once
   Failure/Error: expect(1 + 1).to eq(1)
	
   expected: 1
        got: 2
		  
   (compared using ==)
   # ./formatter_spec.rb:4:in `block (3 levels) in <top (required)>'
	
Finished in 0.015 seconds (files took 0.11401 seconds to load) 
1 example, 1 failure

失敗的示例

rspec ./formatter_spec.rb:3 # 一個規範檔案,用於演示 RSpec 格式化程式在執行一些測試時如何工作,測試通常至少呼叫一次 expect() 方法。

RSpec 格式化程式提供了更改測試結果顯示方式的功能,甚至可以建立自己的自定義格式化程式,但這是一個更高階的主題。

廣告

© . All rights reserved.