sentry-python icon indicating copy to clipboard operation
sentry-python copied to clipboard

Better Test structure

Open antonpirker opened this issue 6 months ago • 1 comments

Right now it is hard to get tests right and there is a lot of magic in conftest.py (and probably other places) and the isolation of tests is alo lacking (resulting in use of pytest.mark.forked usage)

Idea of a better test structure:


def test_something(sentry_init):
    sentry_client = sentry_init(dsn, ...)

    # do something

    sentry_client.transport.envelopes
    # -> array of envelopes

    sentry_client.transport.events
    # -> array of captured events

There is a custom transport on the client that collects data on class internal arrays that can be accessed via properties.

Those arrays should be cleaned after each test run. Should also work in async, and gevent, and eventlet code.

Basically just have one fixture for testing most of the stuff

Also tests should never alter the global state so they are self contained and can run in parallel without pytest markers.

antonpirker avatar Jun 17 '25 06:06 antonpirker

we have a very clean structure on ruby now (and we even expose it so end users can use the same methods to test sentry logic on their end)
https://github.com/getsentry/sentry-ruby/blob/32ca39e4df58865be0e28c0cb11241d766105efe/sentry-ruby/lib/sentry/test_helper.rb#L4-L56

so you can use it for inspiration
you use it like this (setup, teardown and access the events/envelopes)
https://github.com/getsentry/sentry-ruby/blob/32ca39e4df58865be0e28c0cb11241d766105efe/sentry-ruby/spec/sentry/test_helper_spec.rb#L49-L66

sl0thentr0py avatar Jun 17 '25 11:06 sl0thentr0py