Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .pre-commit-config.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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"]
Expand Down
2 changes: 1 addition & 1 deletion random_order/cache.py
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
9 changes: 3 additions & 6 deletions random_order/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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=<bucket_type>"
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):
Expand All @@ -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"):
Expand Down
6 changes: 2 additions & 4 deletions random_order/shuffler.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
# -*- coding: utf-8 -*-

import random
from collections import OrderedDict, namedtuple

Expand Down Expand Up @@ -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
Expand All @@ -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):
Expand Down
3 changes: 0 additions & 3 deletions setup.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,3 @@
#!/usr/bin/env python
# -*- coding: utf-8 -*-

import codecs
import os

Expand Down
4 changes: 2 additions & 2 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,13 @@ 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)


@pytest.fixture
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)
25 changes: 12 additions & 13 deletions tests/test_actual_test_runs.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
import collections
import re
import textwrap
Expand Down Expand Up @@ -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)
Expand All @@ -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"]

Expand All @@ -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
Expand All @@ -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
3 changes: 1 addition & 2 deletions tests/test_doctests.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
# -*- coding: utf-8 -*-
import textwrap

import pytest
Expand Down Expand Up @@ -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",
)
Expand Down
8 changes: 4 additions & 4 deletions tests/test_markers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -24,15 +24,15 @@ 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"
)

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
Expand Down