From f99078dc007c853ca4d8f321be926d0fd08e8cf1 Mon Sep 17 00:00:00 2001 From: andrejs Date: Thu, 29 Oct 2026 08:33:05 +0000 Subject: [PATCH] Add support for precondition and expected result definition --- .../lib/allure-ruby-commons.rb | 36 +++++++++++++++++++ .../allure_ruby_commons/model/test_result.rb | 12 +++++++ allure-ruby-commons/spec/unit/allure_spec.rb | 15 ++++++++ .../spec/unit/test_result_spec.rb | 18 ++++++++++ 4 files changed, 81 insertions(+) diff --git a/allure-ruby-commons/lib/allure-ruby-commons.rb b/allure-ruby-commons/lib/allure-ruby-commons.rb index c7ac813..53edb50 100644 --- a/allure-ruby-commons/lib/allure-ruby-commons.rb +++ b/allure-ruby-commons/lib/allure-ruby-commons.rb @@ -115,6 +115,42 @@ def description_html(description_html) end end + # Add precondition to current test case + # @param [String] precondition + # @return [void] + def add_precondition(precondition) + lifecycle.update_test_case do |test_case| + test_case.precondition = precondition + end + end + + # Add html precondition to current test case + # @param [String] precondition_html + # @return [void] + def precondition_html(precondition_html) + lifecycle.update_test_case do |test_case| + test_case.precondition_html = precondition_html + end + end + + # Add expected result to current test case + # @param [String] expected_result + # @return [void] + def add_expected_result(expected_result) + lifecycle.update_test_case do |test_case| + test_case.expected_result = expected_result + end + end + + # Add html expected result to current test case + # @param [String] expected_result_html + # @return [void] + def expected_result_html(expected_result_html) + lifecycle.update_test_case do |test_case| + test_case.expected_result_html = expected_result_html + end + end + # Add parameter to current test case # @param [String] name # @param [String] value diff --git a/allure-ruby-commons/lib/allure_ruby_commons/model/test_result.rb b/allure-ruby-commons/lib/allure_ruby_commons/model/test_result.rb index 84e01f6..1064736 100644 --- a/allure-ruby-commons/lib/allure_ruby_commons/model/test_result.rb +++ b/allure-ruby-commons/lib/allure_ruby_commons/model/test_result.rb @@ -12,6 +12,10 @@ class TestResult < ExecutableItem # @option options [Array] :title_path # @option options [String] :description # @option options [String] :description_html + # @option options [String] :precondition + # @option options [String] :precondition_html + # @option options [String] :expected_result + # @option options [String] :expected_result_html # @option options [String] :status ('broken') # @option options [String] :stage ('scheduled') # @option options [Allure::StatusDetails] :status_details @@ -28,6 +32,10 @@ def initialize(uuid: SecureRandom.uuid, history_id: SecureRandom.uuid, environme @history_id = history_id @full_name = options[:full_name] || "Unnamed" @title_path = options[:title_path] + @precondition = options[:precondition] + @precondition_html = options[:precondition_html] + @expected_result = options[:expected_result] + @expected_result_html = options[:expected_result_html] @labels = options[:labels] || [] @links = options[:links] || [] @parameters << Parameter.new("environment", environment) if environment @@ -36,6 +44,10 @@ def initialize(uuid: SecureRandom.uuid, history_id: SecureRandom.uuid, environme attr_accessor :uuid, :full_name, :title_path, + :precondition, + :precondition_html, + :expected_result, + :expected_result_html, :labels, :links diff --git a/allure-ruby-commons/spec/unit/allure_spec.rb b/allure-ruby-commons/spec/unit/allure_spec.rb index ca338f2..e0f61e6 100644 --- a/allure-ruby-commons/spec/unit/allure_spec.rb +++ b/allure-ruby-commons/spec/unit/allure_spec.rb @@ -91,6 +91,21 @@ def lifecycle end end + context "with precondition and expected result helpers" do + [ + [:add_precondition, "precondition", "User is logged in"], + [:precondition_html, "preconditionHtml", "

User is logged in

"], + [:add_expected_result, "expectedResult", "Dashboard is displayed"], + [:expected_result_html, "expectedResultHtml", "

Dashboard is displayed

"] + ].each do |method, key, value| + it "sets test case #{key}" do + allure.public_send(method, value) + + expect(@test_case.to_hash[key]).to eq(value) + end + end + end + context "with parameter helpers" do it "adds test parameter" do allure.parameter("name", "value", excluded: true, mode: Allure::Parameter::MASKED) diff --git a/allure-ruby-commons/spec/unit/test_result_spec.rb b/allure-ruby-commons/spec/unit/test_result_spec.rb index 3cf8238..f4b7ad1 100644 --- a/allure-ruby-commons/spec/unit/test_result_spec.rb +++ b/allure-ruby-commons/spec/unit/test_result_spec.rb @@ -146,6 +146,24 @@ end end + context "with preconditions and expected results" do + it "serializes fields supplied at initialization" do + result = Allure::TestResult.new( + precondition: "User is logged in", + precondition_html: "

User is logged in

", + expected_result: "Dashboard is displayed", + expected_result_html: "

Dashboard is displayed

" + ) + + expect(result.to_hash).to include( + "precondition" => "User is logged in", + "preconditionHtml" => "

User is logged in

", + "expectedResult" => "Dashboard is displayed", + "expectedResultHtml" => "

Dashboard is displayed

" + ) + end + end + context "with allure environment" do let(:environment) { "test" }