我是自动化的新手,我需要帮助写这篇文章。
before(:each) do
# test start time
# video start time :00:00:00
setup function
end
it "test case 1" do
#perform action1
#perform action2 ...
end
it "test case 2" do
#perform action1
#perform action2 ...
end
after(:each) do
teardown function
#test end time
#video end time : 00:00:57
end
我的.rb文件看起来像我需要打印测试执行前后的时间
点子:
当批处理运行脚本开始执行时,视频也会同时开始录制 因此脚本打印的时间应与视频播放时间相匹配 视频播放时间从00:00:00开始 所以当第一个测试用例开始时,显示的执行时间应为00:00:00 类似地,第二个测试用例将在00:00:57然后在00:01:46
执行这样我可以匹配在视频的时间轴上执行的测试用例
Final Output:
Video start time: 00:00:00
Video end time : 00:00:57
Video start time: 00:00:57
Video end time: 00:01:40
我必须用红宝石写它。我怎样才能做到这一点。我应该使用计时器??
答案 0 :(得分:3)
跟踪时间只需存储开始时间,并将当前时间与您需要知道自那时起已经过了多少秒的时间进行比较:
start_time = Time.now
sleep(1)
Time.now - start_time # => 1.001715
为了在RSpec测试套件中的每个示例之前和之后执行某些操作,around
hooks是可行的方法。
around(:each) do |example|
# This happens before the example
example.run
# This happens after the example
end
要将秒数格式化为“HH:MM:SS”,您可以使用Ruby/Rails - How to convert seconds to time?中描述的Time.at
和strftime
的组合:
Time.at(123).utc.strftime("%H:%M:%S")
=> "00:02:03"
结合上述内容,您应该可以执行以下操作:
around(:each) do |example|
$start_time ||= Time.now
elapsed_seconds = Time.now - $start_time
puts "Video start time: #{Time.at(elapsed_seconds).utc.strftime("%H:%M:%S")}"
example.run
elapsed_seconds = Time.now - $start_time
puts "Video end time: #{Time.at(elapsed_seconds).utc.strftime("%H:%M:%S")}"
puts
end
这应该输出每个示例所经过的秒数,例如:
Video start time: 00:00:00
Video end time: 00:00:01
Video start time: 00:00:01
Video end time: 00:00:03
Video start time: 00:00:03
Video end time: 00:00:03