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
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/).
### Added

### Changed
- Fixed a bug where randomization logic was being applied even when no random seed was provided (#31)

### Removed

Expand Down
18 changes: 13 additions & 5 deletions pytest_test_groups/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,20 +8,24 @@
# Import 3rd-party libs
from _pytest.config import create_terminal_writer


class StrEnum(str, Enum):
"""Custom StrEnum for Python < 3.11 compatibility."""
def __str__(self):
return self.value



class GroupBy(StrEnum):
DEFAULT = ""
FILENAME = "filename"


def get_group_default(items, group_count, group_id):
"""Get the items from the passed in group based on group count."""
start = _get_start(group_id, group_count)
return items[start:len(items):group_count]


def get_group_by_filename(items, group_count, group_id):
"""Get the items from the passed in group, split by files, based on group count."""
start = _get_start(group_id, group_count)
Expand All @@ -45,16 +49,19 @@ def get_group_by_filename(items, group_count, group_id):

return group_to_items[start]


def _get_start(group_id, group_count):
if not (1 <= group_id <= group_count):
raise pytest.UsageError('Invalid test-group argument')
return group_id - 1


groupByHandlers = {
GroupBy.DEFAULT: get_group_default,
GroupBy.FILENAME: get_group_by_filename
}


def pytest_addoption(parser):
group = parser.getgroup('split your tests into groups and run them')
group.addoption('--test-group-count', dest='test-group-count', type=int,
Expand All @@ -71,20 +78,21 @@ def _sort_in_original_order(items, orig_items):
items.sort(key=original_order.__getitem__)
return items


@pytest.hookimpl(hookwrapper=True)
def pytest_collection_modifyitems(session, config, items):
yield
group_count = config.getoption('test-group-count')
group_id = config.getoption('test-group')
group_by = config.getoption("test-group-by")
seed = config.getoption('random-seed', False)
seed = config.getoption('random-seed')

if not group_count or not group_id:
return

original_items = items[:]

if seed is not False:
if seed is not None:
seeded = Random(seed)
seeded.shuffle(items)

Expand All @@ -94,7 +102,7 @@ def pytest_collection_modifyitems(session, config, items):
if len(items) == 0:
raise pytest.UsageError('Invalid test-group argument')

if seed is not False:
if seed is not None:
items = _sort_in_original_order(items, original_items)

terminal_reporter = config.pluginmanager.get_plugin('terminalreporter')
Expand Down
5 changes: 4 additions & 1 deletion tests/test_pytest.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ def test_z(): pass

assert set(group_1 + group_2) == set(all_tests)


def test_group_by_files(testdir):
testdir.makepyfile(test_file_1="""
def test_a(): pass
Expand Down Expand Up @@ -161,6 +162,7 @@ def test_e(): pass
)
result.assertoutcome(passed=5)


def test_group_runs_after_std_item_collection(testdir):
"""If @pytest.hookimpl(hookwrapper=True) is not used, the plugin will split the item
list of items before other filtering (like -k) has been applied. This could potentially
Expand All @@ -180,6 +182,7 @@ def test_y(): pass
result = testdir.runpytest_subprocess('-k', 'test_y', '--test-group-count', '2', '--test-group', '2')
assert 'Invalid test-group argument' in result.stderr.str()


def test_random_group_runs_in_original_order(testdir):
"""When running tests with a random seed, check test order is unchanged"""
testdir.makepyfile("""
Expand All @@ -197,4 +200,4 @@ def test_b(): pass
'--test-group', '1',
'--test-group-random-seed', '5')
group_1 = [x.item.name for x in result.calls if x._name == 'pytest_runtest_call']
assert group_1 == sorted(group_1, reverse=True)
assert group_1 == sorted(group_1, reverse=True)