From 1126438d1a04b18831771462608844b3c1f775a4 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Wed, 15 Jul 2020 09:56:02 +0800 Subject: [PATCH 1/2] MDL-69278 behat: Mark tests as failed when unable to start Session --- lib/tests/behat/behat_hooks.php | 35 ++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php index 198479c1dec..5628e9240b3 100644 --- a/lib/tests/behat/behat_hooks.php +++ b/lib/tests/behat/behat_hooks.php @@ -86,6 +86,17 @@ class behat_hooks extends behat_base { */ protected static $currentstepexception = null; + /** + * If an Exception is thrown in the BeforeScenario hook it will cause the Scenario to be skipped, and the exit code + * to be non-zero triggering a potential rerun. + * + * To combat this the exception is stored and re-thrown when looking for exceptions. + * This allows the test to instead be failed and re-run correctly. + * + * @var null|Exception + */ + protected static $currentscenarioexception = null; + /** * If we are saving any kind of dump on failure we should use the same parent dir during a run. * @@ -361,8 +372,13 @@ EOF; // The `before_first_scenario_start_session` function will have started the session instead. return; } + self::$currentscenarioexception = null; - $this->restart_session(); + try { + $this->restart_session(); + } catch (Exception $e) { + self::$currentscenarioexception = $e; + } } /** @@ -373,6 +389,12 @@ EOF; */ public function before_scenario_hook(BeforeScenarioScope $scope) { global $DB; + if (self::$currentscenarioexception) { + // A BeforeScenario hook triggered an exception and marked this test as failed. + // Skip this hook as it will likely fail. + return; + } + $suitename = $scope->getSuite()->getName(); // Register behat selectors for theme, if suite is changed. We do it for every suite change. @@ -495,6 +517,12 @@ EOF; * @BeforeStep */ public function before_step_javascript(BeforeStepScope $scope) { + if (self::$currentscenarioexception) { + // A BeforeScenario hook triggered an exception and marked this test as failed. + // Skip this hook as it will likely fail. + return; + } + self::$currentstepexception = null; // Only run if JS. @@ -711,6 +739,11 @@ EOF; * @see Moodle\BehatExtension\EventDispatcher\Tester\ChainedStepTester */ public function i_look_for_exceptions() { + // If the scenario already failed in a hook throw the exception. + if (!is_null(self::$currentscenarioexception)) { + throw self::$currentscenarioexception; + } + // If the step already failed in a hook throw the exception. if (!is_null(self::$currentstepexception)) { throw self::$currentstepexception; From d377bf6b783abcbc33e431bbb49ce2cbe9b90742 Mon Sep 17 00:00:00 2001 From: Andrew Nicols Date: Thu, 16 Jul 2020 08:24:55 +0800 Subject: [PATCH 2/2] MDL-69278 behat: Correct variable name --- lib/tests/behat/behat_hooks.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/tests/behat/behat_hooks.php b/lib/tests/behat/behat_hooks.php index 5628e9240b3..5a47176811d 100644 --- a/lib/tests/behat/behat_hooks.php +++ b/lib/tests/behat/behat_hooks.php @@ -177,7 +177,7 @@ class behat_hooks extends behat_base { $message = <<