-
Notifications
You must be signed in to change notification settings - Fork 758
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
Request for Assert.Multiple #1377
Comments
I have no idea what this means. Please re-open with a specific description of the requested feature. |
In nUnit now you can do something called multiple assertion for one testMethod which is, as I know is a system adopted to recognize all assertion failures when you have several assertions in one method. In other words, normally when you have multiple assertions in a method, if one assertion fails, only that failure is displayed and nothing about the rest. But what this feature enables is, enables assertion failure reporting for all assertions in one method. Here is an example, The result shows only the first failure, but any other failures or successes are not displayed, Hope I put it clearly enough to get this request reopened 😅 I believe the link provided will help you understand the request 😊 Thank you!!! |
The XUnit |
Oh it could be so, I don't know. But still it would be really handy to have this feature found in nUnit, in xUnit as well. So that's why this request is made 😇. Hope it's a good requirement to have 🤔 |
You could write it yourself in about 10 lines of code: take a |
I guess I can, but wouldn't it be nice for this to be a part of the framework? NUnit repo has had a long thread discussing the benefits/implementation of this before: nunit/nunit#391 The syntax they ended up using is also very simple to understand: https://github.com/nunit/docs/wiki/Multiple-Asserts |
There are plenty of things we could do, but for many reasons we choose not to. This reasons include: (a) implementation and maintenance cost; (b) competing priorities; (c) philosophical objections to features. In this case, you're running afoul of (c). We do not believe that a unit testing framework should ever run more than one failing assertion. |
I know this is an old topic but I disagree that you should never have more than one assertion. We are testing our website using selenium and say we open a certain page. We might make 20 assertions for various html elements on that page and we want to see all of the results. Now we obviously could write a separate test for each element but keep in mind that the page needs to be opened at each test. So now we are opening the same page 20 times. Or we could open the page at the beginning of the first test but then we are depending on the order of the tests which is bound to be changed by some programmer who replaces me in 2 years. |
@bfis108137 Absolutely agree. Especially with "QA high level tests" like Selenium, it is so high cost (of time) to reach a place in the application, that there is a need to merge assertions. And there is a risk that the more I assert, the higher probability of failure. I pretty well understand the "backend developer" point of view why the unit tests shold make only a single assertion, but there are other people using the frameworks, and some use cases require mutliple assertions. |
anyone comes across this, an approach that worked for me, although exact types (see the comments on derived types) https://stackoverflow.com/a/47302498/140618
|
Actually there's a good "unit testing" reason we should have multiple assertions in a single test - because we actually testing one thing, think about a message object returned from the code under test, that message has several properties and values, but I'm actually testing one message - one result, from time to time you will find a test that has a single outcome that manifest itself as multiple values - http result + status is one such instance.
I understand you do not want to provide a new capability that would enable developers to write bad unit tests, and I have seen this featured abused in NUnit and JUnit but I think the reason we need it is the original sin of all JUnit based frameworks in .NET and Java - they throw exception on error, this is a mistake that was never corrected and one more pain point for developers starting with unit testing, I think that this alone merits at least thinking bout adding this simple solution. |
@dhelper wrote:
You mis-attribute what I said. I didn't say you shouldn't run one assertion per test. This is what I actually said:
(Emphasis changed to highlight what I think is the key difference between what you're saying and what I'm saying.) It's fine to write multiple assertions to test one logical thing, but I disagree that continuing to run most assertions when one has failed is something that is generally the right behavior in a unit testing framework. Pretty much every argument I've ever drilled down on with people who want multiple failures per test weren't actually writing unit tests; hence, why I believe this is a feature that can and should belong outside of xUnit.net itself. If you disagree philosophically with what I just wrote, then that's fine. But I've already considered your point, and I'm not swayed. |
I too misunderstood this sentence. Given that one does not control when an assertion will fail,
is easily interpreted as
because that would be the only way to guarantee that you never have more than one failing assertion. So, thank you for clarifying! Can you confirm my new understanding of the sentence ? (right below)
|
Yes, that's an accurate representation. Here's a trivial example of why it usually makes sense to just fail fast: // ...
Assert.NotNull(someValue);
Assert.Equal(42, someValue.SomeProperty);
// ... The second assert would fail with a |
Is kind of sad to see how this request for a feature became the above set of interactions were the position of the person that could to do the change is just "do it yourself I wont do it for philosophical reasons" and those reason end up being just point of views. It seems like @bradwilson rather have each person implement their own code than providing and out-of-box implementation that shows the way he thinks it should work. (besides "throw new NotImplementedException();" of course) |
When I get an entity from an method under test, I want to validate all it's properties at once, instead of writing a separate test per property. Still, this counts as only one assertion, because I validate a single entity. One way around that in xUnit to use a custom Validator that throws a single Exception with all the validation errors. You could use FluentValidation for that. Another is to use a dedicated assertion framework. In example using FluentAssertion with an AssertionScope. |
This is shipping as part of v3, in two forms.
xunit/src/xunit.v3.assert.tests/Asserts/MultipleAssertsTests.cs Lines 38 to 80 in caa2cb4
xunit/src/xunit.v3.assert.tests/Asserts/EquivalenceAssertsTests.cs Lines 340 to 367 in caa2cb4
|
Is there any support for porting NUnit's
Assert.Multiple()
functionality to xUnit?I've found it a very nice syntax to use and a lot quicker than writing custom constraints.
The text was updated successfully, but these errors were encountered: