Skip to content

Commit

Permalink
Handle failing imports / collection errors (#176)
Browse files Browse the repository at this point in the history
The tests are based on existing tests in pytest, which would fail with
pytest-randomly being used for its tests.
  • Loading branch information
blueyed authored and adamchainz committed Apr 5, 2019
1 parent 6b513ee commit 0947405
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 2 deletions.
3 changes: 3 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@ Pending Release
.. Insert new release notes below this line
* Update Python support to 3.5-3.7, as 3.4 has reached its end of life.
* Handle CollectErrors and ImportErrors during collection when accessing
``item.module``.


2.1.1 (2019-03-26)
------------------
Expand Down
10 changes: 8 additions & 2 deletions pytest_randomly.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@
import random
import time

from pytest import Collector

# factory-boy
try:
from factory.random import set_random_state as factory_set_random_state
Expand Down Expand Up @@ -135,10 +137,14 @@ def pytest_collection_modifyitems(session, config, items):
current_items = []
for item in items:

try:
item_module = getattr(item, 'module', None)
except (ImportError, Collector.CollectError):
item_module = None

if current_module is None:
current_module = getattr(item, 'module', None)
current_module = item_module

item_module = getattr(item, 'module', None)
if item_module != current_module:
module_items.append(shuffle_by_class(current_items))
current_items = [item]
Expand Down
14 changes: 14 additions & 0 deletions test_pytest_randomly.py
Original file line number Diff line number Diff line change
Expand Up @@ -602,3 +602,17 @@ def test_two():

out = ourtestdir.runpytest('--randomly-seed=1')
out.assert_outcomes(passed=2)


def test_failing_import(testdir):
"""Test with pytest raising CollectError or ImportError.
This happens when trying to access item.module during
pytest_collection_modifyitems.
"""
modcol = testdir.getmodulecol("import alksdjalskdjalkjals")
assert modcol.instance is None

modcol = testdir.getmodulecol("pytest_plugins='xasdlkj',")
with pytest.raises(ImportError):
modcol.obj

0 comments on commit 0947405

Please sign in to comment.