【问题标题】:How to compare date in cucumber/aruba?如何比较黄瓜/阿鲁巴的日期?
【发布时间】:2016-11-06 00:42:21
【问题描述】:

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

shell 脚本:

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

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

阿鲁巴 -

.

├── 特点

│   ├── 支持

│   │   └── 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

所以我只想检查 Sat 是对还是错。为此,请像这样使用一个标签 [Mon,Tue,Wed,Thu,Fri,Sat,Sun]。

如果 Sat 在上述标签中可用,则将此测试用例作为通过。

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

谢谢。

【问题讨论】:

    标签: shell cucumber aruba


    【解决方案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”并且结构在这里(带有功能、场景和步骤),你就可以在里面写几乎任何描述性的东西。更多关于黄瓜的信息here.

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

    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 脚本)的输出,并用它编写测试。 例如,拆分此字符串并将每个部分分配给一个新变量。将星期几作为字符串(例如“Sat”)后,您可以使用 expect keyword 对其进行测试。

    测试是按强度顺序编写的。如果你不走运,第二个测试可能不会在午夜左右通过。如果您想编写自己的测试,我将其他变量(日、月、hms、区域、年)定义为字符串。

    【讨论】:

    • @EricDuminil- 看起来不错。你能解释一下 use_aruba_with_cucumber.feature 和 time_steps.rb 中每一行的含义吗?
    • 我尽力了。您可以尝试修改脚本,看看会发生什么。您会在 Google 上找到大量关于 cucumber/rspec/ruby 的教程。
    • @EricDuminil-干得好。感谢您的努力。谢谢
    猜你喜欢
    • 1970-01-01
    • 2017-03-03
    • 2023-03-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多