2016-11-05 58 views
1

我想在黃瓜/阿魯巴的幫助下測試我的可執行shell腳本。 爲此,我創建了一個shell腳本並將其放置在usr/local/bin /中,以便從任何地方訪問它。如何比較黃瓜/阿魯巴島的日期?

shell腳本:

arg=$1 
if [ [ $arg = 1 ] ] 
then 
    echo $(date) 
fi 

現在我想測試黃瓜/阿魯巴這個shell腳本。 爲此,我創建了一個項目結構。

aruba -

├──功能

│├──支持

││└──env.rb

│└──use_aruba_cucumber.feature

├──的Gemfile

Gemfile -

source 'https://rubygems.org' 
gem 'aruba', '~> 0.14.2' 

env.rb -

require 'aruba/cucumber' 

use_aruba_cucumber.feature -

Feature: Cucumber 
Scenario: First Run 
    When I run `bash abc_qa.sh` 
    Then the output should contain exactly $(date) 

shell腳本代碼返回日期。現在在這個功能文件中,我想通過簡單的檢查來檢查日期是否正確。

例如: 日期返回這樣的:

週六11月5日15時00分十三秒IST 2016

,所以我只是想檢查星期六是對還是錯。爲此,使用一個標籤[星期一,星期二,星期三,星期四,星期五,星期六,星期日]。

如果週六在上面的標籤中可用然後讓這個測試案例作爲通過。

注 - 我是說這個標籤的東西簡單sakel。如果任何其他選項查看一天是正確的一週七天,那麼這應該被讚賞。

謝謝。

回答

1

這是我會做:

features/use_my_date_script_with_parameter.feature

Feature: MyDateScript abc_qa 
Scenario: Run with one parameter 
    When I run `bash abc_qa.sh 1` 
    Then the output first word should be an abbreviated day of the week 
    And the output first word should be the current day of the week 
    And the output should be the current time 

此功能的文件既是文檔和程序的規範。它的意圖是由不一定是開發人員的人編寫的。只要延長是「。功能「和結構是在這裏(有特點,方案和步驟),你可以寫幾乎任何描述裏。關於黃瓜here更多信息。

你可以添加一個新行(如」和輸出應該看起來像A而不B「),並啓動黃瓜它不會失敗,它只會告訴你,你應該在步驟文件中定義什麼

features/step_definitions/time_steps.rb:。

require 'time' 

Then(/^the output should be the current time$/) do 
    time_from_script = Time.parse(last_command_started.output) 
    expect(time_from_script).to be_within(5).of(Time.now) 
end 

Then(/^the output first word should be an abbreviated day of the week$/) do 
    #NOTE: It assumes that date is launched with LC_ALL=en_US.UTF-8 as locale 
    day_of_week, day, month, hms, zone, year = last_command_started.output.split 
    days_of_week = %w(Mon Tue Wed Thu Fri Sat Sun) 
    expect(days_of_week).to include(day_of_week) 
end 

Then(/^the output first word should be the current day of the week$/) do 
    day_of_week, day, month, hms, zone, year = last_command_started.output.split 
    expect(day_of_week).to eq(Time.now.strftime('%a')) 
end 

這是的定義功能文件中的句子尚不爲Cucumber所知,它是一個Ruby文件,因此您可以在其中編寫任何Ruby代碼在doend之間的區塊中。 在那裏你可以訪問最後一個命令的輸出(在這種情況下是你的bash腳本)作爲一個字符串,然後用它寫測試。例如,分割此字符串並將每個零件分配給一個新變量。一旦將星期幾作爲字符串(例如「星期六」),您可以使用expect keyword進行測試。

測試是按強度順序編寫的。如果你運氣不好,第二次測試可能不會在午夜左右過去。如果您想編寫自己的測試,我將其他變量(日,月,hms,區域,年份)定義爲字符串。

+0

@ EricDuminil-看起來不錯。你能解釋一下use_aruba_with_cucumber.feature和time_steps.rb中的每一行含義嗎? – kit

+0

我盡力了。您可以嘗試修改腳本並查看會發生什麼。您可以在Google上找到許多關於cucumber/rspec/ruby​​的教程。 –

+0

@ EricDuminil-很好的工作。感謝你的努力。謝謝 – kit