Is it possible to check if an error was raised? #239
-
For example, on RSpec: def div(a, b)
raise "Division by 0 error" if b == 0
a / b
end
describe "#div" do
context "when b is zero" do
it "raises error" do
calculator = Calculator.new
expect { calculator.div(3, 0) }.to raise_error(RuntimeError)
# or
expect { calculator.div(3, 0) }.to raise_error("Division by 0 error")
# or
expect { calculator.div(3, 0) }.to raise_error
end
end
end On GDScript I have this class: class_name Grid
func _init(width: int, height: int) -> void:
if width % 2 == 0 or height % 2 == 0: assert(false, "Grid must be odd-sized")
# or
if width % 2 == 0 or height % 2 == 0: push_error("Grid must be odd-sized") Is it possible to test this scenario? |
Beta Was this translation helpful? Give feedback.
Replies: 3 comments 7 replies
-
hi @stephannv yes for this scenario you can enable the error to failure reporting in the GdUnit settings.
It will catch both cases |
Beta Was this translation helpful? Give feedback.
-
Ok, if I understand you correctly, you want to test if a certain call generates an error and test for it.
sorry, for this kind of scenario no asserts exists at the moment, feel free to create a feature request |
Beta Was this translation helpful? Give feedback.
-
@stephannv the next version v4.1.4 will provide the new func test_is_success() -> void:
await assert_error(func (): GodotErrorTestClass.new().test(0)).is_success()
func test_is_assert_failed() -> void:
await assert_error(func (): GodotErrorTestClass.new().test(1))\
.is_runtime_error('Assertion failed: this is an assert error')
func test_is_push_warning() -> void:
await assert_error(func (): GodotErrorTestClass.new().test(2))\
.is_push_warning('this is an push_warning')
func test_is_push_error() -> void:
await assert_error(func (): GodotErrorTestClass.new().test(3))\
.is_push_error('this is an push_error')
func test_is_runtime_error() -> void:
await assert_error(func (): GodotErrorTestClass.new().test(4))\
.is_runtime_error("Division by zero error in operator '/'.") |
Beta Was this translation helpful? Give feedback.
Ok, if I understand you correctly, you want to test if a certain call generates an error and test for it.
sorry, for this kind of scenario no asserts exists at the moment, feel free to create a feature request