How to use MethodInterception Concept to invoke method, and by not using "TestMethod" attribute

Palak Shah

Well-known member
Joined
Apr 29, 2020
Messages
97
Programming Experience
1-3
Hello,

I want to create custom attribute which will work as "TestMethod" attribute and in class methods, instead of writing {TestMethod] - I want to use custom AOP attribute, but I am not able to do that, can someone help?

Custom Attribute:
namespace Framework.Attributes
{
    /// <summary>
    /// This attribute will handle the exception
    /// </summary>
    [Serializable]
    public class HandleException : MethodInterceptionAspect
    {
        public string testName = string.Empty;
        public override void OnInvoke(MethodInterceptionArgs args)
        {
            args.Proceed();
            //if (args.Exception != null)
            //{
            //    testName = GetTestName(args);
            //    var logger = GetLogger();

            //    logger.Info($"Application has got exception in method-{args.Method.Name}");
            //    logger.Error($"Exception : {(!string.IsNullOrEmpty(args.Exception.Message) ? args.Exception.Message : "")}");
            //    logger.Error($"Inner Exception : {(!string.IsNullOrEmpty(args.Exception.InnerException.Message) ? args.Exception.InnerException.Message : "")}");
            //}
        }
        
        public ExtentTest GetLogger()
        {
            var report = ReportSingleton.GetInstance();
            var loggers = LoggersSingltons.GetInstance();
            var logger = loggers.Single(x => x.Model.Name == testName);
            return logger;
        }
    }
}

Main Class Code:
public class Scenario1_3DProduct_EndToEnd : ManualRegressionSuite.Model.Abstractions.Test.TestBaseMSTestV2.TestBase
    {
        public CommonMethods CommonMethods { get; set; }
        public string countryName = "Austria";

        [TestInitialize]
        public void BeforeTest()
        {
            BaseSetup();
            CommonMethods = new CommonMethods(shared_parameters, _logger);
        }

        [TestCleanup]
        public void AfterTest()
        {
            BaseCleanUp();
        }

        [TestCategory("BOL")]
        //[TestMethod]
        [HandleException]
        public void BOL_GB_3D_Product_EndToEnd_Scenario1()
        {
            LineSeparatorHelper lineSeparator = GetInstance<LineSeparatorHelper>();
            lineSeparator.LineFormatter("Executing 3D Product End to End Scenario for BOL");

          

            lineSeparator.LineFormatter("Execution for 3D Product End to End Scenario BOL is completed");
        }
    }

Since in MsTest V1 - I am not able to inherit "TestMethod" due to it's sealed
 
Use Xunit instead of MSTest. I believe it's attributes are not sealed. And of they are sealed, it is open source so you can make a fork where you can unseal it.
 
e57.jpg
You chose poorly.
 

Latest posts

Back
Top Bottom