diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml index 3ad3f37..e27792c 100644 --- a/.pre-commit-config.yaml +++ b/.pre-commit-config.yaml @@ -8,7 +8,7 @@ repos: language: python additional_dependencies: [pygments, restructuredtext_lint] - repo: https://github.com/astral-sh/ruff-pre-commit - rev: v0.15.16 + rev: v0.16.7 hooks: - id: ruff args: ["--fix"] diff --git a/random_order/cache.py b/random_order/cache.py index 8fa6827..5c3b305 100644 --- a/random_order/cache.py +++ b/random_order/cache.py @@ -21,7 +21,7 @@ def process_failed_first_last_failed(session, config, items): # Get the names of last failed tests last_failed = [] - for key in last_failed_raw.keys(): + for key in last_failed_raw: parts = key.split("::") if len(parts) == 3: last_failed.append(tuple(parts)) diff --git a/random_order/plugin.py b/random_order/plugin.py index 0b85f29..51d0164 100644 --- a/random_order/plugin.py +++ b/random_order/plugin.py @@ -58,9 +58,7 @@ def pytest_report_header(config): plugin = Config(config) if not plugin.is_enabled: return "Test order randomisation NOT enabled. Enable with --random-order or --random-order-bucket=" - return ("Using --random-order-bucket={plugin.bucket_type}\nUsing --random-order-seed={plugin.seed}\n").format( - plugin=plugin - ) + return f"Using --random-order-bucket={plugin.bucket_type}\nUsing --random-order-seed={plugin.seed}\n" def pytest_collection_modifyitems(session, config, items): @@ -85,10 +83,9 @@ def pytest_collection_modifyitems(session, config, items): session=session, ) - except Exception as e: - # See the finally block -- we only fail if we have lost user's tests. + except Exception as e: # noqa: BLE001 -- see the finally block, we only fail if we have lost user's tests. _, _, exc_tb = sys.exc_info() - failure = "pytest-random-order plugin has failed with {0!r}:\n{1}".format( + failure = "pytest-random-order plugin has failed with {!r}:\n{}".format( e, "".join(traceback.format_tb(exc_tb, 10)) ) if not hasattr(pytest, "PytestWarning"): diff --git a/random_order/shuffler.py b/random_order/shuffler.py index 9e14f39..53c615f 100644 --- a/random_order/shuffler.py +++ b/random_order/shuffler.py @@ -1,5 +1,3 @@ -# -*- coding: utf-8 -*- - import random from collections import OrderedDict, namedtuple @@ -69,7 +67,7 @@ def get_full_bucket_key(item): bucket_keys = list(buckets.keys()) - for full_bucket_key in buckets.keys(): + for full_bucket_key in buckets: if full_bucket_key.bucket == FAILED_FIRST_LAST_FAILED_BUCKET_KEY: # Do not shuffle the last failed bucket continue @@ -93,7 +91,7 @@ def get_full_bucket_key(item): def _get_set_of_item_ids(items): - return set(item.nodeid for item in items) + return {item.nodeid for item in items} def _disable(item, session): diff --git a/setup.py b/setup.py index 3825a61..3654f18 100644 --- a/setup.py +++ b/setup.py @@ -1,6 +1,3 @@ -#!/usr/bin/env python -# -*- coding: utf-8 -*- - import codecs import os diff --git a/tests/conftest.py b/tests/conftest.py index c183eee..c472066 100644 --- a/tests/conftest.py +++ b/tests/conftest.py @@ -38,7 +38,7 @@ def get_test_calls(): def twenty_tests(): code = [] for i in range(20): - code.append("def test_a{0}(): assert True\n".format(str(i).zfill(2))) + code.append(f"def test_a{str(i).zfill(2)}(): assert True\n") return "".join(code) @@ -46,5 +46,5 @@ def twenty_tests(): def twenty_cls_tests(): code = [] for i in range(20): - code.append("\tdef test_b{0}(self): self.assertTrue\n".format(str(i).zfill(2))) + code.append(f"\tdef test_b{str(i).zfill(2)}(self): self.assertTrue\n") return "".join(code) diff --git a/tests/test_actual_test_runs.py b/tests/test_actual_test_runs.py index cbbc5a2..33b5fea 100644 --- a/tests/test_actual_test_runs.py +++ b/tests/test_actual_test_runs.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- import collections import re import textwrap @@ -164,7 +163,7 @@ def test_it_works_with_actual_tests(tmp_tree_of_tests, get_test_calls, bucket, m sequences = set() for x in range(5): - result = tmp_tree_of_tests.runpytest("--random-order-bucket={0}".format(bucket), "--verbose") + result = tmp_tree_of_tests.runpytest(f"--random-order-bucket={bucket}", "--verbose") result.assert_outcomes(passed=14, failed=3) seq = get_test_calls(result) check_call_sequence(seq, bucket=bucket) @@ -181,22 +180,22 @@ def test_random_order_seed_is_respected(testdir, twenty_tests, get_test_calls): "2": None, "3": None, } - for seed in call_sequences.keys(): - result = testdir.runpytest("--random-order-seed={0}".format(seed)) + for seed in call_sequences: + result = testdir.runpytest(f"--random-order-seed={seed}") result.stdout.fnmatch_lines( [ - "*Using --random-order-seed={0}*".format(seed), + f"*Using --random-order-seed={seed}*", ] ) result.assert_outcomes(passed=20) call_sequences[seed] = get_test_calls(result) - for seed in call_sequences.keys(): - result = testdir.runpytest("--random-order-seed={0}".format(seed)) + for seed, expected_calls in call_sequences.items(): + result = testdir.runpytest(f"--random-order-seed={seed}") result.assert_outcomes(passed=20) - assert call_sequences[seed] == get_test_calls(result) + assert expected_calls == get_test_calls(result) assert call_sequences["1"] != call_sequences["2"] != call_sequences["3"] @@ -217,7 +216,7 @@ def test_generated_seed_is_reported_and_run_can_be_reproduced(testdir, twenty_te break assert seed - result2 = testdir.runpytest("-v", "--random-order-seed={0}".format(seed)) + result2 = testdir.runpytest("-v", f"--random-order-seed={seed}") result2.assert_outcomes(passed=20) calls2 = get_test_calls(result2) assert calls == calls2 @@ -236,12 +235,12 @@ def test_generated_seed_is_reported_and_run_can_be_reproduced(testdir, twenty_te ], ) def test_failed_first(tmp_tree_of_tests, get_test_calls, bucket): - result1 = tmp_tree_of_tests.runpytest("--random-order-bucket={0}".format(bucket), "--verbose") + result1 = tmp_tree_of_tests.runpytest(f"--random-order-bucket={bucket}", "--verbose") result1.assert_outcomes(passed=14, failed=3) - result2 = tmp_tree_of_tests.runpytest("--random-order-bucket={0}".format(bucket), "--failed-first", "--verbose") + result2 = tmp_tree_of_tests.runpytest(f"--random-order-bucket={bucket}", "--failed-first", "--verbose") result2.assert_outcomes(passed=14, failed=3) calls2 = get_test_calls(result2) - first_three_tests = set(c.name for c in calls2[:3]) - assert set(["test_a1", "test_b2", "test_ee2"]) == first_three_tests + first_three_tests = {c.name for c in calls2[:3]} + assert {"test_a1", "test_b2", "test_ee2"} == first_three_tests diff --git a/tests/test_doctests.py b/tests/test_doctests.py index 443b9b7..f20f3d9 100644 --- a/tests/test_doctests.py +++ b/tests/test_doctests.py @@ -1,4 +1,3 @@ -# -*- coding: utf-8 -*- import textwrap import pytest @@ -56,7 +55,7 @@ def subtract(a, b): def test_doctests(tmp_tree_of_tests, get_test_calls, bucket): result1 = tmp_tree_of_tests.runpytest( "--doctest-modules", - "--random-order-bucket={0}".format(bucket), + f"--random-order-bucket={bucket}", "--verbose", "-s", ) diff --git a/tests/test_markers.py b/tests/test_markers.py index a301c37..8bd58de 100644 --- a/tests/test_markers.py +++ b/tests/test_markers.py @@ -4,13 +4,13 @@ @pytest.mark.parametrize("disabled", [True, False]) def test_marker_disables_random_order_in_module(testdir, twenty_tests, get_test_calls, disabled): testdir.makepyfile( - "import pytest\n" + ("pytestmark = pytest.mark.random_order(disabled={0})\n".format(disabled)) + twenty_tests + "import pytest\n" + (f"pytestmark = pytest.mark.random_order(disabled={disabled})\n") + twenty_tests ) result = testdir.runpytest("--random-order", "-v") result.assert_outcomes(passed=20) names = [c.name for c in get_test_calls(result)] - sorted_names = sorted(list(names)) + sorted_names = sorted(names) if disabled: assert names == sorted_names @@ -24,7 +24,7 @@ def test_marker_disables_random_order_in_class(testdir, twenty_cls_tests, get_te "import pytest\n\n" + "from unittest import TestCase\n\n" + "class MyTest(TestCase):\n" - + "\tpytestmark = pytest.mark.random_order(disabled={0})\n".format(disabled) + + f"\tpytestmark = pytest.mark.random_order(disabled={disabled})\n" + twenty_cls_tests + "\n" ) @@ -32,7 +32,7 @@ def test_marker_disables_random_order_in_class(testdir, twenty_cls_tests, get_te result = testdir.runpytest("--random-order", "-v") result.assert_outcomes(passed=20) names = [c.name for c in get_test_calls(result)] - sorted_names = sorted(list(names)) + sorted_names = sorted(names) if disabled: assert names == sorted_names