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

Categories

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?


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
3.4k views
Welcome To Ask or Share your Answers For Others

1 Answer

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/


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
thumb_up_alt 0 like thumb_down_alt 0 dislike
Welcome to ShenZhenJia Knowledge Sharing Community for programmer and developer-Open, Learning and Share
...