Skip to content

Commit e8ff95f

Browse files
authored
Fixes randomisation of doctests (#37)
* Fixes issues with doctests reported in #36 - ``class``, ``package`` and ``module`` didn't work because ``DoctestItem`` doesn't have ``cls`` or ``module`` attributes. Thanks @tobywf. * Deprecates ``none`` bucket type. * With tox, run tests of pytest-random-order with both pytest 3 and 4.
1 parent c6bb5ac commit e8ff95f

File tree

6 files changed

+85
-8
lines changed

6 files changed

+85
-8
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ python:
88

99
install:
1010
- pip install tox
11-
- "TOX_ENV=${TRAVIS_PYTHON_VERSION/[0-9].[0-9]/py${TRAVIS_PYTHON_VERSION/.}}"
11+
- "TOX_ENV=${TRAVIS_PYTHON_VERSION/[0-9].[0-9]/py${TRAVIS_PYTHON_VERSION/.}-pytest4}"
1212
script: tox -e $TOX_ENV
1313

1414
before_cache:

README.rst

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -56,7 +56,7 @@ test modules (which is the default behaviour of the plugin), run pytest as follo
5656
$ pytest --random-order
5757

5858
To change the scope of re-ordering, run pytest with ``--random-order-bucket=<bucket-type>`` option
59-
where ``<bucket-type>`` can be ``class``, ``module``, ``package``, ``global``, or ``none``:
59+
where ``<bucket-type>`` can be ``class``, ``module``, ``package``, ``global``:
6060

6161
::
6262

@@ -123,8 +123,9 @@ grandparent
123123
global
124124
All tests fall in the same bucket, full randomness, tests probably take longer to run.
125125

126-
none
127-
Disable shuffling.
126+
none (deprecated)
127+
Disable shuffling. *Deprecated since 1.0.4 because this plugin no longer shuffles tests by default
128+
so there is nothing to disable.*
128129

129130

130131
If you have three buckets of tests ``A``, ``B``, and ``C`` with three tests ``1`` and ``2``, and ``3`` in each of them,
@@ -233,6 +234,14 @@ from being registered so its hooks won't be registered and its command line opti
233234
Changelog
234235
--------------
235236

237+
v1.0.4 (2018-11-30)
238+
+++++++++++++++++++
239+
240+
* Fixes issues with doctests reported in #36 - ``class``, ``package`` and ``module`` didn't work
241+
because ``DoctestItem`` doesn't have ``cls`` or ``module`` attributes. Thanks @tobywf.
242+
* Deprecate ``none`` bucket type.
243+
* With tox, run tests of pytest-random-order with both pytest 3 and 4.
244+
236245
v1.0.3 (2018-11-16)
237246
+++++++++++++++++++
238247

random_order/bucket_types.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
import functools
2+
import os.path
23
from collections import OrderedDict
34

45
bucket_type_keys = OrderedDict()
@@ -34,16 +35,20 @@ def get_global_key(item):
3435

3536
@bucket_type_key('package')
3637
def get_package_key(item):
38+
if not hasattr(item, "module"):
39+
return os.path.split(item.location[0])[0]
3740
return item.module.__package__
3841

3942

4043
@bucket_type_key('module')
4144
def get_module_key(item):
42-
return item.module.__name__
45+
return item.location[0]
4346

4447

4548
@bucket_type_key('class')
4649
def get_class_key(item):
50+
if not hasattr(item, "cls"):
51+
return item.location[0]
4752
if item.cls:
4853
return item.module.__name__, item.cls.__name__
4954
else:

setup.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ def read(fname):
1414

1515
setup(
1616
name='pytest-random-order',
17-
version='1.0.3',
17+
version='1.0.4',
1818
author='Jazeps Basko',
1919
author_email='jazeps.basko@gmail.com',
2020
maintainer='Jazeps Basko',

tests/test_doctests.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
# -*- coding: utf-8 -*-
2+
import py
3+
import pytest
4+
5+
6+
@pytest.fixture
7+
def tmp_tree_of_tests(testdir):
8+
"""
9+
Creates a directory structure:
10+
tmpdir/
11+
foo.py
12+
13+
"""
14+
15+
utils_package = testdir.mkpydir('utils')
16+
utils_package.join('__init__.py').write('')
17+
18+
utils_package.join('foo.py').write(py.code.Source('''
19+
def add(a, b):
20+
"""
21+
>>> add(1, 1)
22+
2
23+
>>> add(0, 0)
24+
0
25+
"""
26+
return a + b
27+
28+
def subtract(a, b):
29+
"""
30+
>>> subtract(1, 1)
31+
0
32+
>>> subtract(0, 2)
33+
-2
34+
"""
35+
return a - b
36+
'''))
37+
38+
return testdir
39+
40+
41+
@pytest.mark.parametrize('bucket', [
42+
'global',
43+
'package',
44+
'module',
45+
'class',
46+
'parent',
47+
'grandparent',
48+
'none',
49+
])
50+
def test_doctests(tmp_tree_of_tests, get_test_calls, bucket):
51+
result1 = tmp_tree_of_tests.runpytest(
52+
'--doctest-modules', '--random-order-bucket={0}'.format(bucket), '--verbose', '-s',
53+
)
54+
result1.assert_outcomes(passed=2, failed=0)
55+
assert 'PytestWarning' not in result1.stdout.str()
56+
assert 'PytestWarning' not in result1.stderr.str()

tox.ini

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,17 @@
11
[tox]
2-
envlist = py{35,36,37},flake8
2+
envlist =
3+
py35-pytest{32,39,4},
4+
py36-pytest{32,39,4},
5+
py37-pytest{32,39,4},
6+
flake8
37

48
[testenv]
59
deps =
6-
pytest
10+
pytest32: pytest >=3.2.0, <3.3.0
11+
pytest39: pytest >=3.9.0, <4.0.0
12+
pytest4: pytest >=4.0.0
713
pytest-xdist
14+
815
commands = py.test -n 2 {posargs:tests}
916

1017
[testenv:flake8]

0 commit comments

Comments
 (0)