Comments (2)
Hi @gao-artur,
Thanks for the suggestion. I am not sure to fully understand what you would like to be able to do. At the moment, all ThrowsExceptionAsync
gives you a Task<TException>
so awaiting that gives you your exception.
from testfx.
Hi @Evangelink. When thrown exception is different from the expected, it may be helpful to get the actual exception to create the error message. For example, you expect to get OperationCancellaedException
, but RpcException
is thrown instead. RpcException
has a few properties with useful information, like Trailers
for example. You would want to use this additional data in the error message (second argument in ThrowsExceptionAsync
method). Currently, the framework will create an error message based on the user message, exception type, message, and stack trace.
if (!typeof(T).Equals(ex.GetType()))
{
userMessage = BuildUserMessage(message, parameters);
finalMessage = string.Format(
CultureInfo.CurrentCulture,
FrameworkMessages.WrongExceptionThrown,
userMessage,
typeof(T).Name,
ex.GetType().Name,
ex.Message,
ex.StackTrace);
ThrowAssertFailed("Assert.ThrowsException", finalMessage);
}
I suggest passing the exception to the user message builder. Note the Func<Exception?, string> messageBuilder
instead of string message
public static async Task<T> ThrowsExceptionAsync<T>(Func<Task> action, Func<Exception?, string> messageBuilder, params object?[]? parameters)
where T : Exception
{
...
string userMessage, finalMessage;
try
{
await action().ConfigureAwait(false);
}
catch (Exception ex)
{
if (!typeof(T).Equals(ex.GetType()))
{
userMessage = BuildUserMessage(messageBuilder, ex, parameters);
...
ThrowAssertFailed("Assert.ThrowsException", finalMessage);
}
return (T)ex;
}
userMessage = BuildUserMessage(messageBuilder, exception: null, parameters);
...
ThrowAssertFailed("Assert.ThrowsException", finalMessage);
...
}
Note the Func<Exception?, string> messageBuilder
instead of string? format
internal static string BuildUserMessage(Func<Exception?, string> messageBuilder, Exception? exception, params object?[]? parameters)
{
...
var format = messageBuilder(exception);
return parameters == null || parameters.Length == 0
? ReplaceNulls(format)
: string.Format(CultureInfo.CurrentCulture, ReplaceNulls(format), parameters);
}
from testfx.
Related Issues (20)
- Class Cleanup gets called immediately after a single test case using VSTestConsoleWrapper HOT 5
- AssemblyInitialize/Cleanup should not be executed when all tests are ignored HOT 7
- Allow not only object[] but also Tuple and ValueTuple for DynamicData HOT 1
- Cannot discover tests in a UWP ARM64 app HOT 6
- Test stays "not run" if TestContext property is readonly, no error HOT 3
- TestAdapter: Wrongly grouped DataTestMethod HOT 7
- TestMethod displayName is being ignored if DataRowAttribute is specified HOT 6
- [MSTest][Discovery] Unable to load types from the test source ... Could not load file or assembly HOT 11
- DataRow: Nulls and empty string are displayed as no data in display name HOT 3
- Do not rely on testhost to define thread apartment HOT 6
- Integrating OpenTelemetry in end-to-end tests
- LogMessage System.FormatException HOT 2
- AZDO failing on executor://mstestadapter/v2': Could not load file or assembly Microsoft.VisualStudio.TestPlatform.TestFramework.Extensions HOT 4
- Data tests (`DynamicDataAttribute`) with readonly structs do not work (default is passed) HOT 3
- Separate Test Results and Logs for AssemblyInitialize, AssemblyCleanup, ClassInitialize and ClassCleanup HOT 2
- Replacement for AssemblyResolution in newer .NET versions? HOT 1
- Release notes has duplicates HOT 2
- UWP/WASDK Tests won't run upgrading to 3.1.1 HOT 6
- DeploymentItem path normalization
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.