Skip to content

Commit

Permalink
Avoid AttributeError for monitor_results on teardown (#51)
Browse files Browse the repository at this point in the history
Avoid AttributeError for monitor_results on teardown, plus test for skipping fixtures
  • Loading branch information
altendky committed Dec 15, 2021
1 parent 00fd018 commit f83281b
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 1 deletion.
3 changes: 3 additions & 0 deletions docs/sources/changelog.rst
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,9 @@
Changelog
=========

* :release:`NEXT`
* :bug:`#50` Fix a bug where a skipping fixture resulted in an exception during teardown.

* :release:`1.6.2 <2021-08-24>`
* :bug:`#40` Fix a bug that cause the garbage collector to be disable by default.

Expand Down
2 changes: 1 addition & 1 deletion pytest_monitor/pytest_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,7 @@ def prf_tracer(request):
ptimes_a = request.session.pytest_monitor.process.cpu_times()
yield
ptimes_b = request.session.pytest_monitor.process.cpu_times()
if not request.node.monitor_skip_test and request.node.monitor_results:
if not request.node.monitor_skip_test and getattr(request.node, "monitor_results", False):
item_name = request.node.originalname or request.node.name
item_loc = getattr(request.node, PYTEST_MONITOR_ITEM_LOC_MEMBER)[0]
request.session.pytest_monitor.add_test_info(item_name, request.module.__name__,
Expand Down
35 changes: 35 additions & 0 deletions tests/test_monitor.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,41 @@ def test_skipped():
assert not len(cursor.fetchall())


def test_monitor_pytest_skip_marker_on_fixture(testdir):
"""Make sure that pytest-monitor does the job without impacting user tests."""

# create a temporary pytest test module
testdir.makepyfile("""
import pytest
import time
@pytest.fixture
def a_fixture():
pytest.skip("because this is the scenario being tested")
def test_skipped(a_fixture):
assert True
""")

# run pytest with the following cmd args
result = testdir.runpytest('-v')

# fnmatch_lines does an assertion internally
result.stdout.fnmatch_lines(['*::test_skipped SKIPPED*'])

pymon_path = pathlib.Path(str(testdir)) / '.pymon'
assert pymon_path.exists()

# make sure that that we get a '0' exit code for the testsuite
result.assert_outcomes(skipped=1)

db = sqlite3.connect(str(pymon_path))
cursor = db.cursor()
cursor.execute('SELECT ITEM FROM TEST_METRICS;')
assert not len(cursor.fetchall())


def test_bad_markers(testdir):
"""Make sure that pytest-monitor warns about unknown markers."""
# create a temporary pytest test module
Expand Down

0 comments on commit f83281b

Please sign in to comment.