2016-11-05 60 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