如何比较黄瓜/阿鲁巴的日期?

时间:2016-11-05 09:36:52

标签: shell cucumber aruba

我想在cucumber / aruba的帮助下测试我的可执行shell脚本。 为此,我创建了一个shell脚本并将其放在usr / local / bin /中,以便可以从任何地方访问它。

shell脚本:

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

现在我想在cucumber / aruba中测试这个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脚本代码是返回日期。现在在这个功能文件中,我想通过简单的检查来检查日期是否正确。

示例: 日期如下:

  

2016年11月5日星期六15:00:13 IST 2016

所以我只想检查周六是对还是错。为此目的,使用一个标签[周一,周二,周三,周四,周五,周六,周日]。

如果Sat在上面的标签中可用,那么将此测试用例作为通过。

注意 - 我说这个标签的东西是为了简单起见。如果检查日的任何其他选项在一周的七天内是正确的,那么应该感激。

感谢。

1 个答案:

答案 0 :(得分: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

此功能文件既是您的程序的文档和规范。它意味着由不一定是开发人员的人编写。只要扩展名是" .feature"并且结构在这里(使用功能,场景和步骤),您可以在内部编写任何描述性内容。有关更多信息cuc 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脚本)作为String,并使用它编写测试。 例如,拆分此字符串并将每个部分分配给新变量。一旦你将星期几作为字符串(例如"星期六"),你就可以用expect keyword进行测试。

测试按强度顺序编写。如果你不幸,第二次测试可能不会在午夜过去。如果你想编写自己的测试,我将其他变量(日,月,hms,区,年)定义为String。