pytest-dev / pytest-random-order

pytest plugin to randomise the order of tests with some control over the randomness
MIT License
65 stars 10 forks source link

Some buckets don't work with doctests #36

Closed tobywf closed 5 years ago

tobywf commented 5 years ago

When running tests against doctests, a warning is printed for these buckets: package, module, and class. The buckets global, parent, grandparent, and none work fine. Seems to be because DoctestItems don't have module or cls attributes (see further down for output).

I'm not hugely familiar with pytest, so I don't know if this needs fixing on their side. Poking around with ipdb, there doesn't seem to be any attribute that could be used instead.

A simple solution for now might be to simply point out that some buckets aren't available when running against doctests in the pytest-random-order documentation, and to leave the warning as is.

I've come up with a minimal working example to reproduce, I'm using Python 3.7.1, pytest-4.0.1, py-1.7.0, pluggy-0.8.0 and random-order-1.0.3. Let me know if you need anything else!


foo.py:

def add(a, b):
    """
    >>> add(1, 1)
    2
    >>> add(0, 0)
    0
    """
    return a + b

Inside fresh virtualenv, using class in this case:

$ pip install pytest pytest-random-order
$ pytest --doctest-modules --random-order-bucket=class foo.py
=== test session starts ===
platform darwin -- Python 3.7.1, pytest-4.0.1, py-1.7.0, pluggy-0.8.0
Using --random-order-bucket=class
Using --random-order-seed=151339

rootdir: ., inifile:
plugins: random-order-1.0.3
collected 1 item

foo.py .            [100%]

=== warnings summary ===
env/lib/python3.7/site-packages/random_order/plugin.py:87
  env/lib/python3.7/site-packages/random_order/plugin.py:87: PytestWarning: pytest-random-order plugin has failed with AttributeError("'DoctestItem' object has no attribute 'cls'"):
    File "env/lib/python3.7/site-packages/random_order/plugin.py", line 75, in pytest_collection_modifyitems
      session=session,
    File "env/lib/python3.7/site-packages/random_order/shuffler.py", line 63, in _shuffle_items
      full_bucket_key = get_full_bucket_key(item)
    File "env/lib/python3.7/site-packages/random_order/shuffler.py", line 52, in get_full_bucket_key
      return ItemKey(bucket=bucket_key(item, session), disabled=disable(item, session))
    File "env/lib/python3.7/site-packages/random_order/bucket_types.py", line 16, in wrapped
      key = f(item)
    File "env/lib/python3.7/site-packages/random_order/bucket_types.py", line 47, in get_class_key
      if item.cls:

    warnings.warn(pytest.PytestWarning(failure))

-- Docs: https://docs.pytest.org/en/latest/warnings.html
=== 1 passed, 1 warnings in 0.01 seconds ===
jbasko commented 5 years ago

Thanks for the very detailed and helpful bug report. These aren't just warnings, these actually are plugin failures so no randomisation happens as a result.

Fixed in v1.0.4

tobywf commented 5 years ago

Awesome, thank you!

I should have said that while doctests didn't get shuffled, normal tests were still shuffled, so I thought it wasn't a huge deal breaker. But happy to report 1.0.4 fixes it completely, thank you so much for the quick turn around!