Skip to content
This repository has been archived by the owner on May 4, 2021. It is now read-only.

Commit

Permalink
Merge pull request #12 from nicoddemus/pytest-plugin
Browse files Browse the repository at this point in the history
Pytest plugin
  • Loading branch information
herczy committed Mar 17, 2015
2 parents cb94202 + 3f47531 commit 760ffb0
Show file tree
Hide file tree
Showing 7 changed files with 116 additions and 5 deletions.
1 change: 1 addition & 0 deletions AUTHORS
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,4 @@ Attila M. Magyar <[email protected]>
Laszlo Attila Toth <[email protected]>
Imre Halasz <[email protected]>
Medgyes Akos Adam <[email protected]>
Bruno Oliveira <[email protected]>
17 changes: 13 additions & 4 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -37,17 +37,26 @@ Testing with typesafety
=======================

Typesafety is meant to be used during testing. True, the checker can be
turned on in production code but the performace slowdown can make this
turned on in production code but the performance slowdown can make this
undesirable.

Currently, our preferred tool for running unit tests is nosetests. Using
the typesafety tool with nose is very simple:
Typesafety comes with builtin plugins for two popular testing frameworks,
`nosetests <https://nose.readthedocs.org>`_ and `pytest <http://pytest.org/>`_
(our preferred tool at Balabit is nosetests), and using it is very simple.

For nose:

::

$ nosetests --enable-typesafety mymodule

And voila! Type checking is enabled for the module ``mymodule``.
And similarly for pytest:

::

$ py.test --enable-typesafety mymodule

And voila! Type checking is enabled for the module ``mymodule`` during tests.

Enabling typesafety manually
----------------------------
Expand Down
3 changes: 2 additions & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,8 @@
entry_points={
'nose.plugins.0.10': [
'typesafety = typesafety.noseplugin:TypesafetyPlugin'
]
],
'pytest11': ['pytest_typesafety = typesafety.pytest_typesafety'],
},
requires=[
'nose',
Expand Down
3 changes: 3 additions & 0 deletions tox.ini
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ envlist=py34,py33,py32
[testenv]
deps=
nose
pytest
mock
pep8
pylint
Expand All @@ -15,12 +16,14 @@ commands=
nosetests --with-coverage --with-doctest typesafety
behave --format progress2
{toxinidir}/scripts/copyright.py --check .
py.test typesafety

[testenv:py32]
# Note that some of the dependences are given with exact versioning
# because previous versions do not support Python 3.2 anymore.
deps=
nose
pytest
mock
pep8
logilab-common==0.62.1
Expand Down
2 changes: 2 additions & 0 deletions typesafety/pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[pytest]
python_files = check_pytest_*.py
49 changes: 49 additions & 0 deletions typesafety/pytest_typesafety.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#
# Copyright (c) 2013-2015 BalaBit
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#


def pytest_addoption(parser):
parser.addoption(
'-T', '--enable-typesafety', action='append', metavar='MODULE',
help='Enable typesafety for the given modules'
)


def pytest_configure(config):
if config.getoption('enable_typesafety'):
import typesafety
import functools

enabled_for = tuple(
mod.split('.') for mod in config.getoption('enable_typesafety')
)
filter_func = functools.partial(__check_need_activate,
enabled_for=enabled_for)
typesafety.activate(filter_func=filter_func)


def pytest_unconfigure(config):
if config.getoption('enable_typesafety'):
import typesafety
typesafety.deactivate()


def __check_need_activate(module_name, enabled_for):
module_name = module_name.split('.')
return any(
module_name[:len(name)] == name for name in enabled_for
)
46 changes: 46 additions & 0 deletions typesafety/pytests/check_pytest_plugin.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#
# Copyright (c) 2013-2015 BalaBit
# This library is free software; you can redistribute it and/or
# modify it under the terms of the GNU Lesser General Public
# License as published by the Free Software Foundation; either
# version 2.1 of the License, or (at your option) any later version.
#
# This library is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# Lesser General Public License for more details.
#
# You should have received a copy of the GNU Lesser General Public
# License along with this library; if not, write to the Free Software
# Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
#
"""
Test module for the pytest plugin; named "check_*" instead of "test_*" so
nosetests doesn't try to collect it.
"""
import pytest

# pylint: disable=invalid-name
pytest_plugins = 'pytester'


@pytest.mark.parametrize('enabled', [True, False])
def test_enabled(testdir, enabled):
testdir.makepyfile(
identity='''
def identity(i: int):
return i
''',
test_identity='''
from identity import identity
def test_identity():
assert identity("name") == "name"
''')

args = ['test_identity.py']
if enabled:
args = ['--enable-typesafety', 'identity'] + args
result = testdir.runpytest(*args)

expected = "*1 failed*" if enabled else "*1 passed*"
result.stdout.fnmatch_lines([expected])

0 comments on commit 760ffb0

Please sign in to comment.