Skip to content
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 our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Stacktrace Substring Matching to Throwable Matchers #3998

Open
wants to merge 11 commits into
base: master
Choose a base branch
from

Conversation

neBM
Copy link

@neBM neBM commented May 3, 2024

This PR introduces the ability to match substrings and regular expressions in the stacktrace of a Throwable in the Kotest assertions library. This feature can be useful when writing tests that need to assert on specific parts of a stacktrace.

Changes:

  1. Added two new infix functions shouldHaveStacktraceContaining and shouldNotHaveStacktraceContaining to the Throwable class. These functions accept a String or Regex and use it to match against the stacktrace of the Throwable.
  2. Added corresponding matcher functions haveStacktraceContaining that create a Matcher<Throwable> to perform the actual matching logic.
  3. Added tests for these new functions in ThrowableMatchersTest.kt.

This enhancement provides more flexibility when writing tests involving exceptions and their stacktraces.

@Kantis
Copy link
Member

Kantis commented May 8, 2024

Could you please ensure that the failure message is tested as well?

E.g.

shouldFail {
  Throwable() shouldHaveStackTraceContaining "foobar"
}.message shouldBe """
   exact message here
"""

@Kantis
Copy link
Member

Kantis commented May 8, 2024

Also, exceptions spell it as "stackTrace", so IMO we should also capitalize the T.

@neBM
Copy link
Author

neBM commented May 13, 2024

Hi @Kantis, thank you for your feedback. I've implemented your suggestions 👍🏻

I noticed that the message with all its stack trace frames can make the test for the message in it's entirety exceptionally lengthy. Considering this, I thought about using a regex to assert on what follows the actual. What are your thoughts on this approach? Do you think it maintains the integrity of the test while addressing the length concern?

This is my first time contributing to open source, so any feedback is greatly appreciated!

@sksamuel
Copy link
Member

Looks good to me. Happy to merge if @Kantis is happy.

Copy link
Member

@Kantis Kantis left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good. I added a small test for the failure messages for the regex matchers as well. Thanks for contributing @neBM 🙌🏼

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants