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

--liveserver-verbose parameter could be useful #1023

Open
mpasternak opened this issue Aug 29, 2022 · 2 comments
Open

--liveserver-verbose parameter could be useful #1023

mpasternak opened this issue Aug 29, 2022 · 2 comments

Comments

@mpasternak
Copy link

In my development this is a recurring pattern: I run Selenium tests just to get a simple "500 Internal server error" page and no output from the standard live_server fixture. This is fine, because this is exactly what Django does officially and what people coming from Django testing framework to pytest-django should expect. Also I don't feel like extending live_server test case as it is already threaded and pretty environment-sensible if you ask me, so I'd rather not touch it as perhaps other people's testing workflows depend on it's documented and undocumented behavior.

But what I would love for those hard-to-debug cases is a verbose live server - a server that dumps a traceback to the standard output of pytest. This is not a complicated thing to do, I already have one but I needed to patch a few things in pytest-django. To get it all clean and done I will need some extra time, but I'm documenting this in hope somebody will pick this up or give some insightful feedback at least.

So basically in live_server_helper.py one needs to add:

class VerboseWSGIRequestHandler(WSGIRequestHandler):
    def handle_uncaught_exception(self, request, resolver, exc_info):
        traceback.print_tb(exc_info[2], file=sys.stderr)
        return super(WSGIHandler, self).handle_uncaught_exception(request, resolver, exc_info)

class VerboseLiveServerThread(LiveServerThread):
    def _create_server(self):
        return ThreadedWSGIServer((self.host, self.port), VerboseWSGIRequestHandler, allow_reuse_address=False)

Then later, in LiveServer.__init__ change self.thread line to:

        self.thread = VerboseLiveServerThread(host, **liveserver_kwargs)

This will make your pytest session dump the output on exit (if -s is given), tracebacks included.

For me this is a big step forward from the default configuration of live_server, which acts just like in Django, which is explicitly silent (https://github.com/django/django/blob/0dd29209091280ccf34e07c9468746c396b7778e/django/test/testcases.py#L1636). For me, having a chance of getting a traceback on stdout is pretty much enough. What I would love in the future, perhaps, is the ability of giving the live_server fixture a proper configuration, for example enabling or disabling some settings or running the live_server from a different settings.py file.

So... if you're a dev and want to drop a comment, please let me know what you think and what would be the easiest way to extend LiveServer. I think I'll go with factory approach, which seems to be common through pytest-django source. After some time I'll submit a PR. Thanks!

@mpasternak
Copy link
Author

Wow, I even got a thumbs up. Thanks, @mgax!

Here's the code: #1024

@mpasternak mpasternak changed the title verbose_live_server fixture could be useful --liveserver-verbose parameter could be useful Sep 6, 2022
@mpasternak
Copy link
Author

Hi, just trying to catch your attention. This is useful.

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

No branches or pull requests

1 participant