0
0
PyTesttesting~3 mins

Why @pytest.mark.xfail for expected failures? - Purpose & Use Cases

Choose your learning style9 modes available
The Big Idea

What if your test failures could tell you which ones really need your attention?

The Scenario

Imagine you have a test that checks a feature still under development or a known bug that is not fixed yet.

You run all tests manually and see this test fail every time, mixing it with unexpected failures.

The Problem

Manually tracking which failures are expected wastes time and causes confusion.

You might spend hours investigating failures that you already know about.

This slows down your work and increases frustration.

The Solution

The @pytest.mark.xfail tag tells pytest that a test is expected to fail.

Pytest then marks these failures differently, so you can focus on new, unexpected problems.

This saves time and keeps your test reports clear.

Before vs After
Before
def test_feature():
    assert feature() == expected  # fails but no special mark
After
@pytest.mark.xfail
def test_feature():
    assert feature() == expected
What It Enables

You can easily separate known issues from new bugs, making your testing smarter and faster.

Real Life Example

When a new feature is half-built, you mark its test with @pytest.mark.xfail so your test suite stays green while you finish the work.

Key Takeaways

Manually tracking expected failures wastes time and causes confusion.

@pytest.mark.xfail clearly marks tests expected to fail.

This helps focus on real new problems and speeds up testing.