API
pytest_robotframework
useful helpers for you to use in your pytest tests and conftest.py
files
RobotVariables = dict[str, object]
module-attribute
variable names and values to be set on the suite level. see the set_variables
function
AssertOptions
pass this as the second argument to an assert
statement to customize how it appears in the
robot log.
example:
assert foo == bar, AssertOptions(
log_pass=False, description="checking the value", fail_msg="assertion failed"
)
Source code in pytest_robotframework/__init__.py
668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 |
|
description = description
instance-attribute
normally, the asserted expression as it was written is displayed as the argument to the
assert
keyword in the robot log, but setting this value will display a custom message
instead. when a custom description is used, the original expression is logged inside the
keyword instead.
fail_message = fail_message
instance-attribute
optional description for the assert
statement that will be included in the
AssertionError
message if the assertion fails. equivalent to a normal assert
statement's
second argument
log_pass = log_pass
instance-attribute
whether to display the assertion as a keyword in the robot log when it passes.
by default, a passing assert
statement will display in the robot log as long as the
following conditions are met:
- the enable_assertion_pass_hook
pytest option is enabled
- it is not inside a hide_asserts_from_robot_log
context manager
(see enabling pytest assertions in the robot log).
- pytest is not run with the --no-asserts-in-robot-log
argument
failing assert
statements will show as keywords in the log as long as the
enable_assertion_pass_hook
pytest option is enabled. if it's disabled, the assertion error
will be logged, but not within a keyword.
example:
# (assuming all of these assertions pass)
# never displays in the robot log:
assert foo == bar, AssertOptions(log_pass=False)
# always displays in the robot log (as long as the `enable_assertion_pass_hook` pytest
# option is enabled):
assert foo == bar, AssertOptions(log_pass=True)
# displays in the robot log as only if all 3 conditions mentioned above are met:
assert foo == bar
__init__(*, log_pass=None, description=None, fail_message=None)
Source code in pytest_robotframework/__init__.py
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 |
|
as_keyword(name, *, doc='', tags=None, args=None, kwargs=None)
runs the body as a robot keyword.
example:
with as_keyword("do thing"):
...
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str
|
the name for the keyword |
required |
doc
|
str
|
the documentation to be displayed underneath the keyword in the robot log |
''
|
tags
|
tuple[str, ...] | None
|
tags for the keyword |
None
|
args
|
Iterable[str] | None
|
positional arguments to be displayed on the keyword in the robot log |
None
|
kwargs
|
Mapping[str, str] | None
|
keyword arguments to be displayed on the keyword in the robot log |
None
|
Source code in pytest_robotframework/__init__.py
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 |
|
catch_errors(cls)
errors that occur inside suite visitors and listeners do not cause the test run to fail. even
--exitonerror
doesn't catch every exception (see https://github.com/robotframework/robotframework/issues/4853).
this decorator will remember any errors that occurred inside listeners and suite visitors, then raise them after robot has finished running.
you don't need this if you are using the listener
or pre_rebot_modifier
decorator, as
those decorators use catch_errors
as well
Source code in pytest_robotframework/__init__.py
611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 |
|
hide_asserts_from_robot_log()
context manager for hiding multiple passing assert
statements from the robot log. note that
individual assert
statements using AssertOptions(log_pass=True)
take precedence, and that
failing assertions will always appear in the log.
when hiding only a single assert
statement, you should use AssertOptions(log=False)
instead.
example:
assert True # not hidden
with hide_asserts_from_robot_log():
assert True # hidden
assert True, AssertOptions(log_pass=True) # not hidden
Source code in pytest_robotframework/__init__.py
741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 |
|
import_resource(path)
imports the specified robot .resource
file when the suite execution begins.
use this when specifying robot resource imports at the top of the file.
to import libraries, use a regular python import
Source code in pytest_robotframework/__init__.py
66 67 68 69 70 71 72 73 74 75 76 |
|
keyword(fn=None, *, name=None, tags=None, module=None, wrap_context_manager=None, max_argument_length_in_log=100)
keyword(
*,
name: str | None = ...,
tags: tuple[str, ...] | None = ...,
module: str | None = ...,
wrap_context_manager: Literal[True],
max_argument_length_in_log: int | None = ...,
) -> _WrappedContextManagerKeywordDecorator
keyword(
*,
name: str | None = ...,
tags: tuple[str, ...] | None = ...,
module: str | None = ...,
wrap_context_manager: Literal[False],
max_argument_length_in_log: int | None = ...,
) -> _NonWrappedContextManagerKeywordDecorator
keyword(
*,
name: str | None = ...,
tags: tuple[str, ...] | None = ...,
module: str | None = ...,
wrap_context_manager: None = ...,
max_argument_length_in_log: int | None = ...,
) -> _FunctionKeywordDecorator
keyword(fn: Callable[P, Never]) -> Callable[P, Never]
keyword(
fn: Callable[P, AbstractContextManager[T]],
) -> Never
keyword(fn: Callable[P, T]) -> Callable[P, T]
marks a function as a keyword and makes it show in the robot log.
unlike robot's deco.keyword
decorator, this one will make your function appear as a keyword in
the robot log even when ran from a python file.
if the function returns a context manager, its body is included in the keyword (just make sure
the @keyword
decorator is above @contextmanager
)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
name
|
str | None
|
set a custom name for the keyword in the robot log (default is inferred from the decorated function name). equivalent to |
None
|
tags
|
tuple[str, ...] | None
|
equivalent to |
None
|
module
|
str | None
|
customize the module that appears top the left of the keyword name in the log. defaults to the function's actual module |
None
|
wrap_context_manager
|
bool | None
|
if the decorated function returns a context manager, whether or not to wrap the context manager instead of the function. you probably always want this to be |
None
|
max_argument_length_in_log
|
int | None
|
unlike in |
100
|
Source code in pytest_robotframework/__init__.py
472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 |
|
keywordify(obj, method_name, *, name=None, tags=None, module=None, wrap_context_manager=False)
patches a function to make it show as a keyword in the robot log.
you should only use this on third party modules that you don't control. if you want your own
function to show as a keyword you should decorate it with @keyword
instead (the one from this
module, not the one from robot)
Parameters:
Name | Type | Description | Default |
---|---|---|---|
obj
|
object
|
the object with the method to patch on it (this has to be specified separately as the object itself needs to be modified with the patched method) |
required |
method_name
|
str
|
the name of the method to patch |
required |
name
|
str | None
|
set a custom name for the keyword in the robot log (default is inferred from the decorated function name). equivalent to |
None
|
tags
|
tuple[str, ...] | None
|
equivalent to |
None
|
module
|
str | None
|
customize the module that appears top the left of the keyword name in the log. defaults to the function's actual module |
None
|
wrap_context_manager
|
bool
|
if the decorated function returns a context manager, whether or not to wrap the context manager instead of the function. you probably always want this to be |
False
|
Source code in pytest_robotframework/__init__.py
569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 |
|
set_variables(variables)
sets suite-level variables, equivalent to the *** Variables ***
section in a .robot
file.
also performs some validation checks that robot doesn't to make sure the variable has the correct type matching its prefix.
Source code in pytest_robotframework/__init__.py
52 53 54 55 56 57 58 59 60 |
|
RobotOptions
Bases: TypedDict
robot command-line arguments after being parsed by robot into a dict
.
for example, the following robot options:
ROBOT_OPTIONS="--listener Foo --listener Bar -d baz"
will be converted to a dict
like so:
{"listener": ["Foo", "Bar"], "outputdir": "baz"}
any options missing from this TypedDict
are not allowed to be modified as they interfere with
the functionality of this plugin. see configuration
for alternatives
Source code in pytest_robotframework/_internal/robot/utils.py
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 |
|
console
instance-attribute
the default in robot is "verbose", however pytest-robotframework changes the default to
"quiet"`, if you change this, then pytest and robot outputs will overlap.
consolecolors
instance-attribute
consolelinks
instance-attribute
only available in robotframework >=7.1.
currently does nothing. see https://github.com/DetachHead/pytest-robotframework/issues/305