Skip to content

Commit

Permalink
Add BuiltIn.robot_running and BuiltIn.dry_run_active
Browse files Browse the repository at this point in the history
New propertys for libraries and other extensions. Fixes #4666.
  • Loading branch information
pekkaklarck committed Feb 28, 2023
1 parent aa1ac6b commit 2b6c764
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 0 deletions.
11 changes: 11 additions & 0 deletions atest/robot/standard_libraries/builtin/builtin_propertys.robot
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
*** Settings ***
Resource atest_resource.robot

*** Test Cases ***
Normal run
Run Tests ${EMPTY} standard_libraries/builtin/builtin_propertys.robot
Check Test Case Test propertys

Dry-run
Run Tests --dryrun --variable DRYRUN:True standard_libraries/builtin/builtin_propertys.robot
Check Test Case Test propertys
12 changes: 12 additions & 0 deletions atest/testdata/standard_libraries/builtin/BuiltInPropertys.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from robot.libraries.BuiltIn import BuiltIn


class BuiltInPropertys:

def __init__(self, dry_run=False):
assert BuiltIn().robot_running is True
assert BuiltIn().dry_run_active is dry_run

def keyword(self):
assert BuiltIn().robot_running is True
assert BuiltIn().dry_run_active is False
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
*** Settings ***
Library BuiltInPropertys.py ${DRYRUN}

*** Variables ***
${DRYRUN} False

*** Test Cases ***
Test propertys
Keyword
22 changes: 22 additions & 0 deletions src/robot/libraries/BuiltIn.py
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,28 @@ def decorator(method):

class _BuiltInBase:

@property
def robot_running(self) -> bool:
"""Return True/False depending on is Robot Framework running or not.
Can be used by libraries and other extensions.
New in Robot Framework 6.1.
"""
return EXECUTION_CONTEXTS.current is not None

@property
def dry_run_active(self) -> bool:
"""Return True/False depending on is dry-run active or not.
Can be used by libraries and other extensions. Notice that library
keywords are not run at all in dry-run, but library ``__init__``
can utilize this information.
New in Robot Framework 6.1.
"""
return self.robot_running and self._context.dry_run

@property
def _context(self):
return self._get_context()
Expand Down
9 changes: 9 additions & 0 deletions utest/api/test_using_libraries.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,15 @@ def test_suite_doc_and_metadata(self):
BuiltIn().set_suite_metadata, 'name', 'value')


class TestBuiltInPropertys(unittest.TestCase):

def test_robot_running(self):
assert_equal(BuiltIn().robot_running, False)

def test_dry_run_active(self):
assert_equal(BuiltIn().dry_run_active, False)


class TestDateTime(unittest.TestCase):

def test_date_seconds(self):
Expand Down

0 comments on commit 2b6c764

Please sign in to comment.