【问题标题】:How to capture shell script program output from cucumber/aruba?如何从 cucumber/aruba 捕获 shell 脚本程序输出?
【发布时间】:2016-11-05 22:28:03
【问题描述】:

我想从黄瓜功能文件中捕获我正在运行的输出。

我创建了一个 shell 脚本程序并将其放在 /usr/local/bin/ 中,以便可以从系统中的任何位置访问它。

abc_qa.sh -

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

黄瓜的项目结构-

阿鲁巴 -

.

├── 特点

│├──支持

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

我想在黄瓜本身中捕获这个abc_qa.sh程序输出,并通过使用任何一种简单的测试来比较这个日期是对还是错,并使这个测试通过。

【问题讨论】:

    标签: ruby bash shell cucumber aruba


    【解决方案1】:

    您可以使用%x(command) 获取命令的标准输出。

    然后您可以使用Time.parse"Sat Nov 5 12:04:18 CET 2016" 转换为2016-11-05 12:04:18 +0100 作为 Time 对象,并将其与 Time.now 进行比较:

    require 'time'
    time_from_abc_script = Time.parse(%x(bash abc_qa.sh 1))
    puts (Time.now-time_from_abc_script).abs < 5 # No more than 5s difference between 2 times
    

    你可以在任何你想要的测试文件中使用这个布尔值。

    例如:

    features/use_aruba_with_cucumber.feature

    Feature: Cucumber
     Scenario: First Run
      When I run `bash abc_qa.sh 1`
      Then the output should be the current time
    

    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
    

    【讨论】:

    • 我是黄瓜新手。你能告诉我我需要写什么以及以什么方式写吗?
    • 我将在我的机器上安装 aruba/cucumber 并编写功能和步骤定义。
    • @EricDuminil- 太棒了。还有一件事,shell 脚本的输出包含:Sat Nov 5 19:30:03 IST 2016。我想检查它的日期是在一周的 7 天,然后通过这个测试。我该怎么做?
    • 这可能是另一个问题。如果这个问题得到了回答,请考虑支持并接受我的回答。我不确定我明白你想要什么。您要检查“Sat”是一周中某天的缩写吗?
    • 请参阅 [stackoverflow.com/questions/40436724/… 问题,以便更好地理解并回答。
    猜你喜欢
    • 2023-04-07
    • 1970-01-01
    • 1970-01-01
    • 2023-03-11
    • 2018-06-09
    • 2022-11-16
    • 2010-11-03
    • 2014-05-11
    • 1970-01-01
    相关资源
    最近更新 更多