Skip to content

Commit ec6a405

Browse files
committed
First commit
1 parent 442ba79 commit ec6a405

File tree

8 files changed

+223
-0
lines changed

8 files changed

+223
-0
lines changed

.editorconfig

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# http://editorconfig.org
2+
3+
root = true
4+
5+
[*]
6+
indent_style = space
7+
indent_size = 4
8+
trim_trailing_whitespace = true
9+
insert_final_newline = true
10+
charset = utf-8
11+
end_of_line = lf
12+
13+
[*.py]
14+
max_line_length = 120
15+
16+
[*.bat]
17+
indent_style = tab
18+
end_of_line = crlf
19+
20+
[LICENSE]
21+
insert_final_newline = false
22+
23+
[Makefile]
24+
indent_style = tab

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
*.egg-info/
2+
.tox/
3+
dist/

.travis.yml

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
sudo: false
2+
language: python
3+
4+
matrix:
5+
include:
6+
- python: 3.5
7+
env: TOX_ENV=py35
8+
- python: 3.6
9+
env: TOX_ENV=py36
10+
- python: 3.7
11+
env: TOX_ENV=py37
12+
- python: pypy
13+
env: TOX_ENV=pypy
14+
- python: 3.6
15+
env: TOX_ENV=flake8
16+
17+
install:
18+
- pip install tox
19+
20+
script:
21+
- tox -e $TOX_ENV
22+
23+
before_cache:
24+
- rm -rf $HOME/.cache/pip/log
25+
26+
cache:
27+
directories:
28+
- $HOME/.cache/pip

MANIFEST.in

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
include LICENSE
2+
include README.rst
3+
4+
recursive-exclude * __pycache__
5+
recursive-exclude * *.py[co]

README.rst

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
=============
2+
pytest-randomness
3+
=============
4+
5+
`Pytest`_ plugin about random seed management
6+
7+
8+
Features
9+
--------
10+
11+
* TODO
12+
13+
14+
Requirements
15+
------------
16+
17+
* pytest >= 2.8
18+
19+
20+
Installation
21+
------------
22+
23+
You can install "pytest-randomness" via `pip`_ from `PyPI`_::
24+
25+
$ pip install pytest-randomness
26+
27+
28+
Usage
29+
-----
30+
31+
* TODO
32+
33+
Contributing
34+
------------
35+
Contributions are very welcome. Tests can be run with `tox`_, please ensure
36+
the coverage at least stays the same before you submit a pull request.
37+
38+
License
39+
-------
40+
41+
Distributed under the terms of the `MIT`_ license, "pytest-randomness" is free and open source software
42+
43+
44+
Issues
45+
------
46+
47+
If you encounter any problems, please `file an issue`_ along with a detailed description.
48+
49+
.. _`MIT`: http://opensource.org/licenses/MIT
50+
.. _`file an issue`: https://github.com/maks3w/pytest-randomness/issues
51+
.. _`Pytest`: https://github.com/pytest-dev/pytest
52+
.. _`tox`: https://tox.readthedocs.io/en/latest/
53+
.. _`pip`: https://pypi.org/project/pip/
54+
.. _`PyPI`: https://pypi.org/project

pytest_randomness.py

Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
import random
2+
3+
4+
class RandomnessPlugin(object):
5+
try:
6+
from factory import random as factory_random
7+
have_factory_boy = True
8+
except ImportError:
9+
have_factory_boy = False
10+
11+
try:
12+
from faker.generator import random as faker_random
13+
have_faker = True
14+
except ImportError:
15+
have_faker = False
16+
17+
def __init__(self, config):
18+
self.tests = config.cache.get('randomness', {})
19+
20+
def pytest_runtest_call(self, item):
21+
nodeid = item.nodeid
22+
if item.config.getoption('randomness_reset_seed') and nodeid in self.tests:
23+
self.set_seeds(self.tests[nodeid])
24+
else:
25+
self.tests[nodeid] = self.get_seeds()
26+
27+
def pytest_sessionfinish(self, session):
28+
session.config.cache.set('randomness', self.tests)
29+
30+
def get_seeds(self):
31+
seeds = {
32+
'python': random.getstate(),
33+
}
34+
if self.have_factory_boy:
35+
seeds['factory_boy'] = self.factory_random.get_random_state()
36+
if self.have_faker:
37+
seeds['faker'] = self.faker_random.getstate()
38+
return seeds
39+
40+
def set_seeds(self, seeds):
41+
def seed_to_state(seed):
42+
return seed[0], tuple(seed[1]), seed[2]
43+
44+
if 'python' in seeds:
45+
random.setstate(seed_to_state(seeds['python']))
46+
if self.have_factory_boy and 'factory_boy' in seeds:
47+
self.factory_random.set_random_state(seed_to_state(seeds['factory_boy']))
48+
if self.have_faker and 'faker' in seeds:
49+
self.faker_random.setstate(seed_to_state(seeds['faker']))
50+
return seeds
51+
52+
53+
def pytest_configure(config):
54+
config.pluginmanager.register(RandomnessPlugin(config), 'randomnessplugin')
55+
56+
57+
def pytest_addoption(parser):
58+
group = parser.getgroup('random', 'Random seed management')
59+
60+
group.addoption(
61+
'--randomness-reset-seed', action='store_true',
62+
dest='randomness_reset_seed', default=False,
63+
help="""Do pytest-randomness to reset random.seed() at the
64+
start of every individual test."""
65+
)

setup.py

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
from setuptools import setup
2+
3+
setup(
4+
name='pytest-randomness',
5+
version='0.1.0',
6+
author='Maks3w',
7+
license='MIT',
8+
url='https://github.com/Maks3w/pytest-randomness',
9+
description='Pytest plugin about random seed management',
10+
py_modules=['pytest_randomness'],
11+
install_requires=['pytest>=2.8'],
12+
classifiers=[
13+
'Development Status :: 3 - Alpha',
14+
'Framework :: Pytest',
15+
'Intended Audience :: Developers',
16+
'Topic :: Software Development :: Testing',
17+
'Programming Language :: Python :: 3 :: Only',
18+
'Programming Language :: Python :: 3',
19+
'Programming Language :: Python :: 3.5',
20+
'Programming Language :: Python :: 3.6',
21+
'Programming Language :: Python :: 3.7',
22+
'Programming Language :: Python :: Implementation :: CPython',
23+
'Programming Language :: Python :: Implementation :: PyPy',
24+
'Operating System :: OS Independent',
25+
'License :: OSI Approved :: MIT License',
26+
],
27+
entry_points={
28+
'pytest11': [
29+
'pytest_randomness = pytest_randomness',
30+
],
31+
},
32+
)

tox.ini

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
# For more information about tox, see https://tox.readthedocs.io/en/latest/
2+
[tox]
3+
envlist = py27,py34,py35,py36,py37,pypy,flake8
4+
5+
[testenv]
6+
deps = pytest>=3.0
7+
commands = pytest {posargs:tests}
8+
9+
[testenv:flake8]
10+
skip_install = true
11+
deps = flake8
12+
commands = flake8 pytest_randomness.py setup.py tests

0 commit comments

Comments
 (0)