Cóż można rozszerzyć Test::Unit::Assertions
, aby zrobić to, co chcesz, nie sądzę, nie ma wbudowany sposób, aby to zrobić. Być może coś takiego (szybkie & brudny):
require 'test/unit'
module Test::Unit::Assertions
def safe_assert(test, msg=nil)
passed = msg.nil? ? assert(test) : assert(test,msg)
ensure
puts 'take screenshot' unless passed
end
end
class MyTest < Test::Unit::TestCase
def setup
puts 'setup'
end
def teardown
puts 'teardown'
end
def test_something
safe_assert true
puts 'before failing assert'
safe_assert false, "message"
puts 'after failing assert'
end
end
wyjściowa:
Loaded suite unittest
Started
setup
before failing assert
take screenshot
teardown
F
Finished in 0.001094 seconds.
1) Failure:
test_something(MyTest) [unittest.rb:5]:
message
1 tests, 2 assertions, 1 failures, 0 errors, 0 skips
Test run options: --seed 58428
EDIT: można rzeczywiście przekazać args do assert
w prostszy sposób:
module Test::Unit::Assertions
def safe_assert(*args)
passed = assert(*args)
ensure
puts 'take screenshot' unless passed
end
end
również, możesz opakować standard assert
w bloku begin
- - -- end
, jeśli potrzebujesz tylko ta funkcja rzadko:
class MyTest < Test::Unit::TestCase
def test_something
safe_assert true
puts 'before failing assert'
begin
passed = assert false, "message"
ensure
puts 'take screenshot' unless passed
end
puts 'after failing assert'
end
end
lub można zbudować metodę zapewniającą zrzut ekranu podobny do poniższego przykładu. To wydaje się być dla mnie najczystszym sposobem:
def screenshot_on_fail
passed = yield
ensure
puts 'take screenshot' unless passed
end
class MyTest < Test::Unit::TestCase
def test_something_else
screenshot_on_fail do
assert true
end
screenshot_on_fail do
assert false, 'message'
end
end
end
To wygląda na dobry sposób, dzięki! –
nie ma za co! nie zapomnij przyjąć odpowiedzi, jeśli to rozwiązało twój problem :) –
Sprawdź moją edycję, jest rzeczywiście o wiele czystszy sposób na to, co przyszło mi do głowy w tej chwili ':)' –