CFMTech/pytest-monitor

--no-monitor breaks pytest.raises and django_assert_num_queries

Veritogen opened this issue · 2 comments

Describe the bug
When we add --no-monitor to the pytest.ini like so addopts = --no-monitor, pytest.raises, django_assert_num_queries and django_assert_max_num_queries (the latter two are provided by pytest-django) do not work anymore. They should be raising test failures.

To Reproduce
Steps to reproduce the behavior:
0. (install django, pytest, pytetst-monitor and pytest-django)

  1. create a django project
  2. create an app
  3. create Book model in app
  4. run tests below with --no-monitor => all tests pass
  5. run tests below without --no-monitor => all tests fail
import pytest

from books.models import Book

def test_raise_exception():
    with pytest.raises(Exception):
        x = 1 / 1
        
def test_query_assertion(django_assert_num_queries, db):
    with django_assert_num_queries(0):
        print(Book.objects.all())

def test_max_queries_assertion(django_assert_max_num_queries, db):
    with django_assert_max_num_queries(0):
        print(Book.objects.all())

Expected behavior
The tests should fail, even though the --no-monitor flag was provided.

Desktop (please complete the following information):

  • OS: Ubuntu 22.04
  • Python version: 3.11
  • Pytest version: 8.2.0
  • pytest-monitor version: 1.6.6

Additional context
Please ask if I missed to provide necessary information!

It happens due to a return statement in pytest-monitor.py where instead should be a raise. I'm currently fixing it and will post a pull request in a minute.

I had to refactor again and ran into some trouble with git (tried to reset an upstream branch I did the PR on) so I had to open a new PR.

If I just changed the line to raise the exception instead of returning it, the test runs ended in an endless loop when monitoring was turned on, so I kept the return statement instead and did the exception handling with the return value of wrapped_function() (line 216 in pytest_monitor.py) on the parent context (caller of wrapped_function() in the if branch).