Skip to content

Commit

Permalink
PYMON #2 - Correctly handles pytest outcomes situation (importorskip,…
Browse files Browse the repository at this point in the history
… fail & skip).
  • Loading branch information
Jean-Sebastien Dieu committed Mar 16, 2020
1 parent fb7bc20 commit 2a9a0e4
Show file tree
Hide file tree
Showing 3 changed files with 28 additions and 4 deletions.
Empty file added examples/pkg5/__init__.py
Empty file.
15 changes: 15 additions & 0 deletions examples/pkg5/test_special_pytest.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import pytest


@pytest.mark.skip(reason='Some special test to skip')
def test_that():
assert True


def test_that_one_is_skipped_too():
pytest.skip('Test executed and instructed to be skipped from its body')


def test_import_or_skip():
pytest.importorskip('this_module_does_not_exists')

17 changes: 13 additions & 4 deletions pytest_monitor/pytest_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,21 @@ def pytest_pyfunc_call(pyfuncitem):
Core sniffer logic. We encapsulate the test function in a sniffer function to collect
memory results.
"""
testfunction = pyfuncitem.obj
funcargs = pyfuncitem.funcargs
testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames}

def wrapped_function():
try:
funcargs = pyfuncitem.funcargs
testargs = {arg: funcargs[arg] for arg in pyfuncitem._fixtureinfo.argnames}
pyfuncitem.obj(**testargs)
except Exception:
raise
except BaseException as e:
return e

def prof():
m = memory_profiler.memory_usage((testfunction, (), testargs), max_usage=True)
m = memory_profiler.memory_usage((wrapped_function, ()), max_usage=True, retval=True)
if isinstance(m[1], BaseException): # Do we have any outcome?
raise m[1]
setattr(pyfuncitem, 'mem_usage', m)
setattr(pyfuncitem, 'monitor_results', True)
prof()
Expand Down

0 comments on commit 2a9a0e4

Please sign in to comment.