如何在黄瓜测试期间 运行 后台作业?
How to run background jobs during cucumber tests?
使用 Cucumber 测试需要后台作业的最佳方法是什么?我需要 运行 DelayedJob 和 Sneakers 工作人员在后台进行测试 运行ning。
您可以运行后台的任何应用程序:
@pid = Process.spawn "C:/Apps/whatever.exe"
Process.detach(@pid)
甚至在测试完成后将其杀死:
Process.kill('KILL', @pid) unless @pid.nil?
如果有人有类似的问题,我最后写了这篇文章(感谢 Square 博客 post):
require "timeout"
class CucumberExternalWorker
attr_accessor :worker_pid, :start_command
def initialize(start_command)
raise ArgumentError, "start_command was expected" if start_command.nil?
self.start_command = start_command
end
def start
puts "Trying to start #{start_command}..."
self.worker_pid = fork do
start_child
end
at_exit do
stop_child
end
end
private
def start_child
exec({ "RAILS_ENV" => Rails.env }, start_command)
end
def stop_child
puts "Trying to stop #{start_command}, pid: #{worker_pid}"
# send TERM and wait for exit
Process.kill("TERM", worker_pid)
begin
Timeout.timeout(10) do
Process.waitpid(worker_pid)
puts "Process #{start_command} stopped successfully"
end
rescue Timeout::Error
# Kill process if could not exit in 10 seconds
puts "Sending KILL signal to #{start_command}, pid: #{worker_pid}"
Process.kill("KILL", worker_pid)
end
end
end
这可以调用如下(将其添加到黄瓜的 env.rb
):
# start delayed job
$delayed_job_worker = CucumberExternalWorker.new("rake jobs:work")
$delayed_job_worker.start
您可以在 features/step_definitions/whatever_steps.rb
中创建自己的步骤定义(希望有更好的名称)
When /^I wait for background jobs to complete$/ do
Delayed::Worker.new.work_off
end
这可以扩展到您希望 运行 使用该步骤的任何其他脚本。然后在测试中,它是这样的:
Then I should see the text "..."
When I wait for background jobs to complete
And I refresh the page
Then I should see the text "..."
使用 Cucumber 测试需要后台作业的最佳方法是什么?我需要 运行 DelayedJob 和 Sneakers 工作人员在后台进行测试 运行ning。
您可以运行后台的任何应用程序:
@pid = Process.spawn "C:/Apps/whatever.exe" Process.detach(@pid)
甚至在测试完成后将其杀死:
Process.kill('KILL', @pid) unless @pid.nil?
如果有人有类似的问题,我最后写了这篇文章(感谢 Square 博客 post):
require "timeout"
class CucumberExternalWorker
attr_accessor :worker_pid, :start_command
def initialize(start_command)
raise ArgumentError, "start_command was expected" if start_command.nil?
self.start_command = start_command
end
def start
puts "Trying to start #{start_command}..."
self.worker_pid = fork do
start_child
end
at_exit do
stop_child
end
end
private
def start_child
exec({ "RAILS_ENV" => Rails.env }, start_command)
end
def stop_child
puts "Trying to stop #{start_command}, pid: #{worker_pid}"
# send TERM and wait for exit
Process.kill("TERM", worker_pid)
begin
Timeout.timeout(10) do
Process.waitpid(worker_pid)
puts "Process #{start_command} stopped successfully"
end
rescue Timeout::Error
# Kill process if could not exit in 10 seconds
puts "Sending KILL signal to #{start_command}, pid: #{worker_pid}"
Process.kill("KILL", worker_pid)
end
end
end
这可以调用如下(将其添加到黄瓜的 env.rb
):
# start delayed job
$delayed_job_worker = CucumberExternalWorker.new("rake jobs:work")
$delayed_job_worker.start
您可以在 features/step_definitions/whatever_steps.rb
中创建自己的步骤定义(希望有更好的名称)
When /^I wait for background jobs to complete$/ do
Delayed::Worker.new.work_off
end
这可以扩展到您希望 运行 使用该步骤的任何其他脚本。然后在测试中,它是这样的:
Then I should see the text "..."
When I wait for background jobs to complete
And I refresh the page
Then I should see the text "..."