如何比较黄瓜/阿鲁巴的日期? [英] How to compare date in cucumber/aruba?

查看:325
本文介绍了如何比较黄瓜/阿鲁巴的日期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



shell脚本:

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



现在我想测试这个shell脚本在黄瓜/ aruba。
为此目的,我创建了一个项目结构。



aruba -



├──功能



│├──支援



││└──env.rb



│└──use_aruba_cucumber.feature



├──Gemfile



Gemfile -

  ://rubygems.org'
gem'aruba','〜> 0.14.2'

env.rb -

  require'aruba / cucumber'

use_aruba_cucumber.feature -

 功能:Cucumber 
场景: b $ b当我运行`bash abc_qa.sh'
时输出应该包含正确的$(date)

Shell脚本代码正在返回日期。现在在这个功能文件中,我想检查的是日期是正确的或不是通过使用简单的检查。



示例:
日期如下所示:


11月5日15:00:13 IST 2016


所以我只想检查星期六是对还是错。为此,使用一个标签[星期一,星期二,星期三,星期四,星期五,星期六,太阳]像这样。



如果星期六在上面标签,



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



感谢。

解决方案

/ p>

features / use_my_date_script_with_parameter.feature

 特性:MyDateScript abc_qa 
场景:使用一个参数运行
当我运行`bash abc_qa.sh 1`
然后输出的第一个单词应该是缩写日周
和输出第一个单词应该是当前的星期几
和输出应该是当前时间

此功能文件是您的程序的文档和规格。它的意思是由不一定是开发者的人写的。只要扩展名是.feature并且结构在这里(With Feature,Scenario和Steps),你可以写几乎任何描述性的内容。有关黄瓜的更多信息此处



您可以添加一个新行(例如输出应该看起来像A而不是B),并启动黄瓜。它不会失败,它只会告诉你你应该在步骤文件中定义什么。



features / step_definitions / time_steps.rb

  require'time'

然后当前时间$ /)do
time_from_script = Time.parse(last_command_started.output)
expect(time_from_script).to be_within(5).of(Time.now)
end

然后(/ ^输出的第一个单词应该是一个缩写的星期几$ /)do
#注意:它假定日期以LC_ALL = en_US.UTF-8作为区域设置启动
day_of_week,day,month,hms,zone,year = last_command_started.output.split
days_of_week =%w(星期一星期三星期三星期五星期六星期日)
预期(days_of_week)
end

然后(/ ^输出的第一个单词应该是当前星期几$ /)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代码,主要是在 do end 之间的块。
在那里你可以访问最后一个命令(在这种情况下你的bash脚本)的输出作为一个字符串,并用它写测试。
例如,拆分此字符串并将每个零件分配给一个新变量。一旦你有一个星期的一个字符串(例如星期六),你可以测试它与 expect关键字



测试是按照强度顺序写的。第二个测试可能不会传递到午夜,如果你不幸运。我定义了其他变量(日,月,hms,区,年)作为String如果你想写自己的测试。


I want to test my executable shell script with the help of cucumber/aruba. For that purpose I created one shell script and place it in usr/local/bin/ so it can accessible from anywhere.

shell script :

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

Now I want to test this shell script in cucumber/aruba. For that purpose I created one project structure.

aruba -

.

├── features

│   ├── support

│   │   └── 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 script code is returning date. Now In this feature file I want to check is that date is correct or not by using simple checking.

example : date is returning like this :

Sat Nov 5 15:00:13 IST 2016

so I just want to check Sat is right or wrong. For that purpose use one label [Mon,Tue,Wed,Thu,Fri,Sat,Sun] like this.

If Sat is available in above label Then make this test case as a pass.

note - I'm saying this label thing for simplicity sakel. If any other option for checking day is correct in seven days of week then this should be appreciated.

Thanks.

解决方案

This is what I would do :

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

This feature file is both a documentation and specs of your program. It is meant to be written by people who are not necessarily developers. As long as the extension is ".feature" and the structure is here (With Feature, Scenario and Steps), you can write pretty much anything descriptive inside. More info about cucumber here.

You could add a new line (e.g. "And the output should look like A and not B"), and launch cucumber. It won't fail, it will just tell you what you should define in steps file.

and 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

This is the definition of the sentences in feature file that aren't yet known to Cucumber. It is a Ruby file, so you can write any Ruby code inside, mostly in the blocks between do and end. There you can have access to the output of the last command (your bash script in this case) as a String, and write tests with it. For example, split this string and assign every part to a new variable. Once you have the day of the week as a String (e.g. "Sat"), you can test it with expect keyword.

The tests are written in order of strength. The second test might not pass around midnight if you're unlucky. I defined other variables (day, month, hms, zone, year) as String if you want to write your own test.

这篇关于如何比较黄瓜/阿鲁巴的日期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆