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

Better assertion' message check #326

Open
l3r8yJ opened this issue Feb 17, 2024 · 4 comments
Open

Better assertion' message check #326

l3r8yJ opened this issue Feb 17, 2024 · 4 comments
Assignees
Labels
enhancement New feature or request help wanted Extra attention is needed question Further information is requested

Comments

@l3r8yJ
Copy link
Contributor

l3r8yJ commented Feb 17, 2024

@volodya-lombrozo I think the description of each assertion should contain placeholders to describe what we actually want to get and what was wrong, otherwise the description message doesn't make any sense, wdyt?

invalid:

@Test
void addsTwoNumbers() {
    int a = 1;
    int b = 1;
    assertThat(
        "1 + 1 is 2",
        sum(a, b)
    ).equalTo(2)
}

valid:

@Test
void addsTwoNumbers() {
    int expected = 2;
    int actual = sum(1, 1)
    assertThat(
        // Can be in any format from String#format up to FormattedText#new
        "1 + 1 should be equal to %s, but was %s".formatted(expected, actual),
        actual
    ).equalTo(expected)
}
@l3r8yJ l3r8yJ changed the title Better message assertion check Better assertion' message check Feb 17, 2024
@volodya-lombrozo
Copy link
Owner

"@l3r8yJ, that's a good suggestion actually. I thought about it myself. The check might be rather useful on one hand but require lots of boilerplate code on the other hand. assertThat from Hamcrest and assertEquals from JUnit already print the information if values aren't equal:

@Test
void calculatesSum() {
  Assertions.assertEquals(2, 1 + 1 + 1, "1 + 1 is 2");
}

This gives:

org.opentest4j.AssertionFailedError: 1 + 1 is 2 ==> 
Expected :2
Actual   :3

So, as you can see, there is no need to duplicate expected and actual values twice.

@volodya-lombrozo volodya-lombrozo added the question Further information is requested label Feb 18, 2024
@volodya-lombrozo volodya-lombrozo self-assigned this Feb 18, 2024
@l3r8yJ
Copy link
Contributor Author

l3r8yJ commented Feb 18, 2024

@volodya-lombrozo sure, good point about boilerplate code, but it seems to be very obvious what happens on simple cases, but in real code unit tests are more complicated. So, even in simple case like this, 2, 1, 1, looks like magic numbers. When we clearly inform what we expecting in test, in my opinion, it's gonna be easier to read.

assertThat from Hamcrest and assertEquals from JUnit already print the information if values aren't equal:

Yes, but in text with placeholders we can put some explanations or additions, what actually was wrong. For example:

"The %s should contain %s.\n\tWhen of %s#methodName was called, this should have modified the original collection, but it didn't happen."
    .formatted(collection, secretElement, caller)

and now, i'm know that i should go to caller and watch what was wrong

Yes, this example very verbose, but, i think it's shows what i'm trying to say, wdyt?

@volodya-lombrozo
Copy link
Owner

volodya-lombrozo commented Feb 18, 2024

@l3r8yJ I agree with you. Let's start by adding this feature as an experimental one. If it demonstrates promising results, we can then integrate it into our standard list.

@volodya-lombrozo volodya-lombrozo added enhancement New feature or request help wanted Extra attention is needed labels Feb 18, 2024
@l3r8yJ
Copy link
Contributor Author

l3r8yJ commented Feb 18, 2024

@volodya-lombrozo sure, this feature quite controversial; it would be better if we can switch it back and forth

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request help wanted Extra attention is needed question Further information is requested
Projects
None yet
Development

No branches or pull requests

2 participants