c# - How to access test class state from custom IApplyToTest-attribute? - Stack Overflow

I have a test class with TestFixtureSource with two values: "Unit" and "Integration"

I have a test class with TestFixtureSource with two values: "Unit" and "Integration". These values are values of mode. Based on this mode the class sets dependencies up as mocks or real implementations. Some class tests are actual for both modes, while others are actual only for one of them. I am trying to find how to skip or ignore some tests depending on the mode value. So, I want to do something like the following

[TestFixtureSource(nameof(UnitOrIntegration))]
class Tests(string Mode)
{
     [SetUp]
     void Setup()
     {
         if (Mode == "Unit") { ... }
         if (Mode == "Integration") { ... }
     }

     [Test] //actual for both modes
     void Test1() { ... }
     
     [Test][WhenModeIs("Unit")]
     void Test2() { ... }
     
     [Test][WhenModeIs("Integration")]
     void Test3() { ... }
}

I found something that looks like can help

    [AttributeUsage(
          AttributeTargets.Method, 
          AllowMultiple = false,
          Inherited = false)]
    public class WhenModeIsAttribute(string Mode)
         : NUnitAttribute, IApplyToTest
    {
        public void ApplyToTest(Test test)
        {
            if (/* I need check mode here */)
            {
                test.RunState = RunState.Ignored;
                test.Properties.Set(
                   PropertyNames.SkipReason, 
                   $"It is not actual for {Mode}");
            }
        }
    }

But I can't find a way to access the mode value. How to do that?

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745672289a4639477.html

相关推荐

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信