This repository has been archived by the owner on May 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #12 from nicoddemus/pytest-plugin
Pytest plugin
- Loading branch information
Showing
7 changed files
with
116 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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]> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[pytest] | ||
python_files = check_pytest_*.py |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |