Reputation: 193
So I have a question I want to ask my Ruby object:
Are there any failed scenarios in my testing results?
The object that I am inquiring looks like this:
=> #<Newman::Results:0x007fdcc2b48b28 @environment="dev", @build_name="customers", @build_id="119f3875-3793-4ae7-9155-78f0ce8c7047", @scenarios=[#<Newman::Scenario:0x007fdcc2b489c0 @name="POST /customers/zuora/create", @assertions=[#<Newman::Assertion:0x007fdcc2b48970 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b48948 @name="GET ssoauthentication/../Authentication/token", @assertions=[#<Newman::Assertion:0x007fdcc2b488f8 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b488d0 @name="GET /users?CustomerId={customerId}", @assertions=[#<Newman::Assertion:0x007fdcc2b48880 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b48858 @name="GET /locations?CustomerId={customerId}", @assertions=[#<Newman::Assertion:0x007fdcc2b48808 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b487e0 @name="PUT/users/{userId}/avatar", @assertions=[#<Newman::Assertion:0x007fdcc2b48790 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b48768 @name="PUT /customers/{customerId}/logo", @assertions=[#<Newman::Assertion:0x007fdcc2b48718 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b486f0 @name="POST /customers/{customerId}/positions", @assertions=[#<Newman::Assertion:0x007fdcc2b486a0 @test="Successful POST request", @exception_message="expected 200 to be one of [ 201, 202 ]", @exception_source="AssertionError: expected 200 to be one of [ 201, 202 ]\n at Object.eval sandbox-script.js:1:7)">]>, #<Newman::Scenario:0x007fdcc2b48678 @name="POST /users", @assertions=[#<Newman::Assertion:0x007fdcc2b48628 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b48600 @name="POST /users/{userId}/invitation", @assertions=[#<Newman::Assertion:0x007fdcc2b485b0 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b48588 @name="POST /registration", @assertions=[#<Newman::Assertion:0x007fdcc2b48538 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b48510 @name="PUT /users/{userId}/avatar", @assertions=[#<Newman::Assertion:0x007fdcc2b484c0 @test="Status code is 200">]>, #<Newman::Scenario:0x007fdcc2b48498 @name="post to slack", @assertions=[#<Newman::Assertion:0x007fdcc2b48448 @test="Status Code is correct">]>]>
...but the gist is that there's a Results object, which has a Scenarios
array filled with 1 or more Scenario
objects. Each Scenario
object has an Assertions
array filled with 1 or more Assertion
objects, and each Assertion
object contains an @exception_message
attribute. This @exception_message
attribute will be nil
unless the assertion fails, in which case it will be true
.
So, going back to the question ("Are there any failed scenarios in my testing results?"): what is the best way to ask this in Ruby (not Rails)?
My best attempt, where results
is my parsed results file and true or false is my desired answer:
results.scenarios.to_a.find { |scenario| scenario.assertions.to_a.find { |assertion| assertion.exception_message != nil }}
This seems to work but there must be a more efficient way.
Upvotes: 0
Views: 154
Reputation: 28305
Here's a more concise way of writing your above logic:
results.scenarios.any? do |scenario|
scenario.assertions.any?(&:exception_message)
end
However, that's still quite a complex block of logic to put in one place. You could break this down further by doing something like:
results.scenarios.any?(&:failed?)
class Newman::Scenario
def failed?
assertions.any?(&:exception_message)
end
end
Or even, to take it a step further:
results.scenario_failed?
class Newman::Results
def scenario_failed?
scenarios.any?(&:failed?)
end
end
class Newman::Scenario
def failed?
assertions.any?(&:exception_message)
end
end
Upvotes: 1