Comments (12)
@robdalsanto you can specify a specific exception type
Assert.ThrowsException<T>(Action action)
Assert.ThrowsExceptionAsync<T>(Func<Task> action)
from testfx.
@jimontheriver I agree. ExpectedException would probably be tagged with an obsolete tag in a future release. So building a project with an ExpectedException would start throwing warnings.
/cc @pvlakshm.
from testfx.
I was actually thinking it would be better to extend it so that you can be more specific with the exception. i.e. permit specifying the error message (and/or the paramName). The attribute can save a lot of boilerplate code in tests when you are testing multiple failure paths.
I agree though that, as is, its not as accurate as it could be. For instance, consider this code:
public void DoWork(Person person)
{
if (person == null) throw new ArgumentNullException(nameof(person));
if (string.IsNullOrWhiteSpace(person.Forename)) throw new ArgumentException($"{nameof(person.Forename)} should be specified", nameof(person));
if (string.IsNullOrWhiteSpace(person.Surname)) throw new ArgumentException($"{nameof(person.Surname)} should be specified", nameof(person));
if (string.IsNullOrWhiteSpace(person.Email)) throw new ArgumentException($"{nameof(person.Email)} should be specified", nameof(person));
}
[TestMethod, ExpectedException(typeof(ArgumentException))]
public void WhenSurnameIsNull_ExceptionThrown()
{
var person = new Person() { Forename = "Test" };
DoWork(person);
}
As is, there is no guarantee that the exception is being thrown on the property I'm intending. If someone inadvertently swaps the checks around, or adds a new property, this test could still pass even though its not on the correct check.
To do this without the attribute requires something like this:
[TestMethod, ExpectedException(typeof(ArgumentException))]
public void WhenSurnameIsNull_ExceptionThrown()
{
var person = new Person() { Forename = "Test" };
try
{
DoWork(person);
}
catch (ArgumentException ex)
{
Assert.IsTrue(ex.ParamName == nameof(Person.Surname)
&& ex.Message == $"{nameof(Person.Surname)} should be specified");
}
}
But by extending the attribute, you could do this:
[TestMethod]
[ExpectedException(typeof(ArgumentException), ParamName := "person", $"{nameof(Person.Surname)} should be specified")]
public void WhenSurnameIsNull_ExceptionThrown()
{
var person = new Person() { Forename = "Test" };
DoWork(person);
}
This lets developers be much more specific with their Exception Testing. Overloading the constructors will allow this enhanced behaviour without breaking existing tests.
from testfx.
We make use of this attribute, so please keep it. That you can specify a specific exception type is meeting our requirements for avoiding false 'test pass' situations.
Without this, we would wind up writing our own try-catch-verify helpers.
--- edit Sept 18, 2108 ---
Thanks @ziomyslaw, I failed to notice the Assert.ThrowsException. Will plan to move to that.
from testfx.
I suggest we make the attribute obsolete and provide a code refactoring to automatically rewrite the code to use new/better APIs.
from testfx.
@fforjan Sadly there are none but I really want to add them! I was expecting to be working on that early this year but priorities have changed
from testfx.
After a couple of internal discussions, we want to try to be more strictly following semantic versioning as we have some users with huge test suite and this kind of change would lead to many failures and some time to fix or ignore. Pushing back to v4 as it will allow breaking changes.
from testfx.
We would need to fix the intellitest wizard that outputs ExpectedException in-tandem with this change so as not to break that workflow.
from testfx.
@Evangelink do you already have the code analyser in this repository for the refactoring tool ? I was looking for it but could not found it ?
from testfx.
We will deprecate the attribute as part of 3.3.0
, the analyzer is added as part of 3.2.0
.
from testfx.
@Evangelink you self-assigned this ticket in December. It still has the Help-Wanted label. I assume that label is no longer applicable and should be removed?
from testfx.
Hi @Kissaki,
I wanted to release v4 a long time ago but we decided to postpone its release so we can group more breaking changes to avoid doing many breaks increments.
We have already provided the analyzer so we only need to drop the attribute in v4.
from testfx.
Related Issues (20)
- Please update sample to show `dotnet test` integration HOT 2
- Please update sample to show Test Explorer integration HOT 2
- Intermittent deadlock on Linux using native AoT support from console output HOT 5
- Allow custom --help output HOT 2
- Progress remains on screen when output is written from test
- Add options to configure output. HOT 1
- Platform.MSBuild should allow generating a helper for registration of extensions
- Show slowest tests HOT 2
- Cancelling after a predefined number of failed tests HOT 10
- [Testing Platform] Command line arguments file source HOT 3
- [Testing Platform] Timeout option HOT 6
- Add `--?` alias for `--help` HOT 1
- Remove `Condition=" '$(GenerateTestingPlatformEntryPoint)' == 'True' "` from extensions
- Improve logging APIs
- Add analyzer doc for the missing ones HOT 3
- [Testing Platform] Count tests HOT 2
- [Testing Platform] Batch Filter HOT 10
- [Testing Platform] Aggregate Filter
- Humanize the timer
- Update default naming for test on output HOT 1
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from testfx.