Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Implement soft assertions#2750

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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.

Already on GitHub?Sign in to your account

Open
mbroton wants to merge3 commits intomicrosoft:main
base:main
Choose a base branch
Loading
frommbroton:soft-assert

Conversation

mbroton
Copy link

Related to#1272

This PR adds support for soft assertions, allowing multiple assertion failures to be collected before raising them together. I implemented this using a context manager pattern since it feels more natural and Pythonic:

withexpect.soft()assoft_expect:awaitsoft_expect(page).to_have_title("wrong title")awaitsoft_expect(page.locator("#div1")).to_have_text("wrong")response=awaitpage.request.get("...")awaitsoft_expect(response).to_be_ok()

Example error report:

>       with expect.soft() as soft_expect:E       AssertionError: Soft assertion failuresE       1. Page title expected to be 'wrong title'E       Actual value: Page TitleE       Call log:E         - PageAssertions.to_have_title with timeout 5000msE         -   - waiting for locator(":root")E         -     9 × locator resolved to <html>…</html>E         -       - unexpected value "Page Title"EE       2. Locator expected to have text 'wrong'E       Actual value: Text1E       Call log:E         - LocatorAssertions.to_have_text with timeout 5000msE         -   - waiting for locator("#div1")E         -     9 × locator resolved to <div>Text1</div>E         -       - unexpected value "Text1"EE       3. Response status expected to be within [200..299] range, was '404'E       Call log:E       → GET http://localhost:2137/non-existentE         -   user-agent: (...)E         -   accept: */*E         -   accept-encoding: gzip,deflate,brE         - ← 404 Not FoundE         -   content-type: text/plainE         -   transfer-encoding: chunked

Implementation Details

  • The implementation maintains backward compatibility with all existing assertion methods
  • Both sync and async APIs are fully supported with identical behavior
  • Error messages preserve the order of failures and include clear numbering (1., 2., etc.)
  • Custom error messages are properly handled and included in the final error report
  • The implementation handles nested errors correctly (when other exceptions occur within the soft assertion block)
  • Timeout settings are respected both at the global and individual assertion level

Initially, I usedcontextlib.contextmanager forexpect.soft() as it was a simpler implementation. However, this approach included some unrelevant information in the traceback when assertions failed. To address this, I created a customSoftAssertionContextManager class that gives us more control over the error handling and traceback presentation.

This is just one way to add soft assertions in Python Playwright. I think the context manager pattern works well with Python, but I’m totally open to other ideas if they fit the project better.

ltsuda reacted with thumbs up emojiltsuda reacted with eyes emoji
@mbroton
Copy link
Author

@microsoft-github-policy-service agree

@mbroton
Copy link
Author

I reviewed the failing checks and also tried to reproduce some of them locally without success. I couldn't find any indication that these failures are connected to my changes, so it seems they might be flaky or not related? Please let me know if you think further investigation is needed on my end.

@mbroton
Copy link
Author

Just wanted to follow up on this PR - happy to address any feedback or make adjustments if needed.

Sign up for freeto join this conversation on GitHub. Already have an account?Sign in to comment
Reviewers
No reviews
Assignees
No one assigned
Labels
None yet
Projects
None yet
Milestone
No milestone
Development

Successfully merging this pull request may close these issues.

1 participant
@mbroton

[8]ページ先頭

©2009-2025 Movatter.jp