QABash Community Forum

Please or Register to create posts and topics.

hard vs. soft assertions in Pytest

😩 “Why did my test stop right there?”

Picture this: You're testing a login page with multiple checks — the page title, the input field visibility, the login button. You hit run, and boom 💥 — the test fails on the first failed assertion, skipping the rest.

You sigh and think, “Wait, I wanted to know if all three things failed or not!”

Well, friend, welcome to the world of hard vs. soft assertions in Pytest — where how you assert makes all the difference.


🔍 What’s an Assertion in Testing?

In Python testing (especially with Pytest), an assert is like a checkpoint. You're saying,

“Hey, this must be true — or something’s broken.”

Assertions help validate:

  • UI elements 🖥️

  • API responses 🌐

  • Calculated results 🧮

  • Business logic 💼

But not all asserts behave the same...


🧱 Hard Assert: The Strict Gatekeeper

A hard assert immediately stops test execution if the condition fails.

📌 Example:

def test_login_page():
assert "Login" in driver.title
assert driver.find_element(By.ID, "username").is_displayed()
assert driver.find_element(By.ID, "login-button").is_enabled()

If the first assert fails, the test exits — no second chances.

⚠️ Why it's risky: You may miss other broken things because the test didn't get that far.


🧊 Soft Assert: The Friendly Reporter

A soft assert collects all failures and reports them at the end, instead of stopping right away.

Pytest doesn’t natively support soft asserts — but you can get it using pytest-check, softest, or custom context managers.

📦 Installing pytest-check:

pip install pytest-check

✅ Example:

import pytest_check as check
def test_login_page():
check.is_in("Login", driver.title)
check.is_true(driver.find_element(By.ID, "username").is_displayed())
check.is_true(driver.find_element(By.ID, "login-button").is_enabled())

Now, all three assertions will run, and failures will be aggregated.


🧪 When to Use Which?

Situation Use Hard Assert Use Soft Assert
Critical checks
UI validation ⚠️
API smoke tests
Functional regression
Debugging flaky tests

⚒️ Pro Tips to Level Up 💡

Tip #1: Use soft asserts in UI tests to validate multiple elements in one go 🧩
Tip #2: Use hard asserts early to fail fast for critical path checks 🚨
Tip #3: Combine both to balance precision and coverage ⚖️
Tip #4: Wrap soft asserts in helper functions for readability 🧼
Tip #5: Don’t overuse soft asserts — know when stopping early is better ⏹️


🧱 Common Mistakes to Avoid

🚫 Assuming Pytest has soft asserts built-in — you need pytest-check or similar 🧰
🚫 Mixing up assert libraries — stay consistent (either check or native) 🧪
🚫 Ignoring test result logs — soft assert failures may not be obvious unless reviewed 🔍
🚫 Using soft asserts in authentication or database tests where failures can cascade 🔒


🎯 Expert Insights: A Little-Known Power Move

You can combine hard and soft asserts in the same test.
Use hard asserts for must-pass checks (like reaching a page), and soft asserts for detailed validations once you're confident the flow is working.

This lets your tests fail smartly instead of dying silently.


Fail Fast or Fail Friendly?

Both hard and soft asserts have their place.
The real skill? Knowing when to be strict and when to be forgiving.

Start with small changes — try pytest-check in one test. Watch how it helps you catch more issues without frustration.

Scroll to Top
×