kit kit - 1 month ago 6
Bash Question

How to compare date in cucumber/aruba?

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.

Answer

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.