Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

--no-monitor breaks pytest.raises and django_assert_num_queries #79

Open
Veritogen opened this issue May 6, 2024 · 2 comments · May be fixed by #81
Open

--no-monitor breaks pytest.raises and django_assert_num_queries #79

Veritogen opened this issue May 6, 2024 · 2 comments · May be fixed by #81

Comments

@Veritogen
Copy link

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!

@lhpt2
Copy link

lhpt2 commented May 7, 2024

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.

@lhpt2
Copy link

lhpt2 commented May 8, 2024

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).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
2 participants