Skip to content

Commit

Permalink
Ruff: pytest related issues (#1131)
Browse files Browse the repository at this point in the history
* ruff PT006: replace csv in pytest parametrize with tuple

* ruff PT012: pytest raises should contain single statement
  • Loading branch information
bhirsz authored Oct 21, 2024
1 parent b624912 commit faf0f6a
Show file tree
Hide file tree
Showing 19 changed files with 45 additions and 41 deletions.
2 changes: 0 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -47,15 +47,13 @@ lint.ignore = [
"ANN201", # TODO: type annotation for return type (800 issues)
"ANN202", # TODO: type annotation for return type of private function
"ANN205", # TODO: type annotation for return type of staticmethod (30 issues)
"PT006", # TODO: pytest.parametrize with ("param", "param2") instead of "param,param2" (30 issues)
"FBT002", # TODO: boolean default positional argument in function definition
"RUF012", # TODO: mutable class attributes should be annotated with typing.classvar
"PLW0129", # TODO: assert on non-empty string literal
"T201", # TODO: print
"FBT003", # TODO: boolean positional value in function call
"ARG002", # TODO: unused method argument
"SLF001", # TODO: private member accessed
"PT012", # TODO: pytest.raises should contain a signle simple statement
"PLR0913", # TODO: too many arguments in fn def (consider if it's good to keep the rule)
"B007", # TODO: loop variable not used
"TRY003", # TODO: exception with long message outside exception class
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def test_rule(self):
self.check_rule(src_files=["test.robot"], expected_file="expected_output.txt")

@pytest.mark.parametrize(
"test_id, pattern",
("test_id", "pattern"),
[
(1, r"[$:{}]"),
(2, r"""[!.?/;+'"()[]{}#$%^&=<>|\]"""),
Expand Down
2 changes: 1 addition & 1 deletion tests/atest/rules/spacing/bad_block_indent/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@


class TestRuleAcceptance(RuleAcceptance):
@pytest.mark.parametrize("file_suffix, target_version", [("", ">=5"), ("_rf4", "==4.*"), ("_rf3", "==3.*")])
@pytest.mark.parametrize(("file_suffix", "target_version"), [("", ">=5"), ("_rf4", "==4.*"), ("_rf3", "==3.*")])
def test_rule(self, file_suffix, target_version):
self.check_rule(
expected_file=f"expected_output{file_suffix}.txt",
Expand Down
4 changes: 2 additions & 2 deletions tests/atest/rules/spacing/bad_indent/test_rule.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class TestRuleAcceptance(RuleAcceptance):
def test_templated_suite(self, config):
self.check_rule(config=config, src_files=["templated_suite.robot"], expected_file=None)

@pytest.mark.parametrize("file_suffix, target_version", [("", ">=5"), ("_rf4", "==4.*"), ("_rf3", "==3.*")])
@pytest.mark.parametrize(("file_suffix", "target_version"), [("", ">=5"), ("_rf4", "==4.*"), ("_rf3", "==3.*")])
def test_strict_3_spaces(self, file_suffix, target_version):
self.check_rule(
config="-c bad-indent:indent:3",
Expand All @@ -17,7 +17,7 @@ def test_strict_3_spaces(self, file_suffix, target_version):
target_version=target_version,
)

@pytest.mark.parametrize("file_suffix, target_version", [("", ">=5"), ("_rf4", "==4.*"), ("_rf3", "==3.*")])
@pytest.mark.parametrize(("file_suffix", "target_version"), [("", ">=5"), ("_rf4", "==4.*"), ("_rf3", "==3.*")])
def test_rule(self, file_suffix, target_version):
self.check_rule(
config="-c bad-indent:indent:-1",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def test_ignore_docs(self):
)

@pytest.mark.parametrize(
"ignore_run_keywords, expected_file",
("ignore_run_keywords", "expected_file"),
[(True, "expected_output_run_kw.txt"), (False, "expected_output_run_kw_off.txt")],
)
def test_run_keyword(self, ignore_run_keywords, expected_file):
Expand Down
5 changes: 3 additions & 2 deletions tests/atest/utils/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,10 @@ def check_rule(
if rule is None:
rule = [self.rule_name]
robocop_instance = configure_robocop_with_rule(config, Robocop(), rule, test_data, src_files, format=format)
with isolated_output() as output, pytest.raises(SystemExit):
with isolated_output() as output:
try:
robocop_instance.run()
with pytest.raises(SystemExit):
robocop_instance.run()
finally:
sys.stdout.flush()
result = get_result(output)
Expand Down
4 changes: 2 additions & 2 deletions tests/e2e/test_e2e.py
Original file line number Diff line number Diff line change
Expand Up @@ -203,7 +203,7 @@ def test_configure_rule_option(self, robocop_instance):
configure_robocop(robocop_instance, args=f"-c line-too-long:line_length:1000 {TEST_DATA_DIR}")

@pytest.mark.parametrize(
"rule, expected",
("rule", "expected"),
[
("idontexist", "Provided rule or report 'idontexist' does not exist."),
(
Expand All @@ -219,7 +219,7 @@ def test_configure_invalid_rule(self, robocop_instance, rule, expected):
assert expected in str(err)

@pytest.mark.parametrize(
"rules, expected",
("rules", "expected"),
[
("invalid", "Provided rule 'invalid' does not exist."),
("parsing-error,invalid", "Provided rule 'invalid' does not exist."),
Expand Down
2 changes: 1 addition & 1 deletion tests/utest/reports/test_file_stats_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@

class TestFileStatReport:
@pytest.mark.parametrize(
"previous_results, files, files_with_issues, compare_runs, output",
("previous_results", "files", "files_with_issues", "compare_runs", "output"),
[
(None, 0, set(), False, "\nNo files were processed."),
({}, 0, set(), False, "\nNo files were processed."),
Expand Down
2 changes: 1 addition & 1 deletion tests/utest/reports/test_reports.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@


@pytest.mark.parametrize(
"configured, expected",
("configured", "expected"),
[
(["timestamp", "sarif"], ["timestamp", "sarif"]),
(["timestamp"], ["timestamp"]),
Expand Down
4 changes: 2 additions & 2 deletions tests/utest/reports/test_return_status_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

class TestReturnStatus:
@pytest.mark.parametrize(
"param, configuration, quality_gates",
("param", "configuration", "quality_gates"),
[
("quality_gate", "", {"E": 0, "W": 0, "I": -1}),
("quality_gates", "", {"E": 0, "W": 0, "I": -1}),
Expand All @@ -21,7 +21,7 @@ def test_quality_gates_configuration(self, param, configuration, quality_gates):
assert report.quality_gate == quality_gates

@pytest.mark.parametrize(
"quality_gates, return_status",
("quality_gates", "return_status"),
[
("", 20),
("e=-1", 10),
Expand Down
2 changes: 1 addition & 1 deletion tests/utest/reports/test_rules_by_id_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@

class TestRulesByIdReport:
@pytest.mark.parametrize(
"previous_results, compare_results, issues_names, expected",
("previous_results", "compare_results", "issues_names", "expected"),
[
(None, False, FOUR_ISSUES, EXPECTED_NO_PREV),
({}, False, FOUR_ISSUES, EXPECTED_NO_PREV),
Expand Down
2 changes: 1 addition & 1 deletion tests/utest/reports/test_rules_by_severity_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

class TestRulesByIdReport:
@pytest.mark.parametrize(
"previous_results, compare_results, issues_names, expected",
("previous_results", "compare_results", "issues_names", "expected"),
[
(None, False, FOUR_ISSUES, EXPECTED_NO_PREV),
({}, False, FOUR_ISSUES, EXPECTED_NO_PREV),
Expand Down
2 changes: 1 addition & 1 deletion tests/utest/reports/test_time_taken_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_version_report(self, rule, compare_runs, previous_results):

@pytest.mark.parametrize("previous_results", [None, {"time_taken": "10.541"}]) # previous is ignored in save
@pytest.mark.parametrize("compare_runs", [True, False]) # saved even if compare is disabled
@pytest.mark.parametrize("get_report, expected_time", [(True, "2.000"), (False, "0.000")])
@pytest.mark.parametrize(("get_report", "expected_time"), [(True, "2.000"), (False, "0.000")])
def test_persistent_save(self, previous_results, compare_runs, get_report, expected_time):
mock_time = mock.Mock()
mock_time.side_effect = [1.0, 3.0]
Expand Down
17 changes: 11 additions & 6 deletions tests/utest/reports/test_timestamp_report.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ def test_timestamp_report(self, rule, compare_runs, previous_results):
assert "Reported: " in report.get_report()

@pytest.mark.parametrize(
"name, value, expected",
("name", "value", "expected"),
[
("timezone", "UTC", r"\+0000"),
("format", "hello", r"Reported: hello"),
Expand All @@ -37,22 +37,27 @@ def test_timestamp_report_configure(self, name, value, expected):
self._configure_and_run(name, value, expected)

@pytest.mark.parametrize(
"name, value, expected",
("name", "value", "expected"),
[
("", "", "Provided param '' for report 'timestamp' does not exist"),
("BAD", "", "Provided param 'BAD' for report 'timestamp' does not exist"),
("timezone", "BAD", "Provided timezone 'BAD' for report 'timestamp' is not valid."),
],
)
def test_timestamp_configure_invalid(self, name, value, expected):
report = TimestampReport()
with pytest.raises(ConfigGeneralError) as err:
report = TimestampReport()
report.configure(name, value)
report.get_report()
assert expected in str(err)

def test_invalid_timestamp_report(self):
report = TimestampReport()
report.configure("timezone", "BAD")
with pytest.raises(ConfigGeneralError) as err:
report.get_report()
assert "Provided timezone 'BAD' for report 'timestamp' is not valid." in str(err)

@pytest.mark.parametrize(
"name, value, expected",
("name", "value", "expected"),
[
("format", "", r".*([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2})"),
],
Expand Down
2 changes: 1 addition & 1 deletion tests/utest/test_external_rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -219,7 +219,7 @@ def test_use_deprecated_rule(self, robocop_pre_load, capsys):

class TestVersionMatching:
@pytest.mark.parametrize(
"rf_version, rules_status",
("rf_version", "rules_status"),
[
(
"4.0",
Expand Down
8 changes: 4 additions & 4 deletions tests/utest/test_including_excluding.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def get_message_with_id(rule_id):

class TestIncludingExcluding:
@pytest.mark.parametrize(
"included, excluded",
("included", "excluded"),
[
(["0101"], ["0102", "0501", "0402"]),
(["W0101", "0102"], ["0501", "0402"]),
Expand All @@ -26,7 +26,7 @@ def test_only_included(self, included, excluded, robocop_pre_load):
assert all(not robocop_pre_load.config.is_rule_enabled(get_message_with_id(msg)) for msg in excluded)

@pytest.mark.parametrize(
"patterns, included, excluded",
("patterns", "included", "excluded"),
[
(["01*"], [], ["0202", "0501", "0403"]),
(["01*", "*5"], ["0101", "0105", "0405"], ["0204", "0402"]),
Expand All @@ -42,7 +42,7 @@ def test_only_included_patterns(self, patterns, included, excluded, robocop_pre_
assert all(not robocop_pre_load.config.is_rule_enabled(get_message_with_id(msg)) for msg in excluded)

@pytest.mark.parametrize(
"included, excluded",
("included", "excluded"),
[
(["0101"], ["0102", "0501", "0402"]),
(["W0101", "0102"], ["0501", "0402"]),
Expand All @@ -57,7 +57,7 @@ def test_only_excluded(self, included, excluded, robocop_pre_load):
assert all(not robocop_pre_load.config.is_rule_enabled(get_message_with_id(msg)) for msg in excluded)

@pytest.mark.parametrize(
"patterns, included, excluded",
("patterns", "included", "excluded"),
[
(["01*"], ["0204", "0405", "0405"], ["0101", "0105"]),
(["01*", "*5"], ["0204"], ["0101", "0105", "0405", "0405"]),
Expand Down
6 changes: 3 additions & 3 deletions tests/utest/test_messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ def change_severity(msg, severity):
return msg

@pytest.mark.parametrize(
"severity, exp_sev",
("severity", "exp_sev"),
[
("e", "E"),
("error", "E"),
Expand Down Expand Up @@ -107,7 +107,7 @@ def test_parse_valid_configurable(self):
assert rule.config["Some"].value == 5

@pytest.mark.parametrize(
"source, range, range_exp",
("source", "range", "range_exp"),
[
("path/to/file1.robot", (None, None, None, None), (10, 1, 10, 1)),
("path/to/file1.robot", (15, None, None, 7), (15, 1, 15, 7)),
Expand Down Expand Up @@ -138,7 +138,7 @@ def test_prepare_message(self, valid_msg, source, range, range_exp):
assert msg.source == source

@pytest.mark.parametrize(
"kwargs, msg, exp",
("kwargs", "msg", "exp"),
[
(
{},
Expand Down
2 changes: 1 addition & 1 deletion tests/utest/test_thresholds.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ def get_message_with_sev_value(rule, sev_value):

class TestThresholds:
@pytest.mark.parametrize(
"threshold, included, excluded",
("threshold", "included", "excluded"),
[
("E", ["E"], ["I", "W"]),
("W", ["E", "W"], ["I"]),
Expand Down
16 changes: 8 additions & 8 deletions tests/utest/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def detect_from_file(file):

class TestParseAssignmentSignType:
@pytest.mark.parametrize(
"value, expected",
("value", "expected"),
[("none", ""), ("equal_sign", "="), ("space_and_equal_sign", " =")],
)
def test_happy_paths(self, value, expected):
Expand Down Expand Up @@ -101,7 +101,7 @@ def test_five_var_diff_three_keyword_diff_assignments(self):

class TestRecommendationFinder:
@pytest.mark.parametrize(
"name, normalized",
("name", "normalized"),
[
("justname", ("justname", "justname")),
("just_name", ("just name", "justname")),
Expand All @@ -115,7 +115,7 @@ def test_normalize(self, name, normalized):
assert actual == normalized

@pytest.mark.parametrize(
"name, candidates, similar",
("name", "candidates", "similar"),
[
("", ["some"], ""),
("some", [], ""),
Expand All @@ -141,7 +141,7 @@ def test_find_similar(self, name, candidates, similar):

class TestMisc:
@pytest.mark.parametrize(
"string, replaced",
("string", "replaced"),
[
(
"Keyword With Embedded ${var} Variable",
Expand Down Expand Up @@ -174,7 +174,7 @@ def test_remove_robot_vars(self, string, replaced):
assert actual == replaced

@pytest.mark.parametrize(
"string, exp_vars",
("string", "exp_vars"),
[
("${var}", [(0, 6)]),
("${var}}", [(0, 6)]),
Expand All @@ -194,7 +194,7 @@ def test_invalid_pattern_type(self):
assert r"Invalid regex pattern: bad escape \\9 at position 1" in str(error)

@pytest.mark.parametrize(
"var_name, normalized_var_name",
("var_name", "normalized_var_name"),
[
("var", "var"),
("my_var", "my_var"),
Expand All @@ -215,7 +215,7 @@ def test_remove_nested_variables(self, var_name, normalized_var_name):


@pytest.mark.parametrize(
"robot_version, rule_version, should_pass",
("robot_version", "rule_version", "should_pass"),
[
("6.0.2", ">=6", True),
("6.1a2.dev1", ">=6", True),
Expand Down Expand Up @@ -273,7 +273,7 @@ def test_invalid_version_specifier():


@pytest.mark.parametrize(
"string, expected_variables",
("string", "expected_variables"),
[
("string", []),
("$var", ["var"]),
Expand Down

0 comments on commit faf0f6a

Please sign in to comment.