Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
3.7k views
in Technique[技术] by (71.8m points)

c# - Can I get Exception Error from the Method I am running?

Is there a way I can get the exception to be fed in BeforeAfterTestAttribute? My goal is to log out the Method error if it is not executed correctly and perform other actions. This comes more or less from This Post

[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method)]
class ReportBeforeAtribute : BeforeAfterTestAttribute
{
    public override void Before(MethodInfo methodUnderTest)
    {
        Trace.Log("Starting Message");
    }

    public override  void After(MethodInfo methodUnderTest)
    {
        Trace.Log("Method Ended Fine ");
    }

    public void After(MethodInfo methodUnderTest, Exception ex)
    {
        Trace.Log("Exeption " + ex.Message);
    }
}

The attribute would be used here:

  [ReportBefore]
    public class TestClass
    {
        [Theory]
        public async Task CheckMethod()
        {
        //Here Is code which Either runs ok or not
        //If breaks I want to pass the Exception to The Attribute in the 
        // After Method
        }
    }

From the above code The first and second part works, though the third method is what I want to create, this is what is not working.

is there any way to achieve this?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

If you want the exception not to break your test, you should probably expect it. The link describes a little how you can work with exceptions.

https://fluentassertions.com/exceptions/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...