如何检测Ruby是否正在运行黄瓜测试

时间:2021-07-08 11:48:29

Very similar to this question: Easy way to detect whether rspec or cucumber is running?

非常类似于这个问题:检测rspec或黄瓜是否正在运行的简便方法?

How can I detect in Ruby that I am running under cucumber?

如何在Ruby中检测到我在黄瓜下运行?

I have some path variables in a common code project that will need to be changed if the code being used is a cucumber project or a standard script. I'm trying to cover a relative pathing issue. require_relative '../../{filename}' will fail when the file structure is different when under the cucumber structure. It would need to traverse back two more levels like: '../../../../{filename}'.

我在公共代码项目中有一些路径变量,如果使用的代码是黄瓜项目或标准脚本,则需要更改。我试图涵盖相对路径问题。当黄瓜结构下的文件结构不同时,require_relative'../../ {filename}'将失败。它需要遍历两个更多级别,如:'../../../../ {filename}'。

Common Project: C:\RubyProjects\common-project\common

Common Project:C:\ RubyProjects \ common-project \ common

  • build_properties.rb

    def build_properties_from_yaml('', '')
      params = YAML.load_file(File.absolute_path << '/utils/parameters.yml')
    end
    
  • build_properties.rb def build_properties_from_yaml('','')   params = YAML.load_file(File.absolute_path <<'/ utils / parameters.yml') 结束

Cucumber Project: C:\RubyProjects\programs\2017\features\step_definitions

黄瓜项目:C:\ RubyProjects \ programs \ 2017 \ features \ step_definitions

  • test_rest.rb

    require_relative './../../../RubyProjects/common-project/common'
    
    class Verify
    
      include Common
    
      build_properties_from_yaml('', '')
    end
    
  • test_rest.rb require_relative'./../../../RubyProjects/common-project/common' 类验证   包括Common   build_properties_from_yaml('','') 结束

Some Other Project: C:\RubyProjects\programs\2017\

其他一些项目:C:\ RubyProjects \ programs \ 2017 \

  • File.rb

    require_relative './../../RubyProjects/common-project/common'
    
    class RunCode
    
      include Common
    
      build_properties_from_yaml('', '')
    end
    
  • File.rb require_relative'./../../RubyProjects/common-project/common' 类RunCode   包括Common   build_properties_from_yaml('','') 结束

With the case of the "utils" folder, it sits under the features folder in cucumber but there is no such folder in other projects without cucumber. That is what throws off the code and i'd like to check for cucumber in the yaml load process.

对于“utils”文件夹的情况,它位于黄瓜的features文件夹下,但在没有黄瓜的其他项目中没有这样的文件夹。这就是抛出代码的原因,我想在yaml加载过程中检查黄瓜。

1 个解决方案

#1


0  

If just if Rails.env.test? does not work for you, you may add to the features/support/env.rb:

如果只是Rails.env.test?不适合你,你可以添加到功能/支持/ env.rb:

ENV['CUCUMBER'] = "true"

Then examine it where you want:

然后检查它你想要的地方:

if ENV['CUCUMBER']
  ...
end

You can wrap this in a method somewhere and use it.

您可以将其包装在某个方法中并使用它。

EDIT: @spikermann's comments are absolutely right. For sure, you're doing something wrong if your code depends from an env. But in some cases (one-time code or kind of) it could be easier to make a hack.

编辑:@spikermann的评论是绝对正确的。当然,如果您的代码来自环境,那么您做错了。但在某些情况下(一次性代码或某种类型),制作黑客攻击可能更容易。

#1


0  

If just if Rails.env.test? does not work for you, you may add to the features/support/env.rb:

如果只是Rails.env.test?不适合你,你可以添加到功能/支持/ env.rb:

ENV['CUCUMBER'] = "true"

Then examine it where you want:

然后检查它你想要的地方:

if ENV['CUCUMBER']
  ...
end

You can wrap this in a method somewhere and use it.

您可以将其包装在某个方法中并使用它。

EDIT: @spikermann's comments are absolutely right. For sure, you're doing something wrong if your code depends from an env. But in some cases (one-time code or kind of) it could be easier to make a hack.

编辑:@spikermann的评论是绝对正确的。当然,如果您的代码来自环境,那么您做错了。但在某些情况下(一次性代码或某种类型),制作黑客攻击可能更容易。