Best Python code snippet using radish
matcher.py
Source:matcher.py
...15import radish.utils as utils16from radish.errors import RadishError, StepImplementationNotFoundError17from radish.models import Step18from radish.stepregistry import StepRegistry19def parse_matcher_config(matcher_config_path):20 """Parse the given matcher config file21 The matcher config file is expected to be a properly22 formatted YAML file, having the following structure:23 .. code-block:: yaml24 - step: <step text>25 should_match | should_not_match: <step implementation func name>26 with_arguments:27 - <args...>28 The YAML file can contain one or more of these ``step`` blocks.29 """30 with matcher_config_path.open("r", encoding="utf-8") as matcher_config_file:31 steps_matcher_config = utils.yaml_ordered_load(matcher_config_file)32 # the matcher config is empty - so we don't have to continue here33 if not steps_matcher_config:34 return None35 # validate the matcher config read from the YAML file36 for step_matcher_config in steps_matcher_config:37 # check if the config really has the `step` key38 if "step" not in step_matcher_config:39 raise RadishError("All Matcher Config must start with the 'step' key")40 # check if the config has either the `should_match` or `should_not_match` key41 if ("should_match" not in step_matcher_config) == (42 "should_not_match" not in step_matcher_config43 ): # noqa44 raise RadishError(45 "Each Matcher Config must either contain a 'should_match' or 'should_not_match' key"46 )47 # check if the config has any invalid keys48 valid_keys = {"step", "should_match", "should_not_match", "with_arguments"}49 if not set(step_matcher_config.keys()).issubset(valid_keys):50 raise RadishError(51 "All Matcher Config only allow the keys: "52 "{}. The following are not allowed: {}.".format(53 ", ".join(sorted(valid_keys)),54 ", ".join(55 sorted(set(step_matcher_config.keys()).difference(valid_keys))56 ),57 )58 )59 return steps_matcher_config60def run_matcher_tests(61 matcher_configs: List[Path], coverage_config, step_registry: StepRegistry62):63 """Run the matcher config tests against all Steps in the Registry"""64 #: holds a set of all covered Step Implementations65 # A Step Implementation only counts as covered if it66 # was successfully tested against a positive (should_match) test.67 covered_step_impls = set()68 for matcher_config_path in matcher_configs:69 match_config = parse_matcher_config(matcher_config_path)70 # nothing to match against, because config was empty71 if not match_config:72 print(73 cf.orange(74 "The matcher config {} was empty - Nothing to do :)".format(75 matcher_config_path76 )77 )78 )79 continue80 for step_to_match in match_config:81 # create a dummy Step object for the match config82 keyword, text = step_to_match["step"].split(maxsplit=1)83 step = Step(...
test_step_testing.py
Source:test_step_testing.py
...39 with pytest.raises(40 RadishError, match="All Matcher Config must start with the 'step' key"41 ):42 # WHEN43 parse_matcher_config(matcher_config_path)44def test_parse_config_should_fail_if_should_match_keys_not_present(tmp_path):45 # GIVEN46 matcher_config_path = create_matcher_config_file(47 tmp_path,48 """49 - step: step text50 """,51 )52 # THEN53 with pytest.raises(54 RadishError,55 match=(56 "Each Matcher Config must either contain "57 "a 'should_match' or 'should_not_match' key"58 ),59 ):60 # WHEN61 parse_matcher_config(matcher_config_path)62def test_parse_config_should_fail_if_should_match_key_and_should_match_present(63 tmp_path,64):65 # GIVEN66 matcher_config_path = create_matcher_config_file(67 tmp_path,68 """69 - step: step text70 should_match: step func71 should_not_match: step func72 """,73 )74 # THEN75 with pytest.raises(76 RadishError,77 match=(78 "Each Matcher Config must either contain "79 "a 'should_match' or 'should_not_match' key"80 ),81 ):82 # WHEN83 parse_matcher_config(matcher_config_path)84def test_parse_config_should_pass_if_should_match_key_not_and_should_not_match_present(85 tmp_path,86):87 # GIVEN88 matcher_config_path = create_matcher_config_file(89 tmp_path,90 """91 - step: step text92 should_not_match: step func93 """,94 )95 # WHEN96 match_config = parse_matcher_config(matcher_config_path)97 # THEN98 assert match_config[0]["should_not_match"] == "step func"99def test_parse_config_should_pass_if_should_not_match_key_not_and_should_match_present(100 tmp_path,101):102 # GIVEN103 matcher_config_path = create_matcher_config_file(104 tmp_path,105 """106 - step: step text107 should_match: step func108 """,109 )110 # WHEN111 match_config = parse_matcher_config(matcher_config_path)112 # THEN113 assert match_config[0]["should_match"] == "step func"114def test_parse_config_should_pass_if_with_arguments_is_specified(tmp_path):115 # GIVEN116 matcher_config_path = create_matcher_config_file(117 tmp_path,118 """119 - step: step text120 should_match: step func121 with_arguments:122 - foo: bar123 """,124 )125 # WHEN126 match_config = parse_matcher_config(matcher_config_path)127 # THEN128 assert match_config[0]["with_arguments"] == [{"foo": "bar"}]129def test_parse_config_should_fail_for_invalid_keys(tmp_path):130 # GIVEN131 matcher_config_path = create_matcher_config_file(132 tmp_path,133 """134 - step: step text135 should_match: step func136 invalid_key: x137 another_invalid_key: y138 """,139 )140 # THEN141 with pytest.raises(142 RadishError,143 match=(144 "All Matcher Config only allow the keys: "145 "should_match, should_not_match, step, with_arguments. "146 "The following are not allowed: another_invalid_key, invalid_key."147 ),148 ):149 # WHEN150 parse_matcher_config(matcher_config_path)151def test_print_failure_should_output_only_red_cross_if_no_func_and_no_errors(capsys):152 # GIVEN153 expected_output = "â \n"154 # WHEN155 print_failure(None, [])156 actual_output = capsys.readouterr().out157 # THEN158 assert actual_output == expected_output159def test_print_failure_should_output_func_location(capsys):160 # GIVEN161 def func():162 pass163 expected_output = r"â \(at .*?test_step_testing.py:{}\)\n".format(164 func.__code__.co_firstlineno...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!