Skip to main content

A pytest plugin to report flaky tests to Sentry.io

Project description

https://travis-ci.com/untitaker/pytest-sentry.svg?branch=master

pytest-sentry is a pytest plugin that sends error reports for flaky but ultimately not completely broken tests to Sentry.

What and Why

Let’s say you have a testsuite with some flaky tests that randomly break your CI build due to network issues, race conditions or other stuff that you don’t want to fix immediately. The known workaround is to retry those tests automatically, for example using pytest-rerunfailures.

One concern against plugins like this is that they just hide the bugs in your testsuite or even other code. After all your CI build is green and your code probably works most of the time.

pytest-sentry tries to make that choice a bit easier by tracking flaky test failures in a place separate from your build status. Sentry is already a good choice for keeping tabs on all kinds of errors, important or not, in production, so let’s try to use it in testsuites too.

How

The prerequisite is that you already make use of pytest and pytest-rerunfailures in CI. Now install pytest-sentry and set the PYTEST_SENTRY_DSN environment variable to the DSN of a new Sentry project.

Now every test failure that is “fixed” by retrying the test is reported to Sentry, but still does not break CI. Tests that consistently fail will not be reported.

Advanced Options

pytest-sentry supports marking your tests to use a different DSN, client or hub per-test. You can use this to provide custom options to the Client object from the Sentry SDK for Python:

import random
import pytest

from sentry_sdk import Hub
from pytest_sentry import Client

@pytest.mark.sentry_client(None)
def test_no_sentry():
    # Even though flaky, this test never gets reported to sentry
    assert random.random() > 0.5

@pytest.mark.sentry_client("MY NEW DSN")
def test_custom_dsn():
    # Use a different DSN to report errors for this one
    assert random.random() > 0.5

# Other invocations:

@pytest.mark.sentry_client(Client("CUSTOM DSN"))
@pytest.mark.sentry_client(lambda: Client("CUSTOM DSN"))
@pytest.mark.sentry_client(Hub(Client("CUSTOM DSN")))

The Client class exposed by pytest-sentry only has different default integrations. It disables some of the error-capturing integrations to avoid sending random expected errors into your project.

Always reporting test failures

You can always report all test failures to Sentry by setting the environment variable PYTEST_SENTRY_ALWAYS_REPORT=1.

This can be enabled for builds on the master or release branch, to catch certain kinds of tests that are flaky across builds, but consistently fail or pass within one testrun.

License

Licensed under MIT, see LICENSE.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pytest-sentry-0.1.3.tar.gz (4.3 kB view details)

Uploaded Source

Built Distribution

pytest_sentry-0.1.3-py3-none-any.whl (5.2 kB view details)

Uploaded Python 3

File details

Details for the file pytest-sentry-0.1.3.tar.gz.

File metadata

  • Download URL: pytest-sentry-0.1.3.tar.gz
  • Upload date:
  • Size: 4.3 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: Python-urllib/3.8

File hashes

Hashes for pytest-sentry-0.1.3.tar.gz
Algorithm Hash digest
SHA256 4888727836fbd172f706e5e54175c4bce08436c718e66f9371ba26540d580149
MD5 4bf6cabb28671842f3b3febf1b861142
BLAKE2b-256 324c055b321c5df2451be9b903ce4be0d72827cd90c6bfab1a48dc616a8a479d

See more details on using hashes here.

File details

Details for the file pytest_sentry-0.1.3-py3-none-any.whl.

File metadata

File hashes

Hashes for pytest_sentry-0.1.3-py3-none-any.whl
Algorithm Hash digest
SHA256 e95f2a42765944fed88645738fff1bfcd9f73fbec3c6abf68676424656dedf97
MD5 549277843b5b53ac82dbf21f035361dd
BLAKE2b-256 bd91d006992b644e40c4d19d072dffb640046997a286dc64ce1024e0b894e787

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page